Idempotency API Best Practices Error Handling Developer Guide

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:

  1. You create a unique key for each message you want to send, usually a random UUID.
  2. You send it with the request, typically in a header called Idempotency-Key.
  3. The API remembers the key and the response for a period of time.
  4. 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

  1. Create the key once per message, not once per attempt. A new key on every retry defeats the whole idea.
  2. Use a random UUID. It is unique, easy to generate, and hard to clash.
  3. Store the key with the message record. If your app crashes mid-request, you can restart and retry with the same key.
  4. Never reuse a key for a different message. Most APIs reject the request when the same key arrives with a different body.
  5. Retry only the right errors. Network failures and 5xx errors are worth retrying. A 400 or 401 will fail every time.
  6. 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 POST requests that create something. GET, PUT, and DELETE are naturally safe to repeat.

How SendAPI Helps

SendAPI supports exactly this pattern, so you do not have to build it yourself:

  • The Idempotency-Key header works on all POST endpoints, 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.