Retrieve a stored response with idempotency
On endpoints that support idempotency, a new attempt with the same key and the same body can retrieve a response that is already stored. When a replay is served, the API does not process the request again. This mechanism does not make retries free and does not guarantee a single execution for concurrent requests.
Why use idempotency
Networks are unreliable. A client may:
- Send a request that succeeds on the server but whose response gets lost in flight.
- Crash in the middle of an automatic retry.
- See a timeout at the load balancer while the API actually processed the request.
Without idempotency, you must choose between:
- Not retrying, and risk losing legitimate operations.
- Retrying blindly, and risk executing the same operation multiple times.
Idempotency lets you retrieve a response that is already stored, without processing the request again during the replay.
How to use it
Add an Idempotency-Key header to your POST request:
POST /public/v1/deliverability/score HTTP/1.1
Host: api.captaindns.com
Authorization: Bearer cdns_live_a3f2XK7mN9QrVtZ4yP1sH6eL8cF2dB5aR3gW7kJxM
Content-Type: application/json
Idempotency-Key: 550e8400-e29b-41d4-a716-446655440000
{"email":"contact@captaindns.com"}
The key must be an opaque string between 1 and 255 printable ASCII characters without whitespace. UUIDv4 or ULID values are recommended as best practice (sufficient entropy, collisions statistically impossible), but any unique client-side identifier works.
What happens server-side
On the first request with a key:
- CaptainDNS computes the SHA-256 of the body and looks up a record for this API key and this idempotency key.
- If no record is found, the processing runs. Its HTTP status and response body are captured while they are sent to the client.
- After the processing, CaptainDNS tries to store this response asynchronously, even if it is an error response.
On a new request with the same key:
- If a record is available and the body hash matches, CaptainDNS returns the stored HTTP status and body, with the
X-Idempotent-Replay: trueheader. - If a record is available but the hash differs, the API returns
409 IDEMPOTENCY_CONFLICT. - If no record is found, the processing can run again.
When a replay is served, the underlying processing does not run: the HTTP status and the body of the stored response are returned with the X-Idempotent-Replay: true header. The usage tracking middleware adds no new entry to the request log. However, credits are consumed and the per-key token bucket is applied before the idempotency lookup. A replay therefore consumes the endpoint's credit cost and a rate limit token, like an ordinary call. Idempotency does not make retries free: plan for their cost in your integration.
Validity window
A stored response is kept for at least 24 hours. A daily purge then removes records past that age: between two purges, an older key can still produce a replay or a conflict. To have the request processed again, use a new key.
This window is a trade-off: too short, you lose the value for delayed retries; too long, you store an ever-growing volume of old responses. 24 hours covers most retry patterns (immediate retry after a network error, retry a few minutes after job recovery, retry scheduled overnight after a monitoring alert).
Methods covered
Idempotency is available on endpoints that enable this mechanism for the POST, PUT, PATCH and DELETE methods. It is not applied to GET or HEAD requests, or to some POST endpoints, including POST /public/v1/ip/whois, POST /public/v1/http/qr and POST /public/v1/http/qr/decode.
The Idempotency-Key header is optional. If you do not send it, the request is processed normally, without replay guarantees.
Exception: POST /public/v1/ip/whois ignores the Idempotency-Key header. Each call runs independently, with no replay or conflict, and costs 2 credits.
Conflicts
A 409 IDEMPOTENCY_CONFLICT is triggered when:
- You reuse an idempotency key with a request body that differs, even by one character.
- This can happen if your client regenerates the request between two attempts (timestamps, embedded UUIDs, JSON key ordering).
To avoid conflicts:
- Compute the idempotency key before serializing the body, not after.
- Do not mix different calls under the same key.
- If your framework reorders JSON keys, fix the order manually.
In case of a legitimate conflict (you really want to replay a different operation), generate a new key.
Usage examples
Retry after a network timeout
idempotencyKey = uuid()
for attempt in 1..3:
try:
response = post(url, body, headers={"Idempotency-Key": idempotencyKey})
return response
except TimeoutError:
sleep(2 * attempt)
raise
Every retry uses the same key and the same body. A previous response can only be replayed if its record is already available. If the previous request is still in progress or its response is not stored yet, a new attempt can run the processing again. Credit and rate limit checks still apply.
Deduplicate inside a job queue
job = fetch_next_job()
idempotencyKey = hash(job.id)
response = post(url, job.payload, headers={"Idempotency-Key": idempotencyKey})
mark_job_done(job.id, response)
If the queue redelivers a job after a crash, reuse the same key and the same body. If the previous response is already stored and available, the API returns it without processing the request again. This new attempt still consumes the endpoint's credit cost and a rate limit token.
Batch with server-side dedupe
for domain in domains:
idempotencyKey = "batch-" + run_id + "-" + domain
post(url, {"domain": domain}, headers={"Idempotency-Key": idempotencyKey})
If the batch is relaunched after a failure, keep the same run_id, the same keys and the same bodies. For each response already stored and available, the API serves a replay without processing the request again. Replayed calls also consume the endpoint's credit cost and a rate limit token.
Limitations
- 24-hour window: beyond that, the record may have been purged and the replay may no longer be available. Very delayed jobs must store responses on the client side.
- Scoped to the API key: the same idempotency key used by two different API keys does not collide. That is by design: every key has its own namespace.
- No inter-server coordination: if you use multiple distinct backends, they share the same API but must coordinate idempotency keys between themselves.
- Strict body identity: an extra whitespace breaks the hash and triggers a conflict. Serialize deterministically.
- Concurrent requests: the key does not reserve an operation in progress. As long as no response is stored, several requests with the same key can run the processing. Avoid concurrent attempts for the same operation.
Next steps
Continue with the error guide to understand how to react to 409 IDEMPOTENCY_CONFLICT, or consult the OpenAPI reference to check each endpoint's parameters and conditions of use.