# Idempotency (/idempotency)



Every write accepts an optional header:

```http
Idempotency-Key: any-unique-string-per-logical-request
```

Retrying with the same key returns the **first answer** instead of doing the
work again, and that reply carries `Idempotent-Replay: true`.

This matters most for sends. A timeout tells you nothing about whether the
message went out; without an idempotency key, retrying is a coin flip between
losing a message and sending it twice.

A key belongs to your account, not to one endpoint or one API key: the same
key sent to another endpoint, or with another of your keys, is the same key.

## What is remembered [#what-is-remembered]

An answer is remembered once something went out: a message that was sent,
queued or scheduled. Retrying with the same key then returns that answer.

Nothing went out, so nothing is remembered, and retrying with the same key runs
the request again:

* a refused send (`4xx` on `POST /messages` or `POST /messages/batch`), such
  as `insufficient_credits`, `no_sender_available` or `validation_failed`. Top
  up, connect a sender or fix the request, then retry with the same key;
* a `POST /messages` whose every message came back `failed` because of its
  sender: credentials or balance the provider refused, a disconnected device,
  no sender left. Those messages stay in your history, failed and refunded,
  and the retry sends new ones;
* `rate_limit_exceeded` (`429`), which did no work at all;
* a server error (`5xx`) that saved nothing.

A failure that may still have reached the recipient, such as a provider that
timed out after taking the message, stays remembered: retrying returns the
failed message instead of risking a second copy. Refusals on every other
endpoint stay remembered too, because some of them change state, like a wrong
verification code counting against its attempts.

A server error never leaves half the work done. If the message was already
handed over, retrying with the same key returns that message instead of sending
it a second time.

`POST /messages/batch` is always remembered, because its rows can mix messages
that went out with elements that failed, and the ones that went out must not go
out twice. Retry a failed element on its own, with its own key.

## Picking a key [#picking-a-key]

One key per logical request. A UUID per attempt is wrong — it makes every retry
a new request. Derive it from something stable in your own system: the order id,
the row id, the job id.

A key is up to 255 characters; a longer one is refused with
`validation_failed`. It is remembered for at least 24 hours. Retry within that
window; after it, the same key may start a new request.

## Two ways it refuses [#two-ways-it-refuses]

`idempotency_key_reused` (409) means the key was used for a request with a
**different body**. A key stands for one request; reusing it for another hides
a bug rather than preventing one.

`idempotency_request_in_progress` (409) means the first request with that key
has not finished. Wait and retry with the same key — you will get the original
answer. Generating a new key here is exactly what you are trying to avoid.

## With an SDK [#with-an-sdk]

The [SDKs](/sdks) put an `Idempotency-Key` on every write for you: one key per call, reused on each of that call's retries. That covers a timeout inside one call. When your own code runs the same send twice, such as a retried queue job, pass a key from your own system so both runs share it:

<CodeBlockTabs defaultValue="Node.js">
  <CodeBlockTabsList>
    <CodeBlockTabsTrigger value="Node.js">
      Node.js
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="PHP">
      PHP
    </CodeBlockTabsTrigger>
  </CodeBlockTabsList>

  <CodeBlockTab value="Node.js">
    ```ts
    await sendkuy.messages.send(params, { idempotencyKey: 'order-1042-shipped' });
    ```
  </CodeBlockTab>

  <CodeBlockTab value="PHP">
    ```php
    $sendkuy->messages()->send($params, ['idempotency_key' => 'order-1042-shipped']);
    ```
  </CodeBlockTab>
</CodeBlockTabs>

The [Laravel](/laravel) notification channel does this on its own: it derives the key from the notification id, which survives every retry of a queued notification.
