Writing to a spreadsheet with a key
What it is
Section titled “What it is”A program with an access key adds rows (for example, orders from a website form), edits cells, updates rows by SKU or number, deletes rows, writes a range and formulas, and creates, renames and deletes sheets. An edit made with a key appears in the spreadsheet at once, is labelled Key “…” in cell history and comes to notifications, just like an edit by a person. For several actions in one request, see Batch writes with the workbook API; to sync a local copy, see Workbook changes via the API; for columns, sheet copies, named ranges, formats and validation, see Workbook editor operations via the API.
How to use it
Section titled “How to use it”Rights and general rules
Section titled “Rights and general rules”| Request | Right needed |
|---|---|
| Add rows | Add rows |
| Edit cells, write a range | Edit cells |
| Update rows by a key column | Edit cells and Add rows |
| Delete rows; create, rename and delete a sheet | Rows and sheets |
| Write a formula | Edit cells on All sheets |
- A column is found by the text of the header row
headerRow(row 1 by default), ignoring case and spaces at the edges. - A value is a string, a number,
true/false,null(clears the value) or a formula as an object{"formula":"=B2*C2"}. A string starting with “=” is stored as text. The cell’s formatting, link, note and photo stay. idempotencyKey(8–200 characters) is required everywhere except renaming and deleting a sheet. Within 48 hours, a repeat with the same key, body andIf-Matchcondition returns the saved success response without writing again, even if the spreadsheet has since changed. The API checks that you can still access the target before replaying it. Reusing the key with a changed body or condition returns409 idempotency_conflict.baseRevisionis therevisionfrom a read and independently determines the coordinates for an edit. ForPATCH /cells, row numbers andheaderRowrefer to this revision, while each column is found by its current header name. The API rebases coordinates and formula references once; if a target coordinate was added or deleted since that revision, it returns409. For other edits and deletions, row numbers refer tobaseRevision; an edit follows the same row when rows above it move, but a deleted target row conflicts. A range, an update by a key column and sheet operations use the current spreadsheet state.- All eight write requests accept an optional
If-Match, includingDELETE /sheets/:id. Send one strong tag such asIf-Match: "412"; its value must be a canonical decimal integer from0to9007199254740991, with no leading zeroes. An unsupported or malformed value returns400 invalid_request. If it does not match the current revision, the API returns412 revision_conflictwith the current revision and makes no change.baseRevisionstill controls coordinates independently. - A key edits protected cells, rows and sheets only if the protection’s Who can edit is All editors. A data validation rule set to reject does not let an invalid value through; one set to warn does.
- New rows go below the last non-empty row of the sheet (in any column), but never above the header; the sheet grows when needed. Two simultaneous requests get different rows.
KEY=dsk_…; BASE=https://api.duoshao.org/v1; SHEET=… # sheet id from /workbookH=(-H "Authorization: Bearer $KEY" -H "Content-Type: application/json")Add rows
Section titled “Add rows”curl -X POST "${H[@]}" $BASE/sheets/$SHEET/rows \ -d '{"idempotencyKey":"order-20261113-0001","rows":[{"Name":"John","Phone":"+7 700 000 00 00","Quantity":5}]}'The response 201 {"revision":413,"rows":[{"row":7}]} gives the numbers of the new rows.
Website form
Section titled “Website form”A key with the single Add rows right and a Websites list is called right from the page. Anyone can see such a key in the page code, so it can only add rows and only from the listed websites; it does not write formulas.
<script>const form = document.querySelector('#order');let requestId = crypto.randomUUID(); // one order, one repeat keyform.addEventListener('submit', async (event) => { event.preventDefault(); const data = new FormData(form); const response = await fetch('https://api.duoshao.org/v1/sheets/SHEET_ID/rows', { method: 'POST', headers: { 'Authorization': 'Bearer dsk_…', 'Content-Type': 'application/json' }, body: JSON.stringify({ idempotencyKey: requestId, rows: [{ 'Name': data.get('name'), 'Phone': data.get('phone') }] }) }); if (response.ok) { requestId = crypto.randomUUID(); form.reset(); }});</script>Edit cells
Section titled “Edit cells”curl -X PATCH "${H[@]}" $BASE/sheets/$SHEET/cells \ -H 'If-Match: "412"' \ -d '{"idempotencyKey":"status-412-0001","baseRevision":412,"updates":[{"row":7,"values":{"Status":"On the website","Price":1990,"Total":{"formula":"=C7*D7"}}}]}'The response is 200 {"revision":413}. row is a row number below the header.
Update rows by a key column
Section titled “Update rows by a key column”key is a column header, for example the SKU. A row that already has this value in that column is updated (the key cell itself is not rewritten); otherwise a new row is added. Values are compared as text without spaces at the edges and case-sensitively: the number 1001 and the string “1001” are the same; a cell with a formula does not count as a key.
curl -X PUT "${H[@]}" $BASE/sheets/$SHEET/rows \ -d '{"idempotencyKey":"sync-20261113-01","key":"SKU","rows":[{"SKU":"MLT-1","Price":150},{"SKU":"MLT-9","Name":"New","Price":9}]}'The response is 200 {"revision":414,"rows":[{"row":2,"created":false},{"row":7,"created":true}]}. One value twice in the sheet or in the request gives 422 ambiguous_key, and nothing is written.
Delete rows
Section titled “Delete rows”curl -X POST "${H[@]}" $BASE/sheets/$SHEET/rows/delete -d '{"idempotencyKey":"clean-412-0001","baseRevision":412,"rows":[5,2,3]}'The response is 200 {"revision":415,"deleted":3}. The order and repeats of the numbers do not matter; the rows below move up. The header row and rows above it cannot be deleted.
Write a range
Section titled “Write a range”range is the top-left cell (B2) or a rectangle (B2:D5) that the values must fit into; values are rows of values. The sheet grows to fit the write.
curl -X PUT "${H[@]}" $BASE/sheets/$SHEET/values \ -d '{"idempotencyKey":"calc-20261113-01","range":"A2","values":[[1700,4,{"formula":"=A2*B2"}],[null,"text"]]}'The response 200 {"revision":416,"range":"A2:C3","cells":5} gives the rectangle taken and the number of cells written.
Formulas
Section titled “Formulas”A formula is written as an object {"formula":"=…"} in any write request; a read returns its result, and the render=formula view returns its text. Only a key with the Edit cells right on All sheets can write a formula, because a formula can refer to a sheet outside the key. A key for selected sheets, an add-only key and a form key get 403 formula_denied. A value written over a formula replaces it.
Sheets
Section titled “Sheets”curl -X POST "${H[@]}" $BASE/sheets -d '{"idempotencyKey":"sheet-nov-0001","title":"November report","header":["Date","Amount"]}'The response is 201 {"revision":417,"sheet":{"id":"…","title":"November report","rows":300,"cols":30}}: the sheet is added last, and header holds the texts of the first row. Only a key on All sheets can create sheets.
curl -X PATCH "${H[@]}" $BASE/sheets/$SHEET -d '{"title":"Summary"}' # 200 {"revision":418,"sheet":{"id":"…","title":"Summary","rows":300,"cols":30}}curl -X DELETE -H "Authorization: Bearer $KEY" $BASE/sheets/$SHEET # 200 {"revision":419}A name is compared ignoring case and spaces at the edges; the only sheet cannot be deleted.
Limitations
Section titled “Limitations”- Adding: up to 100 rows and 64 KB per request; editing cells and writing a range: up to 10,000 cells and 512 KB; updating by a key column: up to 500 rows, 10,000 cells and 512 KB; deleting: up to 1000 row numbers and 64 KB.
- A string in a cell is up to 5000 characters, a formula up to 8192. A sheet is up to 99,999 rows and 999 columns and up to your plan’s row limit; up to 50 sheets per spreadsheet; a sheet name is up to 100 characters.
- A key’s Rows per day counts added rows: by adding, and new rows from updates by a key column.
- From a browser, only adding rows with a key that has websites; everything else only from your own server. Browser preflight also allows the
If-Matchheader. - Formats, named ranges and data validation are available through the editor API. Comments and access settings cannot be changed with a key.
If something goes wrong
Section titled “If something goes wrong”General errors (401, 403 forbidden, 404, 429, 503) are in Access keys and the spreadsheet API. The response may carry details: column, row, value, revision.
| What you see | Why | What to do |
|---|---|---|
400 invalid_request |
The body is not JSON or does not match the pattern: no idempotencyKey, key or baseRevision, an empty list, an extra field, a sheet name that is empty or longer than 100 characters; or an unsupported/malformed If-Match |
Compare with the examples |
403 forbidden |
The needed right is missing; a key for selected sheets creates a sheet | A key with this right on All sheets |
403 formula_denied |
A formula is written by a key without Edit cells on All sheets | Issue such a key or write a value |
403 protected |
The cell, row or sheet is protected | Allow All editors in the protection |
409 revision_conflict |
The target coordinate itself was added or deleted since baseRevision, or another edit’s target row was deleted; the response has the current revision |
Read again and check the intended row and header |
409 idempotency_conflict |
The same idempotencyKey was used with a different body or If-Match condition |
Use a new key for a new request |
412 revision_conflict |
If-Match does not match the current revision; no write was made, and the response has the current revision |
Read again and check the intended change |
409 title_taken |
A sheet with this name already exists | Choose another name |
409 last_sheet |
The only sheet is being deleted | Create another one first |
413 too_large |
More rows, cells or bytes than in “Limitations” | Split into parts |
413 row_limit |
The sheet is full: 99,999 rows or the plan limit | A new sheet or a higher plan |
413 sheet_limit |
The spreadsheet has 50 sheets | Delete an unneeded sheet |
422 unknown_column, 422 ambiguous_column |
No column with this header, or two of them; the response has column |
Check the headers and headerRow |
422 invalid_value |
The value is an array, an object not of the form {"formula":"=…"}, a string longer than 5000; an empty value in the key column |
A string, a number, true/false, null or a formula |
422 invalid_row |
The header row, above it or beyond the sheet; the response has row |
A row number from a read |
422 invalid_range |
The address is not of the form B2 or B2:D5, the values are wider or taller than the rectangle, or go beyond column 999 |
Fix range |
422 ambiguous_key |
A key column value occurs more than once; the response has column and value |
Remove the duplicate in the sheet or in the request |
422 validation_failed |
The value fails data validation; the response has column and row |
Fix the value |
429 daily_limit |
Rows per day is used up | Wait for Retry-After |
