Idempotency
Safely retry email sends and prevent duplicates with idempotency keys
What is idempotency?
Idempotency ensures that retrying a request (due to network issues, timeouts, etc.) will not result in duplicate emails.
Usage
- Header:
Idempotency-Key
- Type: Any unique string (UUID v4 recommended)
- Scope: Each unique key + request body is valid for 24 hours
How it works
- If you send the same request with the same
Idempotency-Key
within 24 hours, you’ll get the same response and no duplicate email is sent. - If you send a different request body with the same key, you’ll get a
422
error.
Examples
Send an email
Retry with the same key and body
If you retry the exact same request, you will receive a 202 Accepted
with the same message_id
and no duplicate email will be sent.
Conflict with same key, different body
Executing the above calls after the first example will result in a 422 Unprocessable Entity
response with:
SMTP
If you need idempotency but are using the SMTP relay, you can simply add the header to the SMTP request and we’ll handle the request similar to the API.
Example
You can use the following example for nodemailer
:
Recommendations
- Always set
Idempotency-Key
when retries are possible. - Generate a new key for each unique email.
- Store the key if you need to retry the same request.
- Do not reuse keys for different emails.
Summary:
Add the Idempotency-Key
header to your /send
requests to prevent duplicate emails when retrying. Use a unique key per email. Identical key + body will be safe to retry; identical key + different body results in an error error.