What is an idempotency key, and when do you need one?

A request and its retry carrying the same idempotency key, merging into one email that is sent once

An idempotency key is an identifier you generate and attach to a request so the server can recognise a retry of the same operation. Send one with an email, and a repeat of that request inside the dedupe window returns the original result instead of sending a second message. You need one anywhere a retry can happen: job queues, webhook handlers and any send an agent makes.

Unitpost ·

The worst network failure is the one where the request succeeds and the response is lost. The client can't tell that apart from a request that never arrived, so it retries, and an email that should have gone out once goes out twice.

How the key fixes it

You derive a key from the operation and send it in the Idempotency-Key header. The server stores the key with the result. A second request with the same key gets the stored result back and nothing is sent again, so the client can retry without knowing whether the first attempt went through.

cURL
await unitpost.email.send(
  { from, to, subject, html },
  { idempotencyKey: `order-confirmation-${orderId}` },
);

A replay comes back with the header Idempotent-Replayed: true, so you can tell it from a fresh acceptance. If you leave idempotencyKey out, the Node SDK generates one for each send() call. That makes the SDK's own automatic retries safe, but a second send() from your job queue gets a new key and sends again. Pass your own key to cover that.

Choosing a good key

The key has to stay the same across every retry of one logical operation and differ between operations. Derive it from your own data, never from the attempt. Unitpost accepts keys of 1 to 256 characters.

  • Good: order-confirmation-4821, tied to the order being confirmed.
  • Good: password-reset-user-93-1721, tied to the user and the reset request.
  • Bad: a fresh UUID per attempt. Every retry looks like a new operation.
  • Bad: password-reset. It collides across users and across time.

The dedupe window

Keys don't live forever. A 24-hour window is the convention: Stripe uses it, and so does Unitpost. That covers any realistic retry sequence and still lets the same key stand for a new operation later. A retry after the window sends a second message.

Same key, different body

Reusing a key with a different payload is a bug in the caller, and the API answers 409 invalid_idempotent_request instead of guessing. Sending the new message would break the guarantee, and returning the old result would drop mail you meant to send.

What idempotency doesn't solve

An idempotency key stops the same operation from being delivered twice. It doesn't merge two different requests that happen to produce similar mail, and it can't help when your own code decides twice to send. Those are bugs in your own application code.

Frequently asked questions

Is an idempotency key the same as a message ID?

No. You create the idempotency key before the request, and the server creates the message ID after it. The key names the operation you're attempting; the ID names the message that resulted.

Do I need idempotency keys if I never retry?

You probably retry without knowing it. Many HTTP clients retry connection errors by default, job queues retry failed jobs, serverless platforms retry on timeout, and webhook senders deliver at least once. If any of those sits in your send path, retries happen whether you wrote them or not.

What happens if I send the same key twice at the same time?

The second request is rejected with a 409 (concurrent_idempotent_requests) instead of being queued, because the first hasn't produced a result yet. Retry a moment later and you get the stored result. Parallel workers that can pick up the same job are where this shows up.

Send email your users actually receive

5,000 emails a month free — API, SMTP, templates, tracking. No credit card.