Sub topic 5
Creating an ingest job
This chapter covers the first call in a bulk load: creating the ingest job. It assumes you have an OAuth access token and your org's instance URL, and that you know how to send HTTP requests with JSON bodies. It also assumes you have read the previous chapters on the job model and on preparing your CSV.
What an ingest job is
An ingest job is a container that Salesforce creates to hold one load operation against one object. You describe the operation when you create the job. You then upload data to it and close it. Creating the job does not send any records.
The job is created with a POST request to the jobs/ingest resource. The response contains the job id, which every later call needs.
The create request
Send a JSON body that describes the load.
curl -X POST "$INSTANCE_URL/services/data/v62.0/jobs/ingest" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"object":"Contact","operation":"insert","contentType":"CSV","lineEnding":"LF"}'Use a current API version in place of v62.0. The body fields are:
| Field | Meaning |
|---|---|
| `object` | The API name of the object to load, such as `Contact`. |
| `operation` | One of `insert`, `update`, `upsert`, `delete` or `hardDelete`. |
| `contentType` | The format of the uploaded data. Use `CSV`. |
| `lineEnding` | `LF` or `CRLF`. It must match the line endings in your file. |
The object and operation fields are required. A job handles one object and one operation, so a load that touches Accounts and Contacts needs two jobs.
An upsert job also needs an externalIdFieldName field. A later chapter covers it.
Reading the response
A successful call returns the new job as JSON. The two values to look at first are id and state.
{
"id": "750xx0000000001AAA",
"operation": "insert",
"object": "Contact",
"state": "Open",
"contentType": "CSV",
"lineEnding": "LF"
}The state is Open, which means the job is waiting for data. Save the id in a variable such as JOB_ID. The upload and close calls use it in their URLs.
Common errors
Most failures at this step come from a few causes.
- Mismatched line endings. If you set
lineEndingtoLFbut the file usesCRLF, rows can be misread later, during processing. Create the job with the value that matches your file. - Wrong object or operation. A misspelled object name returns an error right away. The CSV header is not checked until processing, so a wrong object name can look like a bad header.
- Missing or expired token. An HTTP 401 response means the access token is invalid or has expired. Get a new token and retry.
- Bulk API access not allowed. An HTTP 403 response can mean the user lacks API access or permission on the object.
The same call in Python
Scripts should fail fast if the create call fails.
import os
import requests
instance = os.environ["SF_INSTANCE_URL"]
token = os.environ["SF_ACCESS_TOKEN"]
base = f"{instance}/services/data/v62.0/jobs/ingest"
resp = requests.post(
base,
headers={"Authorization": f"Bearer {token}"},
json={"object": "Contact", "operation": "insert", "contentType": "CSV", "lineEnding": "LF"},
)
resp.raise_for_status()
job = resp.json()
print(job["id"], job["state"])The json= argument sets the Content-Type: application/json header for you.
Exercise
You need to load new Accounts and new Contacts from two CSV files. How many ingest jobs do you create, and what is the state of each one right after creation?
Show answer
Two jobs, one for Account and one for Contact, because a job covers a single object and a single operation. Each job is in the Open state right after creation and waits for its data.