Skip to content

Instantly share code, notes, and snippets.

@dcinzona
Created January 21, 2026 17:13
Show Gist options
  • Select an option

  • Save dcinzona/3c6e77a6a1555688b4135f8eb8aa4525 to your computer and use it in GitHub Desktop.

Select an option

Save dcinzona/3c6e77a6a1555688b4135f8eb8aa4525 to your computer and use it in GitHub Desktop.
Tooling API APEX REST Call
public class RunTestsUsingToolingAPI {
public static HttpResponse runAsynchronousTestsWithToolingAPI() {
String endpoint = '/services/data/v65.0/tooling/runTestsAsynchronous/';
// Set up the HTTP request
String urlString = endpoint;
String baseUrl = URL.getOrgDomainUrl().toExternalForm();
HttpRequest request = new HttpRequest();
request.setEndpoint(baseUrl + urlString);
request.setMethod('POST');
request.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
request.setHeader('Content-Type', 'application/json');
String requestBody = '{"tests": [{"className": "FlowTesting.<test>", "testMethods": [""]},{"className": "", "testMethods": [""]}]}';
request.setBody(requestBody);
// Send the request
try {
Http http = new Http();
HttpResponse response = http.send(request);
// Check for a successful response (status code 200)
if (response.getStatusCode() == 200) {
String result = response.getBody();
System.debug('Test run successfully queued. AsyncApexJob ID: ' + result);
} else {
System.debug('Error calling runTestsAsynchronous. Status code: ' + response.getStatusCode() + ' - ' + response.getStatus());
System.debug('Response body: ' + response.getBody());
}
return response;
} catch (Exception e) {
System.debug('Exception during Tooling API callout: ' + e.getMessage());
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment