Idempotency
Retry a request without doing it twice.
Networks fail. If a request to create a payment or issue a refund times out, you can't tell whether it happened. An idempotency key makes the retry safe: send the same key again and you get the first request's response, instead of a second payment or a second refund.
curl https://api.joingladys.com/api/v1/payments \
-H "Authorization: Bearer access_…" \
-H "Idempotency-Key: 6f1c2a4e-8d0b-4b8e-9f55-3a1d0c7e2b91" \
-H "Content-Type: application/json" \
-d '{"amount": "1500.00", "description": "Order #1042"}'Where to use it
Send Idempotency-Key on every request that creates or changes something:
POST /paymentsPOST /payments/{reference}/capturePOST /payments/{reference}/cancelPOST /payments/{reference}/refunds
It is optional today, but without it a retry is a new request. It will become
required on POST /payments, capture and refunds: a request without a key will
be refused with 400 IDEMPOTENCY_KEY_REQUIRED. Send one now.
Choosing a key
Use a value that identifies the operation, not the attempt: generate it once,
store it with the order or refund it belongs to, and send the same key on every
retry. A random UUID works, as does something like order-1042-refund-1. Keys
can be up to 255 characters.
A key belongs to your app and to one URL. Using refund-1 to refund two
different payments is fine: they are different requests.
What happens
| You send | You get |
|---|---|
| A key you haven't used | The request runs. Its response is remembered for 24 hours. |
| The same key and the same body again | The remembered response, unchanged, with the header X-Idempotent-Replay: true. Nothing runs twice. |
| The same key with a different body | 422 IDEMPOTENCY_KEY_REUSED. Nothing runs. Use a new key for a new request. |
| The same key while the first request is still running | 409 IDEMPOTENT_REQUEST_IN_FLIGHT. Wait a moment and retry. |
| A key, while we can't check it against earlier requests | 503 IDEMPOTENCY_UNAVAILABLE. Nothing ran. Retry with the same key after Retry-After. |
Responses are remembered whether they succeeded or failed with a 4xx, so
retrying a request that was refused returns the same refusal. A 5xx is not
remembered: something went wrong on our side, and retrying with the same key
runs the request again, safely.