# Idempotency

> Retry a write safely with an Idempotency-Key header, and what a replay looks like.

URL: https://renzo-digital.com/docs/concepts/idempotency

A network error can hide whether a write reached the server. Send an `Idempotency-Key` header, and you can repeat the same request without doing the work twice. The second call gets the first answer back.

## Which calls take it

Every `POST`, `PUT`, `PATCH` and `DELETE` under `/api/v1` honours a non-blank `Idempotency-Key`, except password sign-in. These operations declare the header in the API reference, because a double run would cost money or change shared state:

- `POST /api/v1/billing/checkout-sessions`
- `PUT /api/v1/billing/subscription/price`
- `POST /api/v1/price-alerts`
- `POST /api/v1/referrals/me/referrer`
- `PUT /api/v1/economic-calendar/feed/filters`
- `POST /api/v1/economic-calendar/feed/reset`

Checkout uses a durable provider key shared by retries of the same pending purchase. Other billing writes also use provider idempotency.

## Send a key

Use a new random value, such as a UUID, for each action you mean to do once. Reuse it only to retry that same action.

```bash tab="curl"
curl -X POST "$BASE/api/v1/price-alerts" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Idempotency-Key: 6f1c2b8e-3d4a-4e5f-9a0b-1c2d3e4f5a6b" \
  -H "Content-Type: application/json" \
  -d '{"symbol_id": "ed6621c0-5979-526b-8ba1-452bf82b80c0", "direction": "above", "target_price": "250.50", "channels": ["email"]}'
```

```python tab="Python"
import os

import requests

BASE = os.environ["BASE"]
TOKEN = os.environ["TOKEN"]

answer = requests.post(
    f"{BASE}/api/v1/price-alerts",
    headers={
        "Authorization": f"Bearer {TOKEN}",
        "Idempotency-Key": "6f1c2b8e-3d4a-4e5f-9a0b-1c2d3e4f5a6b",
    },
    json={
        "symbol_id": "ed6621c0-5979-526b-8ba1-452bf82b80c0",
        "direction": "above",
        "target_price": "250.50",
        "channels": ["email"],
    },
)
answer.raise_for_status()
print(answer.json())
```

```js tab="Node.js"
const { BASE, TOKEN } = process.env;

const answer = await fetch(`${BASE}/api/v1/price-alerts`, {
  method: "POST",
  headers: {
    Authorization: `Bearer ${TOKEN}`,
    "Idempotency-Key": "6f1c2b8e-3d4a-4e5f-9a0b-1c2d3e4f5a6b",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    symbol_id: "ed6621c0-5979-526b-8ba1-452bf82b80c0",
    direction: "above",
    target_price: "250.50",
    channels: ["email"],
  }),
});
if (!answer.ok) throw new Error(`HTTP ${answer.status}`);
console.log(await answer.json());
```

## What a retry gets

| Case | Answer |
| --- | --- |
| Same key, same method, path and body, first call finished | The stored answer: same status, body and headers, plus `Idempotency-Replayed: true`. Nothing runs again. |
| Same key, first call still running | `409` [`idempotent_request_in_progress`](https://renzo-digital.com/docs/error-codes#idempotent_request_in_progress). Wait, then retry. |
| Same key, same method and path, different query or body | `409` [`idempotency_key_reused`](https://renzo-digital.com/docs/error-codes#idempotency_key_reused). Use a new key for a new action. |
| Same key, different method or path | A separate record. The call runs as new. |
| Key older than 24 hours | Treated as a new key. The action runs again. |

A replayed error keeps its first body, but its `correlation_id` is the one of the retry.

## Rules

- **Scope.** A key belongs to the signed-in caller. Two customers can use the same key without a clash. A call with no valid token is never stored or replayed: each retry runs as new.
- **What counts as "the same request".** The method, the path, the query parameters (names may be reordered; repeated values keep their order) and the exact body bytes. Send the body byte for byte the same on a retry.
- **Limits.** A key longer than 255 characters gets `400` [`bad_request`](https://renzo-digital.com/docs/error-codes#bad_request). A keyed body over 1 MiB gets `413` [`payload_too_large`](https://renzo-digital.com/docs/error-codes#payload_too_large).
- **Answers that are not stored.** These release the key, so a retry runs for real:
  - a path or method the API does not serve;
  - any `401` or `403` answer;
  - any `5xx` answer;
  - any answer with a `Retry-After` header, such as `429` and `503`;
  - answers marked `Cache-Control: no-store`, which carry credentials, such as OTP verify and token refresh.
- **Deletion and permissions.** Historical customer details are erased after account deletion; a staff replay can then return `410` `gone`. Reusing a key after staff permissions change returns a conflict instead of replaying data from the earlier permission set.
- **Keep for 24 hours.** Records live 24 hours. Retry within that time.

## When to use it

- Always on the six operations listed above.
- On any other write you retry automatically, such as creating a watchlist.
- A `GET` never needs it. Reads are safe to repeat.

See [Errors](https://renzo-digital.com/docs/concepts/errors) for which failures to retry.
