Documentation menu

Idempotency

Retry any management write safely with the Idempotency-Key header.

A network error leaves you not knowing whether a write happened. Send an Idempotency-Key header with every management API write (POST, PUT, PATCH, DELETE), and you can retry it with the same key as often as you need: it takes effect once, and every retry gets the first answer.

curl https://api.hixel.dev/v1/customers/org_42/top_ups \
  -H "Authorization: Bearer $HIXEL_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: 6c1b7c58-0b8e-4b2f-9d3e-5a9f3c2e1d7a" \
  -d '{ "amount_micros": "25000000", "success_url": "https://app.example.com/paid", "cancel_url": "https://app.example.com/billing" }'

How it works#

  • The first successful answer to a key is stored for 24 hours, scoped to the calling key's environment.
  • A retry with the same key and the same request (method, path, query and body) gets the stored answer again, with the header Idempotent-Replayed: true. Nothing runs twice.
  • A retry with the same key and a different request is refused: 409 idempotency_key_reused.
  • A retry while the first request is still running is refused: 409 idempotency_key_in_use. Wait and retry.
  • A request that failed (any error) stores nothing: retrying it with the same key runs it again.

A key is 1 to 255 characters of visible ASCII (400 invalid_idempotency_key otherwise). Use a random UUID per operation, or an id from your own system that names the operation, such as top-up:order_8812. GET requests ignore the header; reads are safe to retry anyway.

Secrets are not replayed#

An answer that contains a secret (a new key's secret, a webhook endpoint's signing secret) is stored with that secret set to null. A replay tells you the object was created, but the secret was shown only to the first answer. If you lost it, rotate the key or the endpoint's secret.

Credits have their own key#

Grants and credits take an idempotency_key in the body as well (Grants and credits). The two do different jobs:

  • The header replays the first successful response for 24 hours, per environment. A failed call stores nothing and frees the key. Reusing it for another request is 409 idempotency_key_reused.
  • The body's idempotency_key makes the ledger credit happen once per wallet, forever. Reusing it with another kind, amount or expiry is 409 credit_idempotency_key_reused.

Use the body key for plan allowances and payments, and the header for transport-level retries.

Retrying well#

  1. Generate the key before the first attempt and keep it with the operation.
  2. Retry on network errors, 409 idempotency_key_in_use, 429 and 5xx, with exponential backoff.
  3. Do not retry other 4xx: the request itself is wrong.