How to Retry Failed API Requests Without Sending Duplicate Messages
By Nimana · Last updated November 16, 2026 · 3 min read
A timeout should never mean a customer gets the same message twice. Learn how idempotency keys let you retry failed API requests safely, with a code example.
Why This Matters
You send a message through an API. Before the response arrives, your connection drops. Did the message go out or not?
If you retry blindly, the customer might get two copies of the same order confirmation or login code. If you do not retry, they might get none. Both are bad, and both happen more often than you would expect, because networks fail all the time.
The fix is simple: make your requests safe to repeat.
What Idempotency Means
An idempotent request is one you can send many times with the same result. Sending it five times has the same effect as sending it once. For messaging, that means one message, not five.
How an Idempotency Key Works
Most well-designed APIs support it the same way:
- You create a unique key for each message you want to send, usually a random UUID.
- You send it with the request, typically in a header called
Idempotency-Key. - The API remembers the key and the response for a period of time.
- If you send the same key again, the API returns the saved response instead of repeating the action.
So if a timeout happens, you retry with the same key. If the first request already went through, you get the original result back. If it did not, the request runs normally.
A Code Example
The most important detail is to create the key once and reuse it on every retry:
const { randomUUID } = require('crypto');
async function sendMessage(body, attempts = 3) {
const idempotencyKey = randomUUID(); // create once, reuse on every retry
for (let i = 0; i < attempts; i++) {
try {
const res = await fetch('https://api.example.com/v1/messages', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.API_KEY}`,
'Content-Type': 'application/json',
'Idempotency-Key': idempotencyKey,
},
body: JSON.stringify(body),
});
return await res.json();
} catch (err) {
// Network problem: wait, then retry with the SAME key
await new Promise((r) => setTimeout(r, 1000 * 2 ** i));
}
}
throw new Error('Could not reach the API');
}
Six Tips for Safe Retries
- Create the key once per message, not once per attempt. A new key on every retry defeats the whole idea.
- Use a random UUID. It is unique, easy to generate, and hard to clash.
- Store the key with the message record. If your app crashes mid-request, you can restart and retry with the same key.
- Never reuse a key for a different message. Most APIs reject the request when the same key arrives with a different body.
- Retry only the right errors. Network failures and
5xxerrors are worth retrying. A400or401will fail every time. - Wait between retries. Doubling the delay each time (1, 2, 4 seconds) is kinder to the API and more likely to succeed.
Common Mistakes
- Generating a fresh key inside the retry loop. The API sees each attempt as a new message.
- Assuming keys never expire. Keys are usually remembered for a limited time, so do not rely on them days later.
- Using idempotency keys on every method. They matter for
POSTrequests that create something.GET,PUT, andDELETEare naturally safe to repeat.
How SendAPI Helps
SendAPI supports exactly this pattern, so you do not have to build it yourself:
- The
Idempotency-Keyheader works on allPOSTendpoints, across WhatsApp, SMS, OTP, and email - Responses are saved for 24 hours, so a retry returns the original result instead of sending again
- A reused key with a different body returns a
409 Conflict, which makes mistakes easy to spot - The official SDKs add the header automatically, so you get safe retries without extra code
All of this comes with WhatsApp, SMS, OTP, and email under one API from $9.99 a month.