Idempotency-Key
header with your request, our API will ensure that multiple requests with the same key produce the same result.
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
- Automatic Cleanup: Idempotency keys expire after 24 hours
Supported Operations
Supported Operations
Idempotency is supported on all POST endpoints that create or modify resources:
- API Keys: Create API keys
- Domains: Create domains
- Messages: Send messages and cancel scheduled messages
- Account Members: Add team members
- Suppressions: Create email suppressions
- Routes: Create message routes
- Webhooks: Create webhook endpoints
- SMTP Credentials: Create SMTP credentials
Key Requirements
Key Requirements
- Header Name:
Idempotency-Key
- Key Format: Any string up to 255 characters
- Uniqueness: Keys are scoped to your account
- Expiration: Keys expire after 24 hours
- Request Method: Only POST requests support idempotency
Request Matching
Request Matching
Requests are matched based on:
- Account ID
- Idempotency key
- Request body content (SHA256 hash)
- Request path
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 with any POST request:
Response Behavior
The API responds differently based on the idempotency key status:- First Request
- Replayed Response
- Concurrent Request
- Failed Original
Status:
200 OK
(or appropriate success status)Headers:- 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
Implement exponential backoff when retrying requests. Always use the same idempotency key for retries.
Key Expiration
Keys expire after 24 hours. Don’t reuse keys after this period as the behavior is undefined.
Error Handling
Handle different response codes appropriately:
409
: Wait and retry (concurrent request)412
: Don’t retry (original failed)2xx
withIdempotent-Replayed: true
header: Success (replayed)
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 a moment and retry with the same key. The second request will either get a
409
again (still processing) or the final result once complete.Response
Original Request Failed (412 Precondition Failed)
Original Request Failed (412 Precondition Failed)
This happens when you retry a request whose original attempt failed.What it means: The first request with this idempotency key encountered an error and cannot be safely retried.What to do: Use a new idempotency key if you want to retry the operation.
Response
Key Mismatch
Key Mismatch
If you use the same idempotency key with different request data, the API will treat it as a new request.What it means: The request body, path, or other parameters don’t match the original request.What to do: Ensure you’re using the exact same request parameters when retrying, or use a different idempotency key for different requests.
Implementation Details
The following technical details are provided for transparency but are not required for basic usage.
Request Hashing
The API uses SHA256 hashing of the request body to detect changes between requests with the same idempotency key. This ensures that different requests don’t accidentally match.Concurrency Protection
PostgreSQL advisory locks prevent race conditions when multiple requests with the same idempotency key arrive simultaneously. The first request proceeds normally while subsequent requests wait.Storage Duration
Idempotency records are automatically cleaned up after 24 hours. This prevents the idempotency table from growing indefinitely while providing a reasonable retry window.For high-volume applications, consider implementing client-side deduplication in addition to server-side idempotency to reduce unnecessary API calls.