Batch API
POST /api/batch
Runs several record writes in one database transaction: if any fails, none of them is kept. Each one is checked like the same request on its own (the collection's rules, your hooks).
Batch requests are off until you turn them on: Settings → Application → Batch API, or PATCH /api/settings with
{"batch": {"enabled": true, "maxRequests": 50, "timeout": 3}}.
| Body | |
|---|---|
requests |
the writes, in order: each a method, a url (a Records API path), and its body (and optional headers) |
curl -X POST http://127.0.0.1:8090/api/batch \
-H "Authorization: $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"requests": [
{"method": "POST", "url": "/api/collections/orders/records", "body": {"total": 42}},
{"method": "PATCH", "url": "/api/collections/products/records/p4x8m2k1z9q0w3e", "body": {"stock-": 1}},
{"method": "DELETE", "url": "/api/collections/carts/records/c7n2b5v8x1z4q6r"}
]
}'
It answers one result per request, in order:
[
{ "status": 200, "body": { "collectionName": "orders", "id": "o1a2b3c4d5e6f7g", "total": 42 } },
{ "status": 200, "body": { "collectionName": "products", "id": "p4x8m2k1z9q0w3e", "stock": 9 } },
{ "status": 204, "body": null }
]
If one fails, the whole batch answers 400, with the failed request's index and error in data.
The urls may only be the Records API's create, update, upsert (PUT /api/collections/{collection}/records, which
creates or updates by the body's id) and delete. To upload files in a batch, send multipart/form-data with the
JSON in a @jsonPayload field and each file as requests.N.fieldName.
Next
- Records API: the requests a batch is made of.
- Records: transactions in your own code.
Questions and comments
Comments are a GitHub Discussions thread; sign in with GitHub to post.