# Bulk Operations

> Batch endpoints that create or update many resources in one call: size limits, per-item results, partial failure, idempotency, and when to go async.

A thousand single requests is slow for the client and noisy for the server: a thousand round trips, a thousand auth checks, a thousand log lines. A batch endpoint does the same work in one round trip. The hard part isn't the batching, it's the reporting: when 998 items succeed and 2 fail, the client has to know exactly which 2, and why, without guessing.

## Shape

A batch is a `POST` to the collection's `batch` sub-resource with an array of items. The response is an array of per-item results, in the same order:

```http
POST /customers/batch
Idempotency-Key: f47ac10b-58cc-4372-a567-0e02b2c3d479
Content-Type: application/json

[
  { "email": "alice@example.com", "name": "Alice" },
  { "email": "not-an-email", "name": "Bob" }
]
```

```http
HTTP/1.1 200 OK
Content-Type: application/json

[
  {
    "status": 201,
    "customer": {
      "id": "01932c6c-7616-7590-a8db-cb4b0e4d1c8a",
      "email": "alice@example.com",
      "name": "Alice"
    }
  },
  {
    "status": 422,
    "error": {
      "message": "The given data was invalid.",
      "errors": { "email": ["The email must be a valid email address."] }
    }
  }
]
```

- **Each result has its own `status`,** the code a single request would get, so a client reuses its single-request handling item by item.
- **Successes carry the resource** under its own name, never a `data` wrapper ([JSON Field Conventions](https://www.syntaqx.com/docs/requests/json-conventions)).
- **Failures carry the standard [error format](https://www.syntaqx.com/docs/requests/error-responses).**
- **Order matches the request,** so result `n` is about item `n`. No client-side matching.

## Partial Failure

The default is **independent items**: each one succeeds or fails on its own, and the batch returns `200` as long as it was processed. The HTTP status describes the batch; the per-item `status` describes each item.

Why `200` when some items failed? Because the batch did what it promised: it processed every item and reported on each. A `4xx` for a partly successful batch tells retry logic and monitoring that nothing happened, and a client that retries the whole batch creates the 998 successes twice. WebDAV's `207 Multi-Status` exists for this, but brings an XML body format most HTTP clients don't implement.

| Situation | Response |
| --------- | -------- |
| Batch processed, any mix of item results | `200` with per-item results |
| Batch malformed (not an array, too many items) | `400` or `422`, nothing processed |
| Batch rejected before processing (auth, rate limit) | `401`, `403` or `429`, nothing processed |

Where items must succeed or fail together (moving money between accounts, a [ledger transaction](https://www.syntaqx.com/docs/money/payment-states)), the endpoint is **atomic** and says so: one invalid item fails the whole request with `422`, and nothing is written. Never mix the two modes behind a flag: a client that forgets it gets partial writes where it expected all-or-nothing, a bug that shows up as an unbalanced ledger, not an error.

## Limits

- **At most 100 items per batch.** A ceiling bounds what one request can cost: body size, memory, lock time, and the client's wait. 100 finishes in a normal request's time, cuts round trips by two orders of magnitude, and matches the [page size ceiling](https://www.syntaqx.com/docs/requests/pagination), so a client can fetch a page, change it, and write it back in one batch. Bigger inputs go to an async import.
- **A batch counts as one request per item** against [rate limits](https://www.syntaqx.com/docs/requests/rate-limiting). Batching saves round trips, not quota; otherwise it's a way to do 100 times the work under the same limit.
- **Items are validated before any work starts** when it's cheap, so a malformed batch fails in milliseconds instead of halfway through.

## Idempotency

The whole batch takes one [Idempotency-Key](https://www.syntaqx.com/docs/requests/idempotency). A retry replays the stored per-item results, so the successful items aren't created twice and the failed ones aren't silently retried. To retry only the failures, the client sends a new batch, with a new key, containing just those items.

## When to Go Async

If a batch can't reliably finish in a few seconds, make it a [long-running operation](https://www.syntaqx.com/docs/requests/long-running-operations): return `202 Accepted` with an operation to poll, and put the per-item results at the operation's `result_url` when it's done. [Imports](https://www.syntaqx.com/docs/building-blocks/imports-and-exports) of thousands of rows always go this way.

## References

**Specifications**

- [RFC 9110: HTTP Semantics](https://datatracker.ietf.org/doc/html/rfc9110), the status codes each item result reuses
- [RFC 4918: WebDAV, 207 Multi-Status](https://datatracker.ietf.org/doc/html/rfc4918#section-11.1), the multi-status alternative and why it isn't used here

**Reference**

- [MDN: 422 Unprocessable Content](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/422), item-level validation failures

**In practice**

- [Google AIP-233: Batch methods: Create](https://google.aip.dev/233), batch create semantics
- [Google AIP-234: Batch methods: Update](https://google.aip.dev/234), batch update semantics
- [GitHub Docs: Using pagination in the REST API](https://docs.github.com/en/rest/using-the-rest-api/using-pagination-in-the-rest-api), a 100-item ceiling on most list endpoints, the same order of magnitude as the batch limit
- [Stripe: API Reference](https://docs.stripe.com/api), the counterpoint: one object per request, no bulk updates

---

Canonical URL: https://www.syntaqx.com/docs/requests/bulk-operations
