Sub topic 1
When Bulk API 2.0 is the right tool
Salesforce offers several ways to move records in and out of an org. They differ in how many records they handle per request, whether the call waits for the result, and how much work you do yourself. This chapter gives you a way to choose between them, and explains what Bulk API 2.0 is built to do.
Prerequisites
This chapter assumes you already have the following:
- The basic Salesforce data model: objects, fields, record IDs, and external ID fields.
- Familiarity with REST APIs and HTTP methods (GET, POST, PUT, PATCH).
- Comfort reading JSON and CSV.
- An OAuth access token for an org. Getting one is not covered here.
What Bulk API 2.0 is
Bulk API 2.0 is a REST API for asynchronous, high-volume data operations. "Asynchronous" means you submit work and Salesforce processes it in the background. Your request returns right away, and you check back later for the outcome.
You work with one object, a job, which has two forms:
- An ingest job writes records. Its operations are
insert,update,upsert,delete, andhardDelete. - A query job reads records with a SOQL query. Its operations are
queryandqueryAll.
You upload or download CSV data, and Salesforce splits the work into batches for you. You don't create or size batches yourself. Later chapters cover the job lifecycle in detail. For now, remember that one job covers the whole load, and you track its progress through job states.
The alternatives
You should know what else is available before choosing Bulk API 2.0.
| Option | Typical scale per call | Behavior | Best for |
|---|---|---|---|
| REST API, single record | 1 record | Synchronous | Interactive apps, one record at a time |
| Composite API | Up to 25 subrequests | Synchronous | Dependent operations in one round trip |
| sObject Collections | Up to 200 records | Synchronous | Small batches that need an immediate result |
| Bulk API 1.0 | Many batches, each up to 10,000 records | Asynchronous | Older integrations, XML or JSON payloads, manual control of batches |
| Bulk API 2.0 | Up to millions of records per job | Asynchronous | Large loads and exports from code or scripts |
| Data Import Wizard | Limited row count | UI-driven | Occasional manual imports by admins |
| Data Loader | Large | Desktop tool, uses the Bulk APIs or SOAP | Admins who prefer a GUI or command-line tool |
The Salesforce limits change between releases. The numbers above are the ones I'm confident of, and you should check the current Bulk API Developer Guide before you plan a big load.
Synchronous versus asynchronous
A synchronous call holds the connection open until Salesforce finishes. You get per-record results in the response, and you can react to errors immediately. The cost is that each call carries a small number of records, so loading a million rows takes thousands of requests.
An asynchronous call returns a job ID. You upload data once, close the upload, and poll until the job reaches a terminal state. A million rows takes a handful of requests, but you must handle polling and fetch results separately.
Bulk API 2.0 versus Bulk API 1.0
Both are asynchronous. The practical differences:
- Bulk API 2.0 accepts CSV only. Bulk API 1.0 also accepts XML and JSON.
- In 2.0 you upload one data stream per job and Salesforce does the batching. In 1.0 you create and size batches yourself.
- Bulk API 1.0 lets you choose serial or parallel processing. Bulk API 2.0 does not expose that choice, so you reduce lock contention by preparing your data well (see the performance chapter).
- Bulk API 2.0 has a simpler flow: create job, upload, close, poll, fetch results.
For new work, Bulk API 2.0 is usually the better default. Choose 1.0 only if you depend on a feature it has and 2.0 lacks, such as non-CSV payloads or explicit serial mode.
A decision rule
Volume is the main factor, but not the only one. Work through these questions in order.
- Do you need the result of each record inside the same request? If yes, use a synchronous API. Bulk API 2.0 cannot give you that.
- Is the operation a small number of records, roughly under a few thousand? A synchronous API such as sObject Collections is simpler, and the overhead of a job isn't worth it.
- Is the work thousands to millions of records, and can you wait minutes or hours? Use Bulk API 2.0.
- Is a person doing it once, with no code? Consider the Data Import Wizard or Data Loader.
The cutoff in question 2 is a guideline, not a hard limit. Below a few thousand records, a bulk job still works, but you gain little.
What Bulk API 2.0 handles well
- Initial data migration into a new org.
- Nightly or weekly synchronization with an external system, using
upsertwith an external ID. - Mass updates, such as reassigning ownership or backfilling a new field.
- Mass deletion, including
hardDelete, which bypasses the Recycle Bin and needs a specific permission. - Large exports through query jobs, including
queryAll, which also returns deleted and archived records.
What it does not do well
- Real-time user interactions. A job has startup and polling latency, so it suits throughput, not response time.
- Small, frequent writes. Many tiny jobs waste requests and can hit job-creation limits.
- Complex SOQL. Bulk queries don't support some features, such as
GROUP BY,OFFSET, andTYPEOF. For aggregates, use the standard Query API or compute them downstream. - Partial control over processing order. Because Salesforce batches the data, you cannot rely on a record being processed at a certain moment, only on the job finishing.
Common errors in choosing the API
- Using sObject Collections in a loop for 500,000 records. It works, but it makes thousands of calls and is slow and expensive on API request limits.
- Using Bulk API 2.0 for a 50-row update and then writing polling code you didn't need.
- Expecting an immediate error. A bad row doesn't fail the HTTP request. The row appears later in the failed results file.
- Assuming a finished job means every row succeeded. A job can reach
JobCompletewith some failed records. You always check the counts and the failed results.
A quick sizing check
A small helper can make the choice explicit in a script. This one uses the same guideline as the decision rule above.
def choose_api(record_count: int, needs_inline_results: bool) -> str:
"""Return a suggested Salesforce API for a write operation."""
if needs_inline_results:
return "synchronous API (REST, Composite, or sObject Collections)"
if record_count < 2000:
return "sObject Collections"
return "Bulk API 2.0"
if __name__ == "__main__":
for count, inline in [(50, False), (50, True), (750_000, False)]:
print(count, inline, "->", choose_api(count, inline))The 2,000 threshold is a placeholder. Tune it to your own latency needs and request limits.
Where this fits in the rest of the tutorial
The rest of the tutorial follows one job from start to finish. You will see how to authenticate and find the job endpoints, how job states work, how to prepare a CSV, and how to create, upload, close, monitor, and read results. After that come upserts, error handling, bulk queries, limits, and performance practices. Every step assumes you chose Bulk API 2.0 for the reasons above.
Exercise
A nightly process must update 800,000 Account records from an external system, matching on an external ID field, and nobody needs results during the call. Which API fits, and which job operation do you use?
Show answer
Bulk API 2.0 with an ingest job using the upsert operation and the external ID field. The volume is large, the work is asynchronous, and you can read per-record results afterwards from the results files.
Exercise
Your code sends 40 contact updates from a web form and must show the user which ones failed right away. Should you use Bulk API 2.0? Why or why not?
Show answer
No. The volume is small and you need results in the same request. A synchronous API such as sObject Collections returns per-record results in the response. Bulk API 2.0 would add job overhead and give you results only after polling.
Exercise
A Bulk API 2.0 job reaches the state JobComplete. Does that prove every record was saved? What do you check next?
Show answer
No. JobComplete means Salesforce finished processing, not that every row succeeded. Check the processed and failed record counts on the job, then read the failed results (and unprocessed records if any) to see what needs fixing and resubmitting.