> ## Documentation Index
> Fetch the complete documentation index at: https://docs.routa.chat/llms.txt
> Use this file to discover all available pages before exploring further.

# Idempotency

> Use the Idempotency-Key header to retry a send safely, without creating duplicate messages.

A network failure after a send leaves you not knowing whether the message was created. Resending carelessly can deliver **two messages** to the same customer, which is the worst kind of bug in messaging. The `Idempotency-Key` header solves this: Routa guarantees that the same key produces at most one message.

## Where it applies

| Endpoint | Idempotency |
| - | - |
| `POST /v1/messages` | **Supported.** Recommended on every send. |
| Other endpoints | Do not deduplicate by `Idempotency-Key`. |

> [!NOTE]
>
> The other write endpoints are designed to be safe to repeat (`disable` and `enable`, for example), but they do not use the header. If you need to avoid retries in those cases, set `maxRetries: 0` in the SDK.

## How to use it

Generate a unique key per **logical** message (a UUID v4, for example) before the first attempt, and send the same key on every retry.

```bash cURL theme={null}
curl https://api.routa.chat/v1/messages \
  -H "Authorization: Bearer $ROUTA_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: 6f0a9d9e-3c1f-4e56-9a64-2b7f6a5c1d10" \
  -d '{ "channel": "chan_01J8...", "to": "+5581999999999", "text": "Hi!" }'
```

```ts Node.js theme={null}
// The SDK generates a key automatically and reuses it on every retry.
await routa.messages.send({
  channel: 'chan_01J8...',
  to: '+5581999999999',
  text: 'Hi!',
})

// To control the key, for example by deriving it from an ID in your domain:
await routa.messages.send({
  channel: 'chan_01J8...',
  to: '+5581999999999',
  text: 'Hi!',
  idempotencyKey: `order-1234-confirmation`,
})
```

> [!TIP]
>
> Derive the key from a stable ID in your domain (such as the order ID and the kind of notice). That way, even reprocessing your own job does not duplicate the message.

## What happens in each case

| Situation | Result |
| - | - |
| First time the key is used | The message is created. The response is stored. |
| Same key, same body, already finished | The **original response** is returned, with the `Idempotent-Replay: true` header. |
| Same key, same body, still in progress | `409 idempotency_in_progress`, with `Retry-After`. Try again. |
| Same key, **different body** | `409 idempotency_key_reuse`. |
| Key older than 24 hours | Treated as a first use. |

In the SDK, when the call returns the result of an earlier attempt, `message._replayed` is `true`.

## Scope and lifetime

* The key is valid **within the project**, for this operation. Keys of different projects never collide.
* The record is kept for **24 hours**. After that, repeating the key runs the operation again. The window is for retries, not for permanent deduplication.
* The body comparison ignores key order and whitespace in the JSON.

> [!WARNING]
>
> Do not reuse one `Idempotency-Key` for different messages. One key per logical message, always.

## Limits of the guarantee

Idempotency protects the **API boundary**: an accepted request produces exactly one message in Routa. In the next step, between Routa and WhatsApp, a failure with an unknown outcome (such as a timeout) is handled with a deterministic reference sent to the provider for deduplication, and with a reconciliation query before any new attempt. Even so, *exactly once* delivery does not exist against a third-party API: the documented guarantee is **at least once**, and Routa makes duplicates rare, not impossible. See [Delivery guarantees](https://docs.routa.chat/en/reliability/delivery-guarantees.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.