Recipe

Retry a write safely with an idempotency key

A retried write is dangerous without an idempotency key. If the first request succeeded before the connection dropped, a naive retry creates a duplicate. Send an Idempotency-Key: the server recognises the replay and returns the original result instead of acting twice.

2 min read

Idempotency-KeyHeader to send
Same keyOnly for retries
409Key reuse conflict

Send a key per operation

Generate a unique key (a UUID) for each distinct write, and reuse that same key only when retrying that same request.

curl -sS -X POST https://hub.credicorp.co.uk/public/v1/enquiries \
  -H 'Content-Type: application/json' \
  -H 'Idempotency-Key: 5f2c9a1e-8b3d-4a7c-9e21-0d6f1b2c3a4e' \
  -d '{"form":"contact-us","fields":{"consent":"yes"}}'

What the server does

On first receipt it processes and records the result under the key. On a replay with the same key and same body, it returns the stored result — no duplicate. A same key with a different body returns 409 idempotency_key_reused.

Combine with backoff

Pair keys with jittered backoff so a timeout-then-retry is both safe and well-behaved.

Frequently asked questions

How long is a key remembered?

Long enough to cover a sensible retry window. Reuse the same key only for genuine retries of the identical request; generate a fresh key for anything new.

What if I reuse a key with a different body?

You get a 409 idempotency_key_reused. Keys are bound to the request they first accompanied. See that error.

Funding for UK limited companies

Credicorp lends to your company, not to you personally — short-term working capital with no personal guarantee. See what your business could access.