Idempotency

A request can time out after Sendfern has already queued the email. Send an Idempotency-Key and you can retry it as often as you need: the email goes out once.

Send a key

Add an Idempotency-Key header to POST /v1/emails or POST /v1/emails/batch. The key is any string you choose, up to 200 characters. A longer key is a 400.

curl https://api.sendfern.com/v1/emails \
  -H "Authorization: Bearer $SENDFERN_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: order-1042-shipped" \
  -d '{
    "from": "Acme <hello@acme.com>",
    "to": "user@example.com",
    "subject": "Your order has shipped",
    "text": "Your order #1042 is on its way."
  }'

It works the same way for a batch, where one key covers the whole request:

curl https://api.sendfern.com/v1/emails/batch \
  -H "Authorization: Bearer $SENDFERN_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: weekly-report-2026-11-28" \
  -d '[{"from": "Acme <hello@acme.com>", "to": "jane@example.com", "subject": "Your weekly report", "text": "..."}]'

Only these two endpoints use the header. Other requests ignore it, including resend, so every resend sends a new copy.

What happens on a retry

Sendfern stores the key with a fingerprint of the request body. When the same key comes back:

RequestResult
Same key, same bodyThe first response is returned again, with the same status and body and the header Idempotent-Replayed: true. No new email is created.
Same key, different body409 idempotency_conflict. Nothing is sent.
Same key, first request still running409 idempotency_in_progress. Wait a moment and retry.
Response · 201 Created (replayed)
HTTP/1.1 201 Created
Content-Type: application/json
Idempotent-Replayed: true

{"id": "em_VNqV5JEIqYYeYbZRhbPrJ6", "object": "email", "status": "queued", ...}

"Same body" means the same JSON: the order of keys in an object does not matter, but any changed value does. A replay returns the email as it was when first created, so its status may be out of date. Fetch the email for its current state.

Response · 409 Conflict
{
  "error": {
    "type": "idempotency_error",
    "code": "idempotency_conflict",
    "message": "This Idempotency-Key was already used with a different request body."
  }
}
Response · 409 Conflict
{
  "error": {
    "type": "idempotency_error",
    "code": "idempotency_in_progress",
    "message": "A request with this Idempotency-Key is still in progress."
  }
}

Errors are not stored

Sendfern keeps a response only when the request succeeds. If the first request fails, for example with a 400 for an invalid address or a 403 for an unverified domain, the key is released. Fix the problem and retry with the same key; that request runs as if it were the first.

Scope

  • Keys belong to your account. Another account using the same key does not affect yours.
  • Live and test mode have separate keys, so a key you used with a test key is new to a live key.

How long keys are kept

Sendfern keeps each key for 24 hours after the first request.

Retries in the SDKs

The Node.js and Python SDKs retry network errors, 429 responses and 5xx responses with backoff, twice by default. They wait as long as Retry-After says when the API sends it.

A POST without an idempotency key is never retried after a network error or a 5xx, because the first attempt may have queued the email. Only a 429 is retried, since a throttled request did nothing. Pass a key (idempotencyKey in Node.js, idempotency_key in Python) and sends are retried like any other request. Requests other than POST, such as GET, are always retried.

Change the number of retries when you create the client:

const sendfern = new Sendfern(process.env.SENDFERN_API_KEY, { maxRetries: 3 });

Choosing a key

A good key names the thing that should happen once, not the attempt to do it.

  • Derive it from your own data. order-1042-shipped or user-381-welcome stays the same across retries, process restarts and duplicate jobs from a queue.
  • Or generate a random one once. If nothing in your data identifies the send, create a UUID, store it with the job, and reuse it on every retry of that job.
  • Never generate it per attempt. A new UUID on each retry protects nothing: every attempt looks like a new request.
  • Use a new key for a new email. Reusing a key with a different body is a 409, and reusing it with the same body sends nothing new.

See Errors for the idempotency_error type and Rate limits and quotas for 429.