Sub topic 15
Choosing the right API: a decision framework
This chapter assumes you have read the earlier chapters on REST, Composite, Bulk API 2.0, Apex REST, SOAP, GraphQL, UI API, the event APIs, Metadata API and Tooling API. It also assumes you can authenticate with OAuth 2.0 and know the basic API limits. Here you turn those pieces into a repeatable way to choose.
Ask four questions
Most choices are settled by four questions, asked in this order.
- What kind of data is involved? Records, configuration, or events?
- How much data moves per operation? A few records, thousands, or millions?
- What is the call pattern? Request and response, scheduled batch, or push on change?
- Who is the client? A server integration, a browser or mobile UI, or a developer tool?
Step 1: Data, metadata, or events
Salesforce keeps three kinds of things behind its APIs, and each has its own APIs.
- Record data (Accounts, Contacts, custom objects): REST, SOAP, GraphQL, Bulk API 2.0, Apex REST.
- Configuration (fields, objects, flows, Apex classes): Metadata API to deploy it, Tooling API for development tools.
- Change notifications: Platform Events, Change Data Capture (CDC), Pub/Sub API.
If you are moving configuration between orgs, stop here. Use Metadata API. Do not try to model it as record data.
Step 2: Volume and latency
For record data, volume decides a lot.
| Need | Best fit | Why |
|---|---|---|
| One record, or a few, with an immediate answer | REST API | Simple, synchronous, easy to debug |
| Several dependent operations in one call | Composite resource | Fewer round trips, optional rollback with `allOrNone` |
| Tens of thousands to millions of records | Bulk API 2.0 | Asynchronous, processed in the background, saves API calls |
| Flexible reads across related objects | GraphQL API | One request returns exactly the fields you ask for |
| Logic that must run inside Salesforce | Apex REST | Your own endpoint with your own rules |
| React to changes as they happen | CDC or Platform Events through Pub/Sub API | Push instead of polling |
A common rule of thumb: once a job touches more than a few thousand records, move from REST to Bulk API 2.0.
Step 3: Client and legacy constraints
- A custom user interface that must respect layouts and field-level security: UI API or Connect REST API.
- An existing enterprise tool that only speaks SOAP, or needs a strongly typed contract (WSDL): SOAP API.
- A developer tool that reads or edits Apex, triggers, or debug logs: Tooling API.
Putting it together
The diagram shows the decision order as a flowchart.
Common mistakes
- Polling instead of subscribing. Calling a query every few seconds to detect changes burns your daily API allocation. Use CDC.
- Looping single REST calls over a large file. Ten thousand inserts as ten thousand calls will hit limits. Use Bulk API 2.0 or Composite.
- Writing Apex REST when REST already works. Custom endpoints add code to maintain. Use them only when standard resources cannot express the logic.
- Ignoring limits at design time. Estimate calls per day before you pick, not after the first
REQUEST_LIMIT_EXCEEDEDerror.
Exercise
A nightly job must load 2 million Contact rows from a CSV file, and a separate system must be told within seconds whenever an Opportunity changes. Which APIs do you pick for each requirement, and why?
Show answer
Use Bulk API 2.0 for the nightly load, because it is asynchronous and built for millions of records without using one API call per record. Use Change Data Capture, consumed through Pub/Sub API, for the Opportunity changes, because it pushes change events in near real time and avoids polling.