Sub topic 14
Tooling API for development tools
This chapter assumes you can call the REST API with a bearer token, write basic SOQL, and read simple Apex. It covers what the Tooling API is, how it differs from the Metadata API, and the mistakes people commonly make with it.
What the Tooling API is
The Tooling API exposes Salesforce development artifacts as queryable objects. These include Apex classes, triggers, Lightning components, debug logs, and code coverage results. It uses the same authentication and request style as the REST API, but its endpoints sit under /services/data/v60.0/tooling/.
Use it when you build tools that work on code and configuration in a fine-grained way. Examples are IDE extensions, code analyzers, and scripts that fetch debug logs.
Tooling API and Metadata API compared
Both APIs touch configuration, but they suit different jobs.
| Need | Better fit |
|---|---|
| Deploy or retrieve many components as a package | Metadata API |
| Read or edit one Apex class or trigger | Tooling API |
| Query code coverage or debug logs | Tooling API |
| Move configuration between orgs | Metadata API |
The Metadata API works on whole components in bulk. The Tooling API lets you query and change individual pieces with SOQL.
Querying Apex classes
The Tooling API supports SOQL through its own query resource. This command lists Apex classes in a namespace-free org.
curl -G "https://MyDomain.my.salesforce.com/services/data/v60.0/tooling/query" \
--data-urlencode "q=SELECT Id, Name, ApiVersion FROM ApexClass WHERE NamespacePrefix = null LIMIT 10" \
-H "Authorization: Bearer $ACCESS_TOKEN"The response has the same shape as a REST query: totalSize, done, and a records array. Each record also carries an attributes block with its type and URL.
Checking code coverage
Coverage data lives in the ApexCodeCoverageAggregate object. This query returns the lines covered and uncovered for each class or trigger.
curl -G "https://MyDomain.my.salesforce.com/services/data/v60.0/tooling/query" \
--data-urlencode "q=SELECT ApexClassOrTrigger.Name, NumLinesCovered, NumLinesUncovered FROM ApexCodeCoverageAggregate" \
-H "Authorization: Bearer $ACCESS_TOKEN"A tool can divide NumLinesCovered by the total of both fields to compute a percentage. Salesforce requires 75% overall coverage to deploy Apex to production.
Creating and editing code
You can also write to these objects. Executing anonymous Apex is one example: a GET request to the executeAnonymous resource runs a snippet and reports compile and run results.
curl -G "https://MyDomain.my.salesforce.com/services/data/v60.0/tooling/executeAnonymous" \
--data-urlencode "anonymousBody=System.debug('hello');" \
-H "Authorization: Bearer $ACCESS_TOKEN"The response contains compiled, success, and error fields. Fetch the debug log separately if you need the output.
Common errors
- Using the wrong path. Calling
/services/data/v60.0/queryforApexClassfails, because that object is only available under/tooling/. - Expecting deployment behavior. Changing a class through the Tooling API does not give you the package-level validation and ordering that a Metadata API deployment does. Use the Metadata API to move changes between orgs.
- Treating it as a data API. It is not meant for business records. Use the REST API or Bulk API 2.0 for those.
- Editing production code. Most orgs block direct code edits in production. Run these changes in a sandbox or scratch org.
- Missing permissions. Many Tooling objects need permissions such as "Author Apex" or "View All Data". An authenticated user can still receive
INSUFFICIENT_ACCESSerrors.
Exercise
Exercise
A team is building a VS Code extension that shows each Apex class's test coverage next to its name. Which API should it use, and which object should it query?
Show answer
Use the Tooling API and query ApexCodeCoverageAggregate. It returns NumLinesCovered and NumLinesUncovered per class or trigger, and the extension can compute the percentage from them. The Metadata API is for moving whole components between orgs, and the REST API does not expose this object.
Exercise
Why does GET /services/data/v60.0/query?q=SELECT Id FROM ApexClass fail, and how do you fix it?
Show answer
ApexClass is a development object that is only reachable through the Tooling API. Change the path to /services/data/v60.0/tooling/query.