View as Markdown

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:

request
curl -X POST https://globalymint.com/api/v1/subscriptions \
  -H "Authorization: Bearer $MINT_SECRET_KEY" \
  -H "Idempotency-Key: signup-ws_123" \
  -H "Content-Type: application/json" \
  -d '{"customer_id":"0b8f6c1e-3d2a-4a57-9c41-7e5d2f8a1b90","plan_id":"7a2d9c4e-1f3b-4e5a-9b6c-0d1e2f3a4b5c"}'

We remember the key together with the method, the path and the body.

You sendYou get
The same key and the same body againThe first response, with the same status. _agent.summary says it is a replay.
The same key with a different body409 idempotency_key_reused
The same key while the first request is still running409 idempotency_in_progress. Retry shortly with the same key.
The same key, and the first request started more than 2 hours ago and never finished409 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}/charge
  • POST /api/v1/invoices/{id}/refunds
  • POST /api/v1/invoices/{id}/record-payment
  • POST /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-endpoints and POST /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-sessions and POST /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_key on POST /api/v1/usage-events, and each event's id on POST /api/v1/meter-events.
  • Credits: id on the consume and grant endpoints. A repeat answers with duplicate: 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.

On this page