Sending

How to retry sends safely with an Idempotency-Key

Short answer

To make Mailcandor sends safe to retry, add an Idempotency-Key header (1 to 256 characters) to POST /v1/emails or POST /v1/emails/batch. For 24 hours, the same key with the same body returns the first response with Idempotency-Replayed: true and sends nothing new. The same key with a different body returns 409. Failed requests are not stored, so you can fix and retry with the same key.

Last checked 16 November 2026

Networks fail. If your request times out, you do not know whether Mailcandor received it. Retrying could send the email twice. An idempotency key removes the guesswork.

To use an idempotency key

Add the header to a send:

curl https://api.mailcandor.com/v1/emails \
  -H "Authorization: Bearer $MAILCANDOR_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: order-1042-receipt" \
  -d '{ "from": "Acme <billing@acme.com>", "to": "sam@example.com", "subject": "Receipt for order 1042", "html": "<p>Thanks!</p>" }'

If you send exactly the same request again within 24 hours, you get the first response back, with the header Idempotency-Replayed: true, and no second email.

It works on POST /v1/emails and POST /v1/emails/batch.

The rules

Situation Result
New key Sent as normal. The response is stored for 24 hours.
Same key, same body, first one finished The stored response, plus Idempotency-Replayed: true. Nothing sent.
Same key, same body, first one still running 409 concurrent_idempotent_requests. Retry in a moment.
Same key, different body 409 invalid_idempotent_request. Use a new key for a new email.
First request failed (any non-2xx) Nothing is stored. Fix the request and retry with the same key.
Empty key or over 256 characters 400 invalid_idempotency_key.
More than 24 hours later The key is forgotten; the request sends again.

“Same body” means byte for byte. Rebuild the JSON the same way on retry, or store the exact body you sent.

A retry loop in Node.js

async function sendOnce(email, key) {
  for (let attempt = 1; attempt <= 4; attempt++) {
    try {
      const res = await fetch('https://api.mailcandor.com/v1/emails', {
        method: 'POST',
        headers: {
          Authorization: `Bearer ${process.env.MAILCANDOR_KEY}`,
          'Content-Type': 'application/json',
          'Idempotency-Key': key,
        },
        body: JSON.stringify(email),
      });
      if (res.ok) return res.json();
      if (res.status === 409 || res.status === 429 || res.status >= 500) {
        await new Promise((r) => setTimeout(r, attempt * 1000));
        continue;
      }
      const { error } = await res.json();
      throw new Error(`${error.type}: ${error.message}`);
    } catch (err) {
      if (attempt === 4) throw err;
      await new Promise((r) => setTimeout(r, attempt * 1000));
    }
  }
}

Retry on timeouts, 429 and 5xx. Do not retry 4xx validation errors unchanged: fix the request first.

Choosing good keys

  • Tie it to the event: order-1042-receipt, invite-88-sent. The same event always makes the same key.
  • Not just the recipient: two different emails to the same person need different keys.
  • For batches: one key per batch, such as digest-2026-11-16-part-3.

Over SMTP

Add the header to the message: Mailcandor-Idempotency-Key: order-1042-receipt. The relay passes it on as the API’s Idempotency-Key.

If it does not work

  • 409 invalid_idempotent_request: the body changed between attempts, often because of a timestamp or random value in the JSON. Keep the body identical, or use a new key if it really is a new email.
  • 409 concurrent_idempotent_requests: your retry arrived while the first was still being handled. Wait a second and retry.
  • 400 invalid_idempotency_key: the header is empty or longer than 256 characters.
  • Duplicates still arrive: check that every retry sends the header, and that the key does not change between attempts.

Questions

What should I use as the idempotency key?

Something unique to the email that your code can rebuild on retry, such as order-1042-receipt or user-77-password-reset-2026-11-16T10:00. A random UUID works if you store it before the first attempt.

How long are keys remembered?

24 hours. After that the same key can send again.

Does a key from one team affect another team?

No. Keys are kept per team.

Does idempotency work over SMTP?

Yes. Add a Mailcandor-Idempotency-Key header to the message itself.

Related

Send with someone on your side.

Unlimited domains on every plan, a person before any block, and 3,000 emails a month free. Tell us what you send and we set you up within one business day.