Sub topic 1
What the Salesforce REST API is and when to use it
Prerequisites
This chapter assumes you already know the following:
- Basic HTTP: methods such as GET and POST, headers, and status codes.
- How to read and write JSON.
- What Salesforce objects, fields, and record IDs are, for example that an Account has a Name field and a 15- or 18-character ID.
- How to run
curlor a short Python script.
You also need a Salesforce org, such as a free Developer Edition org. Later chapters need an access token, and the authentication chapter shows how to get one. This chapter only reads a public endpoint, so you can follow it without a token.
What the REST API is
The Salesforce REST API is a set of HTTP endpoints for working with the data and metadata in an org. Each kind of resource has a URL, and you act on it with a standard HTTP method. A request to create an Account is a POST to the Account resource. A request to read one is a GET on that record's URL.
Some features you will rely on throughout this tutorial:
- Resource-oriented URLs. Records, queries, and object descriptions each have their own path under a versioned base URL.
- JSON by default. Request and response bodies are JSON. XML is also supported, but this tutorial uses JSON only.
- Standard HTTP semantics. Methods and status codes carry meaning. A successful create returns
201, and a successful update returns204. - No special client library. Anything that can send an HTTPS request can call it. That includes
curl, Python'srequests, a browser, and a mobile app. - Same data model as the platform. Standard and custom objects, field-level security, sharing rules, validation rules, and Apex triggers all apply to API calls.
The last point matters in practice. The API is not a back door around your org's rules. A record created over REST goes through the same validation and automation as one created in the Salesforce user interface. A request can fail because of a validation rule or a missing permission, and you will see those failures in the error chapter.
How a request is shaped
Every REST call has the same basic structure. The following diagram shows the parts. Later chapters cover each part in depth.
A call combines four pieces of information:
- The instance URL. This is the host for your org, such as
https://MyDomain.my.salesforce.com. - The version and path. These look like
/services/data/v60.0/sobjects/Account. - The method.
POST,GET,PATCH, and so on. - The credentials. An OAuth 2.0 access token in an
Authorization: Bearerheader.
A first request
Before you set up authentication, try one request that needs no token. The root of the data service lists the API versions the org supports.
export SF_INSTANCE_URL="https://MyDomain.my.salesforce.com"
curl "$SF_INSTANCE_URL/services/data/"The response is a JSON array. Each element describes one version, with a label, a url, and a version field. The last element is the newest version your org supports. Use the version value, for example 60.0, to build the versioned base URL, which here is /services/data/v60.0.
This first call shows two things about the API. The version is part of the URL, and you choose it. A response from v60.0 today will keep the same shape for that version later, because Salesforce keeps existing versions stable. Pin the version in your code instead of assuming the newest one.
Most other resources require authentication. If you call one without a token, the API returns 401 with an error body. You will see this body in the error chapter.
When to use the REST API
Salesforce offers several APIs. The REST API is a good default for these jobs:
- Interactive and low-volume integrations. A web or mobile app that creates a lead when a form is submitted, or looks up an account when a user opens a page.
- Record-level work. Creating, reading, updating, and deleting individual records and checking the result immediately.
- Lightweight clients. Scripts, serverless functions, and tools where adding a SOAP library or generated client code would be heavy.
- Exploration and testing. You can try a call in
curlin seconds and read the response by eye.
It is not the best fit for every case. The next table compares it with the nearby options.
| API | Style | Best for | Trade-off |
|---|---|---|---|
| REST API | Synchronous, JSON over HTTP | Record-level CRUD, queries, small to moderate volumes | One call per record unless you use Composite resources |
| Composite resources | Synchronous, JSON over HTTP | Several operations in one round trip, or up to 200 records per call with Composite sObjects | Per-call caps on records and subrequests |
| Bulk API 2.0 | Asynchronous jobs, CSV data | Loading, updating, or exporting very large data sets | You submit a job and poll for results, so it is not immediate |
| SOAP API | Synchronous, XML with a WSDL | Existing enterprise tools that expect a typed contract | More verbose, and needs SOAP tooling |
| Apex REST | Custom endpoints you write in Apex | Business logic that should run inside Salesforce behind one call | You write and maintain the code |
| Pub/Sub API | Event streaming | Reacting to changes as they happen | A different programming model, not request and response |
A rough rule is to start with REST. Move to Composite resources when you notice many sequential calls that could be grouped. Move to Bulk API 2.0 when the job is a data load or export that is large enough that you would not wait on it interactively.
Limits that shape the design
Every call counts against your org's API request allocation, which is measured over a rolling 24-hour period. The size of that allocation depends on your edition and the number of user licenses. Salesforce also restricts concurrent long-running requests. Two consequences follow:
- A loop that makes one request per record can use a large part of your allocation quickly.
- A script that works against a small Developer Edition org can fail against a production org with heavy traffic.
The limits resource reports your current usage. It needs authentication, so it fits once you have a token.
curl "$SF_INSTANCE_URL/services/data/$SF_API_VERSION/limits" \
-H "Authorization: Bearer $SF_ACCESS_TOKEN"In the JSON response, look at the DailyApiRequests entry. It has a Max value and a Remaining value. Checking it before a large job is a cheap way to avoid hitting a limit mid-run. The final chapter of this tutorial covers limits and best practices in full.
What the rest of this tutorial covers
This chapter placed the REST API among the other options. The remaining chapters build up the skills for a full create, read, and update cycle:
- How OAuth 2.0 access tokens authenticate requests.
- How to build resource URLs from the instance URL, version, and sObject name.
- How objects, fields, and record IDs map to JSON.
- Which HTTP method and status code goes with each operation.
- Creating records with
POST, reading them withGET, and updating them withPATCH. - Reading error responses and handling common failures.
- Finding records with SOQL, inspecting objects with describe, upserting by external ID, and working with many records through Composite sObjects.
- API limits and practical best practices.
Common misunderstandings
- "REST bypasses my validation rules." It does not. API writes run the same validation, flows, and triggers as the UI. A call can succeed in the UI and fail over REST if the API user lacks permission to a field.
- "I can use any URL I find in the docs." The host must be your org's instance URL or My Domain. A generic host will not know which org you mean.
- "The newest version is always the best one." Newer versions add resources and fields. A version you have tested is safer for production code than whatever is newest.
- "The 200 status code means success." Success codes vary by operation. A create returns
201and an update returns204, so code that checks for exactly200will treat valid responses as failures. The method and status chapter covers this.
Exercise
Your nightly job must load 500,000 new Contact records from a CSV file, and nobody needs the results immediately. Is the plain REST API with one POST per record a good choice? What would you choose instead, and why?
Show answer
One POST per record is a poor choice. It would use 500,000 requests from the daily API allocation and would be slow because each call is a separate round trip. Bulk API 2.0 fits better. It accepts CSV data in an asynchronous job, so you submit the file and poll for results, which suits a nightly load where no immediate response is needed. If the data were only a few hundred records and you wanted immediate results, Composite sObjects at up to 200 records per call would also work.
Exercise
You run curl "$SF_INSTANCE_URL/services/data/" and get a JSON array of versions. Which part of that response do you use when you build later resource URLs, and why should you pin it in your code?
Show answer
You use the version value of the entry you choose, for example 60.0, to build the /services/data/v60.0 base path. Pin it because a specific version keeps its behavior stable, while the newest version can change as Salesforce releases new ones. A pinned version means your integration behaves the same until you decide to upgrade and test.