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

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.
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.
