Skip to content

Writing to a spreadsheet with a key

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.

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 and If-Match condition 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 returns 409 idempotency_conflict.
  • baseRevision is the revision from a read and independently determines the coordinates for an edit. For PATCH /cells, row numbers and headerRow refer 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 returns 409. For other edits and deletions, row numbers refer to baseRevision; 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, including DELETE /sheets/:id. Send one strong tag such as If-Match: "412"; its value must be a canonical decimal integer from 0 to 9007199254740991, with no leading zeroes. An unsupported or malformed value returns 400 invalid_request. If it does not match the current revision, the API returns 412 revision_conflict with the current revision and makes no change. baseRevision still 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 /workbook
H=(-H "Authorization: Bearer $KEY" -H "Content-Type: application/json")
Окно терминала
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.

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 key
form.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>
Окно терминала
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.

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.

Окно терминала
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.

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.

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.

Окно терминала
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.

  • 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-Match header.
  • Formats, named ranges and data validation are available through the editor API. Comments and access settings cannot be changed with a key.

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