Idempotency-Key to replay a stored result when you retry the same request. A request with a key does its operation at most one time while the key is valid. A key is valid for 24 hours. A retry with the same key and the same request after a server error, a timeout or a lost answer does not send a second message or create a second resource.
Available on Free, Pro and Max. See plans and features.
For email sent over SMTP, use the ahasend-idempotency-key header.
Idempotency is particularly important for critical operations like sending emails, creating resources, or processing payments where duplicate actions could cause problems.
How it works
When you make a request with anIdempotency-Key header:
- First Request: The API processes your request normally and stores the response
- Subsequent Requests: If you retry with the same key, the API returns the stored response instead of processing the request again
- Server Errors Are Retriable: If the original attempt failed with a server error (HTTP 5xx), no response is stored. When it failed, timed out or you lost its answer, retry with the same key and the same request: the operation happens at most one time, and the retry gets its result
- Automatic Cleanup: Stored idempotency responses expire after 24 hours. The exception is API key creation: because its response carries a one-time
secret_key, the replayable response expires after 5 minutes
Supported Operations
Supported Operations
Only the operations in the Supported operations table declare the idempotency header. These cover:
- API Keys: Create API keys
- Domains: Create domains
- Messages: Send messages, including transactional templates
- Templates: Create, update and publish templates, and restore template versions
- Campaign Templates: Create and update campaign templates
- Campaigns: Create, update and duplicate campaigns, and send, unschedule and test them
- Account Members: Add team members
- Suppressions: Create email suppressions
- Routes: Create message routes
- Webhooks: Create webhook endpoints
- SMTP Credentials: Create SMTP credentials
- Sub-Accounts: Create sub-accounts
- Sub-Account API Keys: Create sub-account API keys
One-Time Secrets (API Key Creation)
One-Time Secrets (API Key Creation)
API-key creation endpoints — Create API Key and Create Sub-Account API Key — return a one-time
secret_key in the response.Their idempotent replay response is encrypted and expires after 5 minutes (instead of the usual 24 hours). Within that window, an exact retry with the same Idempotency-Key replays the same secret_key. After it, the secret can no longer be retrieved — if you lose it, create a new key.Other operations that declare idempotency store responses for 24 hours.Key Requirements
Key Requirements
- Header Name:
Idempotency-Key - Key Format: Any string up to 255 characters
- Uniqueness: Keys are scoped to your account
- Expiration: Stored responses expire after 24 hours — except API key creation, whose secret-bearing response expires after 5 minutes
- Request Method: Only the operations listed below support idempotency; other operations do not gain it implicitly
Request Matching
Request Matching
Requests are matched based on:
- Account ID
- Idempotency key
- Request method and path, including path parameters
- Request body content (SHA256 hash)
422 Unprocessable Entity.Supported operations
A fresh execution has no
Idempotent-Replayed header. A completed replay returns the original status and body with Idempotent-Replayed: true. An in-progress request returns 409, Idempotent-Replayed: false, and a positive integer Retry-After. A mismatched request returns 422 with neither of these headers. The API has no 412 failed-key response.
Key selection best practices
A client generates an idempotency key, which is a unique key that the server uses to recognize subsequent retries of the same request. How you create unique keys is up to you, but we suggest using V4 UUIDs, or another random string with enough entropy to avoid collisions. Idempotency keys are up to 255 characters long.Key generation examples
Key Collision Risk: With V4 UUIDs, the probability of generating duplicate keys is approximately 1 in 5.3 x 10^36. For practical purposes, this is negligible even at massive scale.
Usage example
Include theIdempotency-Key header only on an operation that declares it:
Response behavior
The API responds differently based on the idempotency key status:- First Request
- Replayed Response
- Concurrent Request
- Failed Original
- Payload Mismatch
Status:
202 Accepted for Create Message, Create Template Message, Create Conversation and Send Campaign, 201 Created for resource creation, and 200 OK for other operations, such as Update TemplateHeaders:- Standard response headers
- No special idempotency headers
Best practices
Unique Keys
Use unique, descriptive keys that won’t conflict with other operations. Consider including timestamps or UUIDs.
Retry Logic
Retry with the same idempotency key and the same request when you do not know the outcome, or the answer was not stored, as the Failed Original tab under Response behavior describes. Examples are a network error, a
5xx, a rate-limit 429 (see API rate limits), and a 409 that is still in progress. Before each retry, wait: for the Retry-After delay when the answer has one, or with exponential backoff when it has none. A stored answer is replayed, so a retry of a stored 4xx with the same key gets the same answer. The snippet below handles the in-progress 409; the Error Handling card lists how to treat the other 409 answers.Pass a makeRequest(key) callback that sends the same body each time and, on failure, throws an object with the HTTP status and the response headers (a Headers object).Key Expiration
A key lives 24 hours from its first use. After that, a request with the same key is a new request, so don’t reuse a key after this period. API key creation is shorter: its one-time
secret_key is only replayable for 5 minutes.Error Handling
Handle different response codes appropriately:
409withIdempotent-Replayed: falseandRetry-After: request in progress — wait for theRetry-Afterinterval, then retry with the same key- Any other
409: a conflict with the state of the resource — do not retry with the same key; after you resolve the conflict, use a new key 422: Key reused with a different request — use a new idempotency key, or retry with the original payload and endpoint5xx: Transient failure — wait for theRetry-Afterdelay, or with backoff when there is none, then retry with the same idempotency key2xx/4xxwithIdempotent-Replayed: trueheader: replayed original outcome
Error scenarios
Request Already in Progress (409 Conflict)
Request Already in Progress (409 Conflict)
This happens when you make concurrent requests with the same idempotency key.What it means: Another request with the same key is currently being processed.What to do: Wait for the interval indicated by the
Retry-After response header, then retry with the same key. The retry gets a 409 again (still processing) or the stored result once complete. If the original request never finished (for example, it was interrupted), the retry goes ahead after at most 5 minutes, and the operation still happens at most one time.Response
Original Request Failed with a Server Error
Original Request Failed with a Server Error
If the original request failed with an HTTP
5xx error, no response is stored for the idempotency key.What it means: The failure is treated as transient, and the key remains usable.What to do: Wait for the Retry-After delay when the response has one, or with exponential backoff when it has none, then retry with the same idempotency key and the same request. The operation happens at most one time, so the retry does not send a second message or create a second resource. (Other answers can be stored. The Failed Original tab under Response behavior lists what is stored and the exceptions.)Key Mismatch
Key Mismatch
If you use the same idempotency key with different request data — or with the same data against a different endpoint or resource — the API returns
422 Unprocessable Entity.What it means: The request doesn’t match the original request for that key.What to do: Ensure you’re using the exact same request (payload and endpoint) when retrying, or use a different idempotency key for different requests.Response
Implementation details
Request hashing
The API uses SHA256 hashing of the request method, path, and body to detect changes between requests with the same idempotency key. This ensures that different requests don’t accidentally match — including the same payload sent to a different resource.Concurrency protection
Claiming an idempotency key happens atomically; While a request is in flight, concurrent requests with the same key receive409 Conflict with a Retry-After header instead of waiting.
Execution leases
An in-flight request holds its idempotency key for up to 5 minutes, and another request with the key and the same body gets409 during that time. If the original request never completes (for example, it was interrupted), the key unblocks automatically after that time, and the next retry with the same request goes ahead. The operation still happens at most one time for the key.
Storage duration
A key lives 24 hours from its first use, and its stored result expires with it; successful API-key creation results expire after 5 minutes. After expiry, a request with the same key is a new request. Responses that contain a one-time secret — API key creation and sub-account API key creation — are stored encrypted and their replayable response expires after 5 minutes. Within that window an exact retry replays the samesecret_key; afterward the secret can no longer be returned.

