Batch writes with the workbook API
What it does
Section titled “What it does”POST /batch runs several actions on one workbook in order. A successful batch creates one new revision. If any action fails, the whole batch is canceled. Notification records are saved with the write; live updates and optional email run only after commit, so a rolled-back batch sends neither.
How to use it
Section titled “How to use it”Get the workbook revision and make sure the access key has the rights needed for every action. Use one idempotencyKey for the whole batch. To write only if the workbook still has the revision you read, send it in If-Match.
curl -X POST "$BASE/batch" -H "Authorization: Bearer $KEY" \ -H 'Content-Type: application/json' -H 'If-Match: "412"' --data-binary '{ "idempotencyKey": "weekly-report-0412", "actions": [ {"action":"sheet-create","ref":"report","data":{"title":"Report","header":["Product","Price"]}}, {"action":"values","sheetRef":"report","data":{"range":"A2","values":[["Test",12.5]]}} ] }'Each action has an action and a data object. For a sheet action, provide exactly one address: sheetId or a sheetRef declared earlier in the batch. Declare ref only with sheet-create or sheet-copy; it must start with a Latin letter and contain up to 32 Latin letters, digits, hyphens or underscores. A sheet-copy ref addresses the new copy in later actions. Use the fields from a single write request in data, without idempotencyKey or baseRevision. For deleting a sheet, use data: {}.
A 200 response contains one revision and a results array in action order. Each result contains the action and the fields returned by its single-request equivalent. sheet-create and sheet-copy return the new sheet and the declared ref.
Limitations
Section titled “Limitations”| Action | Right needed |
|---|---|
append |
Add rows |
update, values |
Edit cells |
upsert |
Edit cells and Add rows |
delete-rows, sheet-rename, sheet-delete |
Rows and sheets |
sheet-create |
Rows and sheets, with a key for All sheets |
columns-insert, columns-delete |
Rows and sheets |
sheet-copy |
Rows and sheets and Edit cells on All sheets |
name-upsert, name-delete, format, validation-upsert, validation-delete |
Edit cells |
Each action uses coordinates as they are when that action runs. After a row is deleted, later actions see the shifted rows and protections. The usual per-action limits, formula rules and data validation rules also apply inside a batch.
A batch can include editor operations. Like sheet-create, sheet-copy can return a new sheet through ref. Cells copied, formatted or updated by a validation rule count toward the batch’s shared cell limit.
A batch can contain up to 20 actions, a 1 MiB body, 10,000 written cells, 500 new rows from adding or upserting, and 1,000 deleted rows. The API accepts up to 30 batches per minute per key and admits up to 16 in-flight batch requests per server process before reading their bodies. Uploads have a 10-second limit; canceling a request stops its body read. Send batches from a server. A website form key still adds rows through a separate request.
If something goes wrong
Section titled “If something goes wrong”- An action error includes
actionIndex. The first action has index0. - For
412 revision_conflict, read the workbook again and check the action coordinates. Use a new idempotency key for the corrected batch. - If the connection drops, retry the same batch with the same
idempotencyKeyandIf-Match. A successful response is kept for 48 hours and replayed only if the key still has access to the target in its current scope. - For
409 idempotency_conflict, use a new key for a new body or condition. - For
413 too_large, reduce the batch. An upload that exceeds 10 seconds is canceled and returns503. A database lock wait is limited to 2 seconds; a timeout returns503withRetry-After. For429or503, wait forRetry-Afterbefore retrying.
