Sub topic 15
Choosing the right API: a decision framework
This chapter pulls the earlier chapters together into one method for picking a Salesforce API. It assumes you have read the chapters on authentication, SOQL, the REST API, Composite resources, Bulk API 2.0, API limits, and the event APIs. It also assumes you can read simple HTTP calls and JSON.
Why a framework helps
Salesforce offers more than ten APIs, and several of them can do the same job. REST, SOAP, and Bulk API 2.0 can all insert records. REST, GraphQL, and UI API can all read a record. If you choose by habit, you end up with integrations that hit limits, run slowly, or are hard to maintain.
A framework turns the choice into a short list of questions. Each question removes some options. After a few questions, usually one API is left.
The questions are about your requirement, not about the API:
- What kind of thing are you working with: data, configuration, or development artifacts?
- Which direction does the information flow, and who starts it?
- How many records are involved per operation?
- How fast must the result arrive?
- Does the work need custom server-side logic?
- What does the client already support?
Step 1: Data, metadata, or development artifacts
Start by separating the kind of thing you touch.
- Records such as Accounts, Contacts, and custom object rows are data. Use a data API.
- Object definitions, fields, flows, page layouts, and permission sets are metadata. Use Metadata API to deploy or retrieve them in bulk.
- Apex classes, triggers, debug logs, and code coverage are development artifacts. Use Tooling API.
Do not try to manage configuration through a data API. If your task is "copy these custom fields from sandbox to production," that is a Metadata API job, usually run through Salesforce CLI or a deployment tool rather than hand-written calls. If your task is "build an editor plugin that shows Apex errors," that is Tooling API.
Everything after this step assumes the answer was data.
Step 2: Direction and initiator
Ask who starts the exchange.
- Your client asks Salesforce for something. This is request/response. Candidates are REST, SOAP, GraphQL, Bulk API 2.0, UI API, and Apex REST.
- Salesforce tells your client that something happened. This is event-driven. Candidates are Platform Events, Change Data Capture (CDC), and Pub/Sub API.
If you find yourself polling every few seconds to ask "did anything change?", that is a sign you want the event path. Polling spends API calls on empty answers. A subscription spends none while nothing changes.
| Need | Mechanism | Why |
|---|---|---|
| Mirror record changes into another system | Change Data Capture over Pub/Sub API | CDC publishes create, update, delete, and undelete events for you |
| Announce a business event that is not a record change | Platform Events | You define the event schema and publish it yourself |
| Subscribe from an external service | Pub/Sub API | gRPC-based, with replay support so a client can resume after a gap |
| Check a status once, on demand | REST | A single call is simpler than a subscription |
Step 3: Volume per operation
For request/response work, record count is the strongest signal.
- Up to a few hundred records, interactive or near real time: REST API. Use Composite or Batch resources to cut round trips.
- Thousands to millions of records, no one waiting: Bulk API 2.0. It is asynchronous. You create a job, upload data, and check the job state later.
- Somewhere in between: compare the call counts. Loading 5,000 records through REST with a Composite request of up to 25 subrequests would take about 200 calls. A single Bulk API 2.0 job would take a few calls and count far less against your daily request allocation.
A common rule of thumb is that Bulk API 2.0 starts to pay off once you reach the low thousands of records. Treat that number as a starting point and test it. Bulk jobs have setup overhead and are processed in the background, so a small job can finish more slowly than the same work done through REST.
Check the current limits for your edition before you commit to a number. They change between releases.
Step 4: Latency and consistency
Ask how quickly the caller needs an answer.
- A user is waiting on a screen, or another system needs the result in the same transaction: use a synchronous API. REST, SOAP, GraphQL, and Apex REST all return the result in the response.
- Nobody is waiting, and throughput matters more than speed per record: use Bulk API 2.0.
- You need to react within seconds of a change but not hold a connection open per request: use events.
Also think about transactions. A Composite request with allOrNone set to true rolls back every subrequest if one fails. Bulk API 2.0 does not offer that. It processes records in batches, and some can succeed while others fail. You must read the failed-results file afterward. If your business rule says "all or nothing," that pushes you toward Composite or Apex REST for small sets.
Step 5: Custom logic
Standard REST resources expose records and queries. They do not know your business rules. If an integration would otherwise need three or four chained calls plus client-side logic, consider an Apex REST endpoint.
Apex REST suits cases where:
- one call should run validation, create several related records, and return a custom response shape;
- you want the logic to live in Salesforce and be shared by several clients;
- you need to enforce rules that are not expressible as field validation.
The cost is that you now own code. It needs tests, deployment, and version management, and it runs under Apex governor limits. Do not write an Apex REST endpoint if a Composite request would do the same job.
Step 6: Client constraints and read shape
Last, look at the client and the shape of the read.
- SOAP API fits when the client generates code from a WSDL, as many older enterprise tools do, or when an existing integration already uses it. For new work with no such constraint, choose REST.
- GraphQL API fits when a client needs fields from several related objects in one request and wants to name exactly the fields it needs. It is for reads, and it suits front ends that would otherwise make many REST calls.
- UI API and Connect REST API fit when you are building a user interface and want responses that respect layouts, field-level security, and localization. UI API returns data together with the metadata needed to render it.
The decision flow
The diagram below combines the steps into one path.
Applying the framework to scenarios
Scenario 1: Nightly load of 2 million order lines
The data is records. The client initiates. The volume is large and nobody waits. The answer is Bulk API 2.0 with an ingest job, as in the three-step lifecycle you saw earlier: create the job, upload CSV, and set the state to UploadComplete. After the job finishes, download the failed-results file and decide what to retry.
Scenario 2: A web portal creating an account and its first contact
The data is records, volume is two, and a person is waiting. Two related records must succeed or fail together. Use a Composite request with allOrNone set to true and a reference such as @{newAccount.id} to link the contact. Apex REST is a second option if you also need server-side rules.
Scenario 3: Keep a data warehouse in sync with Opportunities
Polling with SOQL on LastModifiedDate works at first. It misses deletes unless you also query deleted records, and it uses calls even when nothing changed. Enable Change Data Capture for Opportunity and subscribe through Pub/Sub API. Plan for the case where your subscriber is offline: use the replay ID to resume, and know that event retention is limited. If the gap is longer than the retention window, run a one-time Bulk API 2.0 query to resynchronize.
Scenario 4: A mobile app needing an account with its contacts and recent cases
Several related objects, selected fields, one round trip. GraphQL API is a good fit. If the app is built to render Salesforce layouts, UI API is the alternative.
Scenario 5: Moving a new custom object and its fields to production
This is configuration. Use Metadata API through a deployment tool.
Common mistakes
- Using REST for a bulk load. It works for a while, then exhausts the daily request allocation or runs for hours. Switch to Bulk API 2.0.
- Using Bulk API 2.0 for a user-facing action. The job is asynchronous. The user waits for background processing and gets no immediate result.
- Polling instead of subscribing. This burns calls and adds delay. Use CDC or Platform Events.
- Writing Apex REST by default. Custom endpoints add code to maintain. Use standard resources first.
- Choosing SOAP for new work without a reason. Unless a WSDL-driven client requires it, REST is simpler and more widely supported.
- Ignoring limits during design. Estimate calls per hour and per day before building. Add retry with backoff for rate-limit and temporary server errors.
- Forgetting that an API choice is not permanent. One integration can use several APIs. A common pattern is Bulk API 2.0 for the initial load, CDC for ongoing changes, and REST for occasional lookups.
Summary table
| Requirement | Best fit | Main reason |
|---|---|---|
| Deploy fields, flows, or layouts | Metadata API | Works on configuration, not records |
| Manage Apex or read debug logs from a tool | Tooling API | Built for development tools |
| Millions of records, batch window | Bulk API 2.0 | Asynchronous and efficient on call count |
| Few records, interactive | REST API | Simple, synchronous, widely supported |
| Several dependent writes in one call | Composite | Fewer round trips, optional rollback |
| Custom validation and orchestration | Apex REST | Logic stays in Salesforce |
| Related reads with chosen fields | GraphQL API | One request, exact field selection |
| Rendering a Salesforce-style UI | UI API, Connect REST API | Respects layouts and security metadata |
| React to record changes | CDC with Pub/Sub API | Push instead of polling |
| React to a custom business event | Platform Events | You define the event |
| WSDL-driven legacy client | SOAP API | Strongly typed contract |
Exercise
A nightly job must update 800,000 Contact records from an external system. A teammate proposes a loop of single-record PATCH calls through the REST API. Which API would you choose instead, and give two reasons.
Show answer
Bulk API 2.0. First, it is asynchronous and built for large volumes, so you upload the data as CSV in one job instead of making 800,000 calls. Second, it uses far fewer API calls against your allocation, which leaves room for other integrations. Nobody is waiting on the result, so asynchronous processing is acceptable. Read the failed-results file afterward to retry rows that did not succeed.
Exercise
A support portal must create a Case and attach two related CaseComment records in one action. If any record fails, none may be saved. The user is waiting. Which API and which setting?
Show answer
Use the REST API Composite resource with allOrNone set to true, and reference the new Case Id from the later subrequests (for example with @{newCase.id}). The call is synchronous, so the user gets an immediate answer, and allOrNone rolls everything back on any failure. Apex REST is a valid alternative if extra server-side logic is needed.
Exercise
An external billing system must learn within seconds when an Account's BillingAddress changes. Its team currently polls every 30 seconds. What would you recommend, and what failure case must the design handle?
Show answer
Enable Change Data Capture for Account and have the billing system subscribe through Pub/Sub API, so changes are pushed instead of polled. The design must handle subscriber downtime: store the last replay ID and resume from it. Because event retention is limited, also plan a fallback, such as a Bulk API 2.0 or REST query to resynchronize, if the outage exceeds the retention window.