Guides
Retries and idempotency
Networks fail in the worst place: after the API accepted your message but before you saw the answer. An Idempotency-Key turns "did it go through?" into a question you can simply ask again.
The header#
Send an Idempotency-Key with every POST /v1/messages: 1 to 128 letters, digits, ., _, : or -.
POST /v1/messages HTTP/1.1
Authorization: Bearer ems_...
Content-Type: application/json
Idempotency-Key: booking-4821-confirmation| You send | The API answers |
|---|---|
| A key it has not seen for this token | Accepts the message normally: 202. |
| The same key and the same body again | The original answer: 202 with the first message's id and status and the header Idempotent-Replayed: true. Nothing is sent again and no limit is used. |
| The same key with a different body | 422 idempotency_key_reuse. |
| Two identical requests at the same moment | One message; both requests get its id. |
Keys belong to the token: two tokens can use the same key without meeting. A key is remembered for as long as the message record is kept.
"Same body" means the same message after normalisation: addresses compared in lowercase, fields in any order.
Choosing keys#
Build the key from something that identifies the message in your own system and does not change when you retry:
booking-4821-confirmation,booking-4821-reminder-1password-reset-7f3a9c(the reset request id)invoice-2026-0193-sent
Do not use the time, a random value generated on each attempt, or the recipient alone: those either change on retry or collide between different messages.
Which errors to retry#
| Answer | Retry? | How |
|---|---|---|
| No answer: timeout, connection reset, DNS failure | Yes | With the same key, after a short back-off. |
429 rate_limited | Yes | After the seconds in Retry-After (at most a minute). |
429 daily_cap_reached, api_budget_reached | Later | Retry-After points to 00:00 UTC. Put the message back in your own queue instead of waiting in a request. |
| 500, 503 | Yes | With the same key, after Retry-After if present, otherwise back-off. |
| 400, 413, 415, 422 | No | The request itself is wrong; fix it. |
| 401, 403, 404 | No | Retrying cannot help; check the token, permission, sender or id. |
A sensible back-off: wait about 1, 2, 4 and 8 seconds with some randomness, then give up and keep the message in your own queue. Never retry in a tight loop.
// Retry with the same key; honour Retry-After; give up after four attempts.
for (let attempt = 1; attempt <= 4; attempt++) {
const res = await post(message, { 'Idempotency-Key': key }).catch(() => null);
if (res?.ok) return res.json();
const retryable = !res || [429, 500, 502, 503, 504].includes(res.status);
if (!retryable || attempt === 4) throw new Error(`gave up: ${res?.status ?? 'network'}`);
const wait = Number(res?.headers.get('retry-after')) * 1000 || 500 * 2 ** attempt;
await new Promise((r) => setTimeout(r, wait * (0.5 + Math.random() / 2)));
}# Retry with the same key; honour Retry-After; give up after four attempts.
for attempt in range(1, 5):
try:
res = post(message, headers={"Idempotency-Key": key})
except requests.RequestException:
res = None
if res is not None and res.ok:
return res.json()
retryable = res is None or res.status_code in (429, 500, 502, 503, 504)
if not retryable or attempt == 4:
raise RuntimeError(f"gave up: {res.status_code if res is not None else 'network'}")
wait = int(res.headers.get("Retry-After", 0)) if res is not None else 0
time.sleep((wait or 0.5 * 2 ** attempt) * random.uniform(0.5, 1))The client modules already do this.
Retries after the answer#
Once the API has accepted a message, retrying is the delivery worker's job, not yours. If the mail system is unavailable it tries again after 30 seconds, 2 minutes, 10 minutes, 1 hour, 6 hours, then marks the message dead. You never need to send the message again to make that happen.
The worker records every hand-off, so a second delivery of the same message is refused. The one case it cannot rule out is a hand-off whose confirmation was lost on the way back (a timeout at exactly the wrong moment). The next attempt may then give the mail system a second copy. This is rare; it is the price of never losing a message.
Last reviewed 2026-10-01.