# How do developer IDEs leverage the Tooling API to compile code, run tests, and inspect debug logs?

**Topic:** Integration · **Difficulty:** Medium · **Tags:** Integration, Tooling API, Developer Tooling

Read it on the site: https://www.forcechallenges.com/interview-questions/how-do-developer-ides-leverage-the-tooling-api-to-compile-code-run-tests-and-inspect-debug-logs

## Answer

**Basics:** The Metadata API is coarse-grained: you retrieve or deploy whole components. IDEs need small, fast, interactive operations such as "compile this one class and tell me the errors", "run these tests and stream results" or "show me the log for that run". The Tooling API fills that gap. Endpoint: `/services/data/vXX.0/tooling/`. The Developer Console is built on it, and so are the Apex Replay Debugger and test runs in VS Code (via the Salesforce CLI).

**How it works:**
1. Compile and save: tools create a `MetadataContainer`, add `ApexClassMember` / `ApexTriggerMember` records holding the new body, then post a `ContainerAsyncRequest`. Salesforce compiles, returns `CompilerErrors`, and optionally deploys (`IsCheckOnly` = true validates only).
2. Run anonymous Apex: `GET /tooling/executeAnonymous?anonymousBody=...`.
3. Tests: `runTestsAsynchronous` queues an `ApexTestQueueItem` per class; results land in `ApexTestResult`, coverage in `ApexCodeCoverage` and `ApexCodeCoverageAggregate`. `runTestsSynchronous` is for a single class.
4. Debugging: create a `DebugLevel` and a `TraceFlag` for a user, run the code, then query `ApexLog` and fetch `/sobjects/ApexLog/<id>/Body`.

**Advanced / deep dive:** Tooling objects are real sObjects, so you can use SOQL (`/tooling/query`), describe calls and DML-style create/update/delete, but many relationship queries are restricted. The CLI (`sf apex run test`, `sf apex get log`, `sf apex tail log`) wraps these calls; log tailing uses Streaming. Tooling API is not a deployment pipeline: production deployments should still go through Metadata API (Salesforce CLI `project deploy`) with test-level controls. Access requires the "API Enabled" and "Author Apex" permissions, and viewing other users' logs needs "View All Data" or "Manage Users".

**Code / config example:**
```bash
# Find the latest 5 debug logs, then download one body
curl -H "Authorization: Bearer $TOKEN" \
 "$INSTANCE/services/data/v62.0/tooling/query?q=SELECT+Id,Operation,LogLength+FROM+ApexLog+ORDER+BY+StartTime+DESC+LIMIT+5"
curl -H "Authorization: Bearer $TOKEN" \
 "$INSTANCE/services/data/v62.0/tooling/sobjects/ApexLog/07L.../Body"
```

**Hidden gotchas & must-remember points:**
- A `TraceFlag` has an expiration (maximum 24 hours); no active flag means no log is written.
- A single debug log is capped at 20 MB and is truncated in the middle; logs are kept for 24 hours. Excess total log volume can pause logging until you delete logs.
- Tooling API supports REST and SOAP; names and shapes of objects change per API version.
- Coverage numbers in `ApexCodeCoverageAggregate` reflect the latest runs only, not a historical record.
- Saving through the Tooling API in a production org is blocked; production changes need deployment.

**Best practice:** Use the Tooling API for inspect/run/debug tasks and Metadata API (or CLI deploy) for moving code between orgs.

---
Source: ForceChallenges · last updated 2026-11-02
