Idempotency
Send an Idempotency-Key with a POST and a retry returns the first response instead of running the request again.
How it works
Add a header with a value you choose:
We remember the key together with the method, the path and the body.
| You send | You get |
|---|---|
| The same key and the same body again | The first response, with the same status. _agent.summary says it is a replay. |
| The same key with a different body | 409 idempotency_key_reused |
| The same key while the first request is still running | 409 idempotency_in_progress. Retry shortly with the same key. |
| The same key, and the first request started more than 2 hours ago and never finished | 409 idempotency_outcome_unknown. See below. |
A key that starts with internal: | 400 idempotency_key_invalid. That prefix is reserved; choose another key. |
A request that failed with a 4xx is remembered too, so the retry gets the same refusal: the original status code, with the same error.code and error.message. A request that failed with a 5xx is not remembered: retry with the same key and it runs again.
Keys are tracked on POST only. Query parameters are not part of the comparison.
When the outcome is unknown
If the first request with a key stopped part way, for example because the server restarted, we cannot tell whether it took effect, and running it again could do it twice. After 2 hours the key answers 409 idempotency_outcome_unknown, with error.details.first_request_at. Check whether the first request took effect, for example by listing the resource it would have created. If it did, you are done. If it did not, send the request again with a new key.
Choosing a key
Derive the key from your own record of the operation: an order id, a sign-up id, a job id. Reuse it on every retry of that operation.
Do not generate a fresh random key on each retry. That turns one operation into several.
Where a key is required
These move money, so they refuse to run without one and answer 400 idempotency_key_required:
POST /api/v1/invoices/{id}/chargePOST /api/v1/invoices/{id}/refundsPOST /api/v1/invoices/{id}/record-paymentPOST /api/v1/invoices/one-off
Where a key is not tracked
A few POST endpoints do not store keys. Sending the header there does no harm, and a retry runs the request again.
POST /api/v1/api-keys,POST /api/v1/api-keys/{id}/roll,POST /api/v1/webhook-endpointsandPOST /api/v1/webhook-endpoints/{id}/rotate-secret. Their reply contains a secret that is shown once, so the reply is never stored for replay. A retry creates a second key or endpoint; revoke or remove the spare.POST /api/v1/webhook-endpoints/{id}/test. Each call sends a new test event.POST /api/v1/checkout-sessionsandPOST /api/v1/portal-sessions. A second session is a second link; it costs nothing until a payer uses it.POST /api/v1/pricing-tables.
The API reference shows the Idempotency-Key header on every endpoint that tracks it.
Requests that carry their own id
Some bodies have an id of their own, and repeating it is always safe:
- Usage events:
idempotency_keyonPOST /api/v1/usage-events, and each event'sidonPOST /api/v1/meter-events. - Credits:
idon the consume and grant endpoints. A repeat answers withduplicate: true.
In the SDK
The SDK retries only what is safe to repeat. subscriptions.create, subscriptions.changePlan, invoices.createOneOff and invoices.recordPayment need an idempotencyKey from you and refuse to run without one.