Sub topic 2
Authentication and endpoint structure
This chapter assumes you know basic HTTP methods and JSON, and that you already have an OAuth access token for your org. Getting that token is not covered here. It also assumes you know what the earlier chapter said about when Bulk API 2.0 is the right tool.
What every request needs
Bulk API 2.0 is a REST API. Each call needs two things:
- The instance URL of your org, such as
https://yourdomain.my.salesforce.com. The OAuth response returns it asinstance_url. - The access token, sent in an
Authorization: Bearerheader.
The examples in this tutorial read both from shell variables.
export INSTANCE_URL="https://yourdomain.my.salesforce.com"
export ACCESS_TOKEN="00D...your-token"Never hard-code a token in a script or commit it to source control. Read it from the environment or a secrets store.
The URL pattern
All Bulk API 2.0 endpoints start with the same prefix:
{instance_url}/services/data/vXX.X/jobs/vXX.X is the API version, for example v62.0. Use a current version, and keep the same version across all calls in one job.
The next path segment says what kind of job you are working with.
| Path | Purpose |
|---|---|
| `/jobs/ingest` | Create an ingest job (insert, update, upsert, delete) |
| `/jobs/ingest/{jobId}` | Get job status, or change its state |
| `/jobs/ingest/{jobId}/batches` | Upload CSV data |
| `/jobs/ingest/{jobId}/successfulResults/` | Rows that were saved |
| `/jobs/ingest/{jobId}/failedResults/` | Rows that failed |
| `/jobs/ingest/{jobId}/unprocessedrecords/` | Rows never attempted |
| `/jobs/query` | Create a bulk query job |
| `/jobs/query/{jobId}/results` | Download query results |
The job ID in the path ties every later call to the job you created.
Which method does what
The same URL can behave differently depending on the HTTP method.
POSTcreates a job.PUTuploads data to a job.PATCHchanges a job's state, for example to close or abort it.GETreads status or results.DELETEremoves a finished job.
Headers to get right
Two headers cause most early errors.
Content-Type: application/jsonfor calls that send JSON, such as creating a job or changing its state.Content-Type: text/csvfor the data upload.
A wrong content type on the upload typically returns an error such as 415 Unsupported Media Type.
Common authentication errors
- 401 Unauthorized. The token is expired or invalid. Request a new token, or refresh it, then retry.
- 403 Forbidden. The user lacks permission. The running user needs API access and permission on the object, such as create access for inserts.
- 404 Not Found. The path or job ID is wrong, or the instance URL points to a different org than the one that created the job.
A long job can outlast an access token. Your monitoring code should handle a 401 by refreshing the token and repeating the request. The job keeps running in Salesforce either way.
Exercise
You created an ingest job and now want to close it so Salesforce starts processing. Which HTTP method and URL do you use, and what content type does the body have?
Show answer
Use PATCH on /services/data/vXX.X/jobs/ingest/{jobId} with Content-Type application/json and the body {"state":"UploadComplete"}.