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/batch
Terminal
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>"}}
    ]
  }'
FieldDescription
requests[].idRequired. Your identifier, echoed in the response.
requests[].methodRequired. GET, POST, PUT, PATCH or DELETE.
requests[].urlRequired. Path relative to the API's public URL, without a leading /api.
requests[].bodyOptional JSON body.
requests[].headersOptional headers, including Authorization.
concurrencyHow 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 concurrency to 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 insertMany and removeMany use this endpoint.