Submit a batch
Create a Batchrouter batch with POST /v1/batches — inline items or an uploaded JSONL manifest, a quote_id, an Idempotency-Key, routing, and privacy.
Not open yet
Creating a batch submits your work for asynchronous processing and reserves credits against a quote. You send POST /v1/batches with either inline items or a reference to an uploaded file, accept a quote_id, and pass an Idempotency-Key so retries are safe.
This is the deep version of step 3 of the quickstart. It assumes you already have an API key (see Authentication) and a quote (see Quotes and pricing).
Before you start
You need two things in hand:
- An API key, sent as
Authorization: Bearer br_live_…. Keep it server-side. - A
quote_id(qlock_…) fromPOST /v1/quotes/model. The quote is free, pins your price estimate, and is required to reserve credits before dispatch.
Quotes are free; credits are charged at batch creation. If your workspace lacks credits you'll get a 402 — check GET /v1/auth/account and top up credits in the dashboard at https://app.batchrouter.com/billing.
Choose your input: inline items or a file
A batch's work comes from exactly one of two sources — they are mutually exclusive:
items— an inline array of batch items. Best for small-to-medium batches you assemble in memory.input_file_id— afile_…id from a JSONL file you uploaded withPOST /v1/filesandX-BatchRouter-Purpose: batch_input. Best for large batches: inlineitemsare capped at 5,000 per request, a file is not. The file is either Batchrouter Sectioned v1 or standard OpenAI Batch lines; see JSONL format for both shapes and the file-based example below. A file uploaded without that purpose is rejected here with400 invalid_input_file.
Send one or the other. Sending both — or neither — fails preflight with 400 and error.details.preflight. Which one to send is a size question; see Large batches.
The canonical item used across these docs:
{"customer_item_id":"item-1","operation": "chat.completions","model":"gpt-5.4-mini","input":{"messages":[{"role":"user","content":"Summarize: Batchrouter routes batch-AI workloads across providers."}]}}gpt-5.4-mini is a model id from the live catalog. List the models available to you now, with per-provider pricing and capacity, via GET /v1/catalog/models.
Large batches
The rule for how to send a job depends only on its size:
| Your job | Send it as | Why |
|---|---|---|
| Up to 5,000 items and a request body up to 20 MiB | Inline items in one POST /v1/batches | One request, one JSON body, nothing to upload. |
| More than that | Upload the JSONL to POST /v1/files with purpose batch_input, then create with input_file_id | The file is checked and streamed to storage, and its requests are inserted in pages. |
There is no item cap on an uploaded manifest; the 5,000-item cap applies to inline items only. The browser dashboard follows the same rule, so an API customer is never at a disadvantage, and an official SDK that makes the inline-versus-upload choice for you is being built.
The one ceiling every request meets is the per-request body size that Cloudflare enforces at the edge, before Batchrouter sees the request. A very large single upload can be refused there with a 413 that is not a Batchrouter error (no error.code in the body). The limit depends on the zone plan, so it is not printed here; for very large files, contact us.
A job above that size today: split the JSONL into several files and run one batch per file.
- Split the file at line boundaries. Mixed-model files are fine, and splitting by file does not change routing or price: each batch is quoted and routed on its own items, at the same rates.
- Upload each file to
POST /v1/fileswith purposebatch_inputand keep itsfile_id. To split a Sectioned v1 file, give each part its own header line, and when a cut falls inside a section, repeat that section's line at the top of the next part. - Quote each file with
POST /v1/quotes/model(one quote per batch), then create one batch per file with the same options, the batch's ownquote_id, a freshIdempotency-Key, and a sharedmetadatakey, for example"metadata": { "run_id": "nightly-2026-10-16" }, so you can join the results afterwards. - Poll each
batch.idand merge the results bycustomer_item_id; keep those unique across the whole job, not just within a file.
The Idempotency-Key header
POST /v1/batches requires an Idempotency-Key header — a unique string of 8–128 characters per logical submission. If a network error leaves you unsure whether the batch was created, retry with the same key: Batchrouter returns the original batch instead of creating a duplicate. Use a fresh key for each new batch (a UUID works well).
Request options
Beyond the input and quote_id, the body accepts these optional fields:
| Field | Values | Default | What it does |
|---|---|---|---|
sla_tier | priority · standard · relaxed · flex · economy | standard | How fast the batch must finish: 4h, 24h, 48h, 72h or 7 days. Slower tiers cost less. See SLA tiers. |
provider_preferences | object | — | Provider controls: order, only, ignore, allow_fallbacks, and fallback ({ regions, providers }, who comes after the first choice, in what order). Also the filters data_collection, zdr, max_total_cost, the tiebreak sort and the preferred_* limits: see Provider preferences. |
webhook | { url, secret } | — | Per-batch completion callback; secret is 8+ chars and signs X-BatchRouter-Signature. |
metadata | object | — | Arbitrary key/value pairs echoed back on the batch. |
input_item_count | integer | — | File-based only. The number of items the manifest should contain; a manifest that parses to a different count is rejected with 400 input_manifest_count_mismatch. |
input_manifest_format | batchrouter_sectioned_v1 · openai_batch_jsonl · batchrouter_items_jsonl (deprecated) · auto | auto | File-based only. The shape of the file. The upload already detects it from the first line and stores it, so you rarely need this. See How the shape is detected. |
Set these consistently with the quote_id you're accepting. If you quoted with a given max_price, create with the same intent so the reserved estimate matches the routing decision.
Create a batch (inline items)
-
Assemble your items and accept the quote.
-
Send the request with an
Idempotency-Key.curl -X POST https://api.batchrouter.com/v1/batches \ -H "Authorization: Bearer $BATCHROUTER_API_KEY" \ -H "Content-Type: application/json" \ -H "Idempotency-Key: $(uuidgen)" \ -d '{ "quote_id": "qlock_abc123", "sla_tier": "standard", "items": [ { "customer_item_id": "item-1", "operation": "chat.completions", "model": "gpt-5.4-mini", "input": { "messages": [ { "role": "user", "content": "Summarize: Batchrouter routes batch-AI workloads across providers." } ] } } ], "webhook": { "url": "https://example.com/hooks/batchrouter", "secret": "a-strong-shared-secret" }, "metadata": { "project": "docs-demo" } }' -
Store
batch.id(batch_…) and poll for completion. See Results.
Create a batch (file-based)
For a large batch, upload the requests as JSONL first, then quote and create with the returned id as input_file_id instead of items. Everything else about the create request is the same.
-
Upload the JSONL to
POST /v1/fileswithX-BatchRouter-Purpose: batch_input: raw body,Content-Type: application/x-ndjson,Content-Lengthrequired. The file is Batchrouter Sectioned v1 or standard OpenAI Batch lines; see JSONL format for the headers and both shapes.curl -X POST https://api.batchrouter.com/v1/files \ -H "Authorization: Bearer $BATCHROUTER_API_KEY" \ -H "X-BatchRouter-Purpose: batch_input" \ -H "Content-Type: application/x-ndjson" \ -H "Content-Length: $(wc -c < requests.jsonl)" \ --data-binary @requests.jsonl # => { "file_id": "file_xyz789", "file": { "kind": "input_manifest", "item_count": 10000, ... } }POST /v1/fileswithX-BatchRouter-Purpose: batch_inputstores a request file (kind: "input_manifest") forinput_file_id. A file without that purpose cannot be used asinput_file_id;POST /v1/batchesrejects it with400 invalid_input_file. The olderPOST /v1/batches/input-manifestsis a deprecated alias of thebatch_inputupload. -
Quote with
POST /v1/quotes/model, sendinginput_file_idandinput_item_countwhere you would senditems. The quote reads the file and prices every request. -
Create the batch with the same
input_file_id, thequote_id, and anIdempotency-Key.curl -X POST https://api.batchrouter.com/v1/batches \ -H "Authorization: Bearer $BATCHROUTER_API_KEY" \ -H "Content-Type: application/json" \ -H "Idempotency-Key: $(uuidgen)" \ -d '{ "quote_id": "qlock_abc123", "input_file_id": "file_xyz789", "input_item_count": 10000, "sla_tier": "standard" }'
A bad line fails with 400 and the line number, before any credits are reserved. Today that check runs when you quote and create; it may move to upload time, so do not rely on the upload succeeding as proof that the file is valid.
The 202 response
A successful create returns 202 Accepted — the batch is queued for routing, not yet processed. The body wraps the batch summary (abridged here; the full batch object also carries sla_tier, pricing_estimate, file ids, and more):
{
"batch": {
"id": "batch_9f3c2a1b7e4d46c8a1b2c3d4e5f60718",
"state": "priced",
"counts": {
"total": 1,
"pending": 1,
"running": 0,
"completed": 0,
"failed": 0,
"canceled": 0
},
"created_at": "2026-06-18T10:00:00Z",
"deadline_at": "2026-06-19T10:00:00Z"
}
}| Field | Meaning |
|---|---|
batch.id | The batch_… identifier you poll and fetch results with. |
batch.state | The lifecycle field. A new batch is priced, then advances queued → dispatching → running → finalizing → completed. See the status lifecycle. |
batch.counts | Per-item state counts: total, pending, running, completed, failed, and canceled. counts.total is the number of items accepted into the batch. |
batch.created_at | When the batch was created. |
batch.deadline_at | The 24-hour SLA deadline by which the batch is expected to complete. |
Replaying the same Idempotency-Key returns this same response rather than creating a second batch.
Errors
| Status | Cause | Fix |
|---|---|---|
400 | Preflight failed (bad JSONL, both/neither input, context window exceeded, unsafe webhook URL). | Inspect error.details.preflight; see JSONL format. |
400 invalid_input_file | input_file_id names a file that is not a request file. | Upload the requests with POST /v1/files and X-BatchRouter-Purpose: batch_input, and use that id. |
400 input_manifest_count_mismatch | The file holds a different number of requests than input_item_count. | Re-upload the file, or fix the count. |
401 | Missing or invalid key. | Check your Authorization header — see Authentication. |
402 | Insufficient credits. | Check GET /v1/auth/account; top up credits in the dashboard at https://app.batchrouter.com/billing. |
409 | Conflict (e.g. reused key on a different payload, or a quote_id whose work no longer matches the request). | Use a fresh Idempotency-Key for a new batch; request a new quote for changed work. |
411 · 415 | File upload without Content-Length, or a batch_input upload with a non-JSONL Content-Type. | Send Content-Length and Content-Type: application/x-ndjson to POST /v1/files. |