Before continuing, make sure you’ve completed the SDK setup and requirements.
Overview
Network issues, timeouts, and client crashes can cause API requests to fail without a clear response. When this happens, you may not know if the email was actually sent. Retrying the request risks sending the same email twice. Idempotency keys solve this problem. By including anIdempotency-Key header in your request, you tell the API to process the request only once. If you retry with the same key, the API returns the original response instead of sending the email again.
How it works
- You generate a unique key and include it in the
Idempotency-Keyheader. This can be a technical identifier like a UUID, or a business identifier such as an external customer ID or an order reference (e.g.,order-12345). - The API processes the request and stores the response associated with that key.
- If you send the same request again with the same key, the API returns the stored response without reprocessing.
- Keys expire after 24 hours. After that, the same key can be reused.
Send an email with an idempotency key
With the SDK
With the REST API
Bulk emails
For bulk sending, the idempotency key applies to the entire bulk request, not to individual emails within it. If you retry a bulk request with the same key, the full bulk response (including per-email statuses) is returned from cache.Detecting replayed responses
When the API returns a cached response, it includes theIdempotent-Replayed: true header. The response body and status code are identical to the original response.
You can use this header to distinguish a replayed response from a fresh one in your logs or monitoring.
Key format
| Property | Value |
|---|---|
| Minimum length | 8 characters |
| Maximum length | 255 characters |
| Recommended format | UUID v4 |
| Case sensitive | Yes |
order-12345.
Error handling
The API returns specific error codes for idempotency-related issues.Invalid key
Returned when the key is empty, too short, or too long.Key already in progress
Returned when a request with the same key is still being processed. Wait a moment and retry.Payload mismatch
Returned when you reuse a key with a different request body. Each key is bound to the exact request body that was sent with it. Use a new key for different requests.Behavior on errors
How the API handles idempotency depends on the type of error from the original request:- Client errors (4xx): The error response is cached. Retrying with the same key returns the same error. You need to fix the issue and use a new key.
- Server errors (5xx): The error response is not cached. You can safely retry with the same key, and the API will process the request again.
Best practices
- Generate a new unique key for each distinct email you want to send
- Store the key alongside the operation in your system so you can retry with the same key if needed
- Use UUIDs or similar random identifiers to avoid accidental collisions
- The key is optional. If you do not need retry safety, you can omit the header entirely
