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

# Idempotency

> Retry POST requests safely with the Idempotency-Key header.

Send an `Idempotency-Key` header on a `POST` request and a retry of the same request will not create a second call, lead or ticket.

```bash theme={null}
curl -X POST https://client-backend.oktivo.com/api/v2/calls \
  -H "Authorization: Bearer okt_your_token" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: order-81234-confirmation" \
  -d '{"phone": "+14155551234", "name": "Priya Sharma", "agentId": "agt_123"}'
```

## Where it applies

| Endpoint | v1 | v2 |
| - | - | - |
| `POST /calls` | Yes | Yes |
| `POST /calls/bulk` | Yes | Yes |
| `POST /leads` | Yes | Yes |
| `POST /tickets` | Yes | Yes |
| `POST /webhooks/endpoints` | No | Yes |

The header is optional. Without it, the request behaves as before. On any other endpoint the header is ignored.

## Key format

8 to 128 characters: letters, digits, `.`, `_`, `:` and `-`. Use a UUID or a stable id from your system, such as an order id. Any other format returns `400` with `INVALID_IDEMPOTENCY_KEY`.

Keys are scoped to your account and to the API version. The same key used on v1 and v2 is two independent keys.

## What happens on a retry

| Situation | Response |
| - | - |
| First request with this key | Processed normally. The response is stored for 24 hours. |
| Same key, same request | The stored response is returned with the original status code and the header `Idempotent-Replay: true`. Nothing is processed again. |
| Same key, different request | `422` with `IDEMPOTENCY_KEY_REUSED`. |
| Same key while the first request is still running | `409` with `IDEMPOTENCY_IN_PROGRESS` and `Retry-After: 1`. Retry shortly. |

"Same request" means the same method, path and JSON body. Key order inside the body does not matter, and the query string is not part of the comparison. In v2 the body is compared after validation, so values that normalize to the same input (for example extra whitespace around a name, or unknown fields that are dropped) count as the same request.

In v2, a replayed success response carries the `requestId` and `timestamp` of the replaying request, with the same `data`. A replayed error response is stored as sent, so it keeps the `requestId` of the original request.

## What is not stored

* Responses with a `5xx` status or `429` are not stored. You can retry with the same key and the request is processed again.
* Requests rejected before the key is checked are not recorded. In v2 that includes authentication errors, rate limiting and body validation errors. You can fix the request and reuse the key.

## What is stored

Every other response is stored and replayed, including `4xx` errors produced by the handler. For example, a `402` for insufficient credits or a `404` for an unknown agent is replayed for the same key. In v1, body validation runs inside the handler, so a `400` is also replayed. Use a new key after you change the request.

## Slow requests and fail-open behavior

* While a request with a key is running, the server keeps its in-progress claim alive (it is refreshed every 40 seconds against a 120-second claim). A slow `POST /calls/bulk` therefore keeps blocking a duplicate with `409 IDEMPOTENCY_IN_PROGRESS` until it finishes. If the worker crashes, the claim expires after 120 seconds and the key can be used again.
* Idempotency is **fail-open**. If the idempotency store is unavailable when the request starts, the request is processed without protection instead of being rejected, and a retry with the same key can run twice. If the store fails while the response is being saved, the response is still returned to you, but a retry may run again.

> [!NOTE]
>
> Idempotency protects against duplicate submissions by your own retries. Do not rely on it as the only guard against duplicates when your operation is not safe to repeat.

## Retry pattern

1. Generate the key once per logical operation, before the first attempt.
2. On a timeout, a network error, `429`, `503` or `500`, retry with the same key and the same body.
3. On `409 IDEMPOTENCY_IN_PROGRESS`, wait for `Retry-After` and retry.
4. Never reuse a key for a different operation.


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