Batch requests
Send many API requests in one round trip with the batch endpoint, and control how many run at the same time.
The batch endpoint runs several API requests for you and returns all responses together. Use it to save round trips from mobile clients, to import data, or to fan out writes from a function.
POST
/batchTerminal
curl -X POST https://api.example.com/api/batch \
-H "Content-Type: application/json" \
-d '{
"concurrency": 2,
"requests": [
{"id": "list", "method": "GET", "url": "bucket/<bucketId>/data?limit=1", "headers": {"Authorization": "APIKEY <key>"}},
{"id": "create", "method": "POST", "url": "bucket/<bucketId>/data", "body": {"title": {"en_US": "Batched"}, "slug": "batched"}, "headers": {"Authorization": "APIKEY <key>"}}
]
}'| Field | Description |
|---|---|
requests[].id | Required. Your identifier, echoed in the response. |
requests[].method | Required. GET, POST, PUT, PATCH or DELETE. |
requests[].url | Required. Path relative to the API's public URL, without a leading /api. |
requests[].body | Optional JSON body. |
requests[].headers | Optional headers, including Authorization. |
concurrency | How many requests run at the same time. 0 (default) means no limit. |
Response
JSON
{
"responses": [
{"id": "list", "status": 200, "headers": {"content-type": "application/json; charset=utf-8"}, "body": [{"_id": "6b0b1972a5611972c29c25fd", "title": "Hello Spica"}]},
{"id": "create", "status": 201, "headers": {"content-type": "application/json; charset=utf-8"}, "body": {"_id": "6b0b1ba5a5611972c29c2658", "title": {"en_US": "Batched"}, "slug": "batched"}}
]
}Each request is authenticated and authorized on its own, with the headers you give it. A failing request doesn't stop the others: check the status of each response.
Tips
- Use
concurrencyto protect the database during large imports. - Requests in a batch aren't a transaction. Make them independent, or check each response and compensate on failures.
- The devkit's
insertManyandremoveManyuse this endpoint.