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 -.

HTTP
POST /v1/messages HTTP/1.1
Authorization: Bearer ems_...
Content-Type: application/json
Idempotency-Key: booking-4821-confirmation
You sendThe API answers
A key it has not seen for this tokenAccepts the message normally: 202.
The same key and the same body againThe 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 body422 idempotency_key_reuse.
Two identical requests at the same momentOne 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-1
  • password-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#

AnswerRetry?How
No answer: timeout, connection reset, DNS failureYesWith the same key, after a short back-off.
429 rate_limitedYesAfter the seconds in Retry-After (at most a minute).
429 daily_cap_reached, api_budget_reachedLaterRetry-After points to 00:00 UTC. Put the message back in your own queue instead of waiting in a request.
500, 503YesWith the same key, after Retry-After if present, otherwise back-off.
400, 413, 415, 422NoThe request itself is wrong; fix it.
401, 403, 404NoRetrying 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.

JavaScript
// 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)));
}

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.