Retries and idempotency
Idempotency-Key, which writes are safe to retry, and Retry-After.
Idempotency-Key
A network can fail after the API has done a write but before you hear back. To retry safely, send Idempotency-Key: <unique key> on writes the API marks idempotent (x-idempotent: true in the document, “Idempotent” in the reference). A UUID per logical write is right.
- Retry with the same key and the same request (method, path, query and body) and you get the first response again, for 24 hours, with
Idempotent-Replayed: true. The write isn’t done twice. - The same key with a different request answers
422 idempotency_key_reused. - A retry while the first request is still running answers
409 idempotency_in_progress. Wait, then retry. - A secret is shown once, and never kept for a retry. Writes that answer one (a new API key’s
token, a webhook’s signingsecret) still aren’t done twice, but a retry after the first succeeded answers409 secret_already_issued, with what was made inissuedand no secret. If you didn’t store the first answer, rotate that key or secret for a new one. - Only successful responses are kept: after an error, the key is free and a retry runs again.
- Keys belong to the caller, per route. Signed-out requests ignore the header.
import { createCollectorClient } from '@collector/sdk';
const collector = createCollectorClient({ token: process.env.COLLECTOR_TOKEN });
// Keep the key with the job that makes the write: a retry from another process, an hour
// later, replays the first answer instead of creating a second alert.
const idempotencyKey = crypto.randomUUID();
const { data, attempts, requestId } = await collector.withResponse.createPriceAlert({
body: {
issueId: '00000000-0000-4000-8000-000000000000',
editionId: null,
graded: false,
bandId: '9.0-9.6',
direction: 'below',
targetCents: 2500,
currency: 'USD',
},
idempotencyKey,
});
console.log(`alert ${data.alert.id} after ${attempts} attempt(s), request ${requestId}`);Which writes are idempotent
- POSTPublish or schedule an announcement
- PATCHEdit an announcement
- POSTTake an announcement down now
- POSTMake an admin API key
- POSTRotate an admin API key
- POSTLift an API block
- PATCHEdit a catalog record
- POSTAdd an imprint to a publisher
- POSTLink an edition to its series
- DELETERemove an edition link
- POSTLock or unlock a field
- POSTChange a series or issue slug
- POSTVerify a provisional record
- POSTRevert an overridden field
- POSTPut an imprint on series
- POSTPut an imprint on many series, as a job
- POSTLink an issue to an issue it reprints
- DELETERemove a reprint link
- POSTCancel a catalog job
- POSTMove issues to another series
- POSTUndo an issue move
- POSTUndo a revert
- POSTApprove or decline a code
- POSTUndo a code’s review
- POSTApprove or decline a cover photo
- POSTUndo a cover photo’s review
- POSTReview a provisional edition
- POSTUndo an edition’s review
- PATCHResolve or reopen an incident
- POSTCancel an import job
- POSTResolve an import conflict
- POSTStart a Metron sync now
- POSTMerge two records
- POSTUndo a merge
- POSTApprove or reject a change request
- POSTUndo a change request’s review
- POSTDecide many moderation items, as a job
- POSTCancel a moderation job
- POSTGive someone a plan
- POSTEnd a plan grant now
- POSTDecide a report
- POSTUndo “hide content” for a report
- PUTChange platform settings
- PATCHVerify, suspend or set a store’s fee
- PATCHClose or reopen a support message
- PATCHChange someone’s role or access
- POSTApprove or decline a variant proposal
- POSTResolve an import suggestion
- POSTUndo a review in the Variants queue
- POSTMake an admin webhook
- DELETEDelete an admin webhook
- PATCHChange an admin webhook
- POSTResend an admin webhook delivery
- POSTRotate an admin webhook’s secret
- POSTSend a test event to an admin webhook
- POSTMake a personal API key
- POSTRotate a personal API key
- POSTSuggest a correction
- POSTSend a cover photo
- POSTBring a candidate into the catalog
- POSTAdd a book no source knows yet
- POSTPropose a variant
- POSTImport a collection file
- POSTCommit a reviewed import
- POSTAdd a copy
- POSTUndo a change to a copy
- POSTAdd a code to a copy
- POSTChange what a copy is
- PUTSay which variant your copy is
- POSTAdd many copies
- POSTChange many copies at once
- POSTAdd a storage location
- POSTAsk a store to hold a book
- POSTCancel a hold
- POSTRecord the sale
- POSTMessage about a hold
- POSTAccept or decline a hold request
- POSTCreate a price alert
- POSTMake labels
- POSTRequest a full export
- POSTAsk a store about a book
- POSTSet the book aside for the asker
- POSTReply in a question’s thread
- POSTReport something
- POSTCreate a scan
- POSTAdd a scan to the collection
- POSTStart a scanning session
- POSTAct on many scans
- POSTCreate a store
- POSTMake a store API key
- POSTRotate a store API key
- POSTAccept a hold request
- POSTRecord a hold’s sale
- POSTDecline a hold request
- POSTImport inventory rows
- POSTUpsert listings by SKU or barcode
- POSTAdjust stock
- POSTList a book
- POSTSet many listings’ status
- POSTInvite someone to the team
- POSTAnswer a question
- POSTMake a store webhook
- POSTResend a store webhook delivery
- POSTRotate a store webhook’s secret
- POSTMake a webhook
- POSTResend a delivery
- POSTRotate a webhook’s secret
- POSTAdd a wish
- POSTGot it: move a wish into your collection
Other writes aren’t replayed. Before retrying one, check whether it happened (read the record back).
When to retry
- 429 and 503: wait for the
Retry-Afterheader (seconds), then try again. - 500, 502, 504 and dropped connections: retry reads, and idempotent writes with their key, backing off exponentially with some randomness.
- Other 4xx: don’t: the request needs changing. The problem’s
codesays why.
The SDK does all of this: two retries by default, honouring Retry-After, with a fresh Idempotency-Key on each idempotent write, and one X-Request-Id across a call’s retries.