SendpositoryDocs
Guides

Idempotency

Make retries safe with an idempotency key, so a timeout on a send that actually succeeded never mails the same person twice.

Updated

Networks fail after the work is done. Your request reaches us, we send the email, and the response is lost on the way back. Your code sees a timeout and retries - and the customer gets two one-time passcodes.

An idempotency key prevents that.

How to use it

Send an Idempotency-Key header with any request you might retry:

curl -X POST https://api.sendpository.com/v1/emails \
  -H "Authorization: Bearer $SENDPOSITORY_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: order-10428-receipt" \
  -d '{ "from": "...", "to": ["..."], "subject": "...", "html": "..." }'

Replaying the same key on the same account returns the original message id instead of sending again:

{ "id": "d67e39ed-96be-4b35-ab22-853163d92c92" }

Same response, no second email.

What a replay actually returns

A replay reports what happened to the original attempt, not a blanket success:

Original messageReplay gets
Scheduled, provider-accepted, sent or delivered200 with the original id
Still dispatching409 idempotency_in_progress with Retry-After; keep the same key
Transport outcome cannot be established503 send_outcome_unknown; check delivery and keep the same key
Failed to send409 idempotency_original_failed, naming the failed message
A different payload under the same key409 idempotency_key_reused

A duplicate can briefly wait for the original request. If it is still dispatching, retry after the indicated delay with the same key and body. A queued row is not proof that the provider accepted anything.

After an uncertain transport result, do not switch to a new key: the provider may already have accepted the email. Check its timeline in the dashboard and contact support if the outcome remains unknown. We do not automatically dispatch an uncertain operation again. A definite failure also retains its original result; decide whether a new send is appropriate only after checking delivery.

A different body under the same key is refused instead of silently dropping the new message. Requests recorded before the fingerprint migration cannot have their original body fingerprint checked; use a new operation ID for a genuinely new message, while keeping the existing ID for its retries.

Same key means the same message

The key is matched against a fingerprint of the request body. Changing the recipient, subject or body while keeping the key is an error, not an update.

Choosing a key

Derive it from the thing that caused the email, not from the clock:

// Good - stable across retries of the same event.
`welcome-${user.id}`
`receipt-${order.id}`
`otp-${user.id}-${session.id}`

// Bad - different on every retry, so it protects nothing.
`otp-${Date.now()}`
crypto.randomUUID()

A random key defeats the purpose

If the key changes between attempts, each attempt is a new message. The key must be the same on the retry as it was on the original.

Scope

Keys are scoped to your account, so they never collide with another customer's. The deduplication record lasts while the original email row is retained. Deleting it through retention or account deletion removes that history; your application should also deduplicate business events.

Where it matters most

One-time passcodes, password resets and payment receipts. Anything where a duplicate is confusing, alarming, or a security problem.

On this page