Idempotency

Retry a request that changes data without doing it twice.

Send an Idempotency-Key header so you can retry a request without charging a customer twice. A retry with the same key gets the first response back.

Which requests take a key

Every POST and PATCH takes an Idempotency-Key. GET, PUT and DELETE ignore the header, because repeating them has the same result.

The calls that create something require it, such as Create a payment and Create a refund. Without it they answer 400 with the code parameter_missing. On the other writes the key is optional, but a request without one runs every time you send it.

A key belongs to your merchant and to one mode. The same text used with a test key and with a live key is two keys.

Choosing a key

Use a new random value for each new request. A UUID works well. Use 1 to 255 visible ASCII characters with no spaces, or the request is refused with 400, the code parameter_invalid and param set to Idempotency-Key. Store the key with your own record of the operation, so a retry sends the same one.

Never put personal data in a key, such as the customer's phone number or email address. Your own order number is fine.

Idempotency-Key: 5f1c1e0a-3b7e-4c8e-9d1a-2f6b8a4c7e11

A key is kept for 24 hours. After that, the same key starts a new request.

What you get back

You sendYou get
A new keyThe request runs.
The same key, method, path and body againThe first response, with its status (201 for a created payment), its body and the header Idempotent-Replayed: true. Nothing runs again.
The same key with another method, path or body400 with type idempotency_error and code idempotency_key_reused. Use a new key for a different request.
The same key while the first request is still running409 with type idempotency_error and code idempotency_request_in_progress.

What is kept

The first response is kept when it is a success or a 4xx other than 429. A repeat of a request that was refused with 400 or 404 gets the same refusal back with Idempotent-Replayed: true. To fix such a request, change it and use a new key.

A 5xx and a 429 are not kept, and the key is free again. A retry with the same key runs the request again.

When to retry

Retry with the same key after a network error, a timeout, a 429, a 500, a 502 or a 504. Retry a 503 only when it has a Retry-After header, after that many seconds. A 409 with code idempotency_request_in_progress means the first request with this key is still running: retry with the same key after a short wait, and the retry returns its result once it finishes. Do not retry any other 409 or 4xx unchanged: read the resource, then decide. See Errors.

On this page