Docs / Concepts / Idempotency

Idempotency

Mailgrid deduplicates retried requests automatically. Send the same email twice — get one delivery.

How it works

Before each send, Mailgrid computes an idempotency key from a stable hash of the request payload:

key derivation
key = sha256(tenant_id ⊕ scope ⊕ client_key? ⊕ sorted(payload))

If a row with that key already exists in the idempotency table:

Otherwise we claim the key, perform the send, and write the final result back.

Custom idempotency keys

For cases where the payload is identical but you want distinct sends (or vice versa), pass your own idempotencyKey:

explicit key
{
  "from": "hello@yourdomain.com",
  "to":   "user@example.com",
  "subject": "Welcome",
  "html": "<p>Hi</p>",
  "idempotencyKey": "order-12345"
}

TTL

Idempotency rows live for 24 hours. After that, the same payload sent again will be treated as a new send. A cron job (17 * * * *) purges expired rows.

Response shapes

ScenariostatusHTTP
First send, successsent200
Recipient on suppression listsuppressed200
Duplicate request (cached completion)replayed200
Concurrent duplicate (in-progress)409
Why hash the payload?

So clients that don't set their own idempotency key still get protection. The hash is stable across whitespace and key ordering, so format differences don't break dedup.