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

# Idempotency keys

> Retry a request safely: the same key never places a second call.

A network error can hide the reply to a request that Wave did receive. If your code then sends the request again, it can do the same thing twice — for example, place a second call to the same person. To prevent this, send an **`Idempotency-Key`** header on a request that changes data (`POST`, `PUT`, `PATCH` or `DELETE`).

```bash theme={null}
curl https://api.wave.sa/v1/calls \
  -H "Authorization: Bearer $WAVE_API_KEY" \
  -H "Idempotency-Key: 6f1c2d9a-0b7e-4e55-9a1f-3c2b8d4e7a10" \
  -H "Content-Type: application/json" \
  -d '{ "to": "+9665XXXXXXXX" }'
```

Use a new random value (for example, a UUID) for each new request, and the **same** value for each retry of that request.

## What Wave does with the key

| You send | Wave replies |
| - | - |
| The same key and the same request, within 24 hours, after the first one finished | The first reply again, with the header `x-idempotent-replayed: true`. Nothing new happens: no second call. |
| The same key and the same request while the first one still runs | `409 IDEMPOTENCY_KEY_CONFLICT`. Wait a moment, then retry. |
| The same key with a different request (another body, endpoint or method) | `422 IDEMPOTENCY_KEY_REUSED`. Use a new key for a new request. |
| A key that is empty, longer than 255 characters, or not printable ASCII (no spaces) | `400 VALIDATION_ERROR`. |
| No key | The request runs as usual, with no retry protection. |

Two requests are the same when they use the same method, the same endpoint and the same JSON body. The order of the fields in the body does not matter.

## Which replies Wave keeps

* Wave keeps a successful reply (`2xx`) for 24 hours.
* Wave also keeps one failure: `502 CALL_FAILED` from `POST /v1/calls` or `POST /v1/callback` when the telephony engine did not answer clearly. The call may have started, so a retry with the same key gets the same `502` and does **not** place a call. Check your webhooks or the person before you try again, and send a **new** key to place a new call.
* Wave does not keep any other error. After a `4xx` (for example a validation error) or another `5xx`, you can correct the request and retry with the same key.
* Wave does not keep a reply that contains a secret (for example a new webhook signing secret or web-calling credentials), or a reply larger than 64 KB. The key still stops a duplicate request while the first one runs (`409`) and a different request (`422`). But after the first reply, a retry with the same key runs again. Before you retry such a request, check if the first one was done.

## Scope

A key belongs to one project and one key environment. Two production keys of the same project share their idempotency keys, so a retry still works after you rotate a key. A sandbox key and a production key never share an idempotency key.

The key works only on a request that has an API key (or a signed-in portal user). On a request with neither, Wave ignores the header.

The key is optional, but we recommend it on every call request.


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