Sub topic 4
REST API: the default choice for record access
This chapter assumes you can already get an access token (see the OAuth chapter), you know HTTP verbs and JSON, and you know Salesforce objects, fields, and records. The examples use curl against a My Domain URL.
Why REST is the default
The REST API exposes Salesforce records as resources under /services/data/vXX.X/. It uses standard HTTP verbs, returns JSON, and works from any language. For most integrations that read or write a modest number of records, it is the simplest option and the first one to try. Move to another API only when volume, latency, or the kind of data points elsewhere.
| Task | HTTP call | Resource |
|---|---|---|
| Run a SOQL query | GET | `/query?q=...` |
| Create a record | POST | `/sobjects/Account` |
| Read a record | GET | `/sobjects/Account/{Id}` |
| Update a record | PATCH | `/sobjects/Account/{Id}` |
| Delete a record | DELETE | `/sobjects/Account/{Id}` |
Querying records
Send a SOQL statement in the q parameter. Let curl encode it for you with --data-urlencode, because the query contains spaces and quotes.
curl -G "https://MyDomain.my.salesforce.com/services/data/v60.0/query" \
--data-urlencode "q=SELECT Id, Name FROM Account WHERE Industry = 'Technology' LIMIT 5" \
-H "Authorization: Bearer $ACCESS_TOKEN"The response contains totalSize, done, and a records array. If done is false, the response also includes a nextRecordsUrl. Call that URL to get the next page. Do not build the paging URL yourself.
Creating a record
POST a JSON body to the object's resource. Field names are API names, so custom fields end in __c.
curl -X POST "https://MyDomain.my.salesforce.com/services/data/v60.0/sobjects/Account" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"Name": "Acme Corp", "Industry": "Technology"}'A successful call returns HTTP 201 with a body like {"id": "001...", "success": true, "errors": []}. Keep that id. You need it for later reads, updates, and deletes.
Updating and deleting
Update with PATCH and send only the fields that change. A successful update returns 204 with no body. Delete with DELETE on the record URL, which also returns 204. To insert or update by an external system's key, use PATCH on /sobjects/Account/ExternalId__c/value. This is an upsert, and it avoids a lookup before every write.
A typical request lifecycle
Common errors
- 401 INVALIDSESSIONID: the token is expired or belongs to a different org or domain. Request a new token and check the host name.
- 400 MALFORMED_QUERY: the SOQL is invalid, often from an unescaped quote or a misspelled field.
- 400 REQUIREDFIELDMISSING: a required field is absent from the body.
- 404 NOT_FOUND: the record Id or the API version in the URL is wrong.
- 403 REQUESTLIMITEXCEEDED: the org has used its API allocation. The limits chapter covers this.
Where REST stops being the best fit
Each REST call is one HTTP request that counts against your limits. Doing thousands of single-record calls is slow and costly. Use Composite resources to combine related calls, and use Bulk API 2.0 for large volumes. Both are covered in the next chapters.
Exercise
Your integration upserts customers from a billing system that has its own customer number. Which REST call avoids a query before each write, and what status code does a successful update return?
Show answer
Use PATCH on /sobjects/Account/ExternalId_c/value, where ExternalId_c is an external ID field on Account. Salesforce creates the record if the key is new and updates it if it exists. An update returns 204 with no body, and a create returns 201.
Exercise
A query returns "done": false. What should the client do next?
Show answer
Request the URL in nextRecordsUrl, and repeat until done is true. Otherwise the client sees only the first page of records.