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

# Idempotency

> Retry POST requests safely without creating duplicate chapters or double holds.

Networks fail. If a `POST` times out you can't tell whether it reached us. Send an `Idempotency-Key` header and you can retry the exact same request safely: we run it at most once and replay the saved response.

```bash theme={null}
curl -X POST https://api.relettr.com/v1/chapters/$CHAPTER_ID/start \
  -H "Authorization: Bearer $RELETTR_API_KEY" \
  -H "Idempotency-Key: 0b6e3c52-8c1f-4a7e-9d2b-5f1a7c3e9b10"
```

## Rules

* Every `POST` accepts `Idempotency-Key`: any string up to 255 characters. Use a **UUIDv4**.
* It is **required** on `POST /v1/chapters` and `POST /v1/chapters/{id}/start`. Without it, a retried create would make a second chapter.
* Keys are scoped to your account and to live or test mode.
* `GET` and `DELETE` ignore the header; they're idempotent already. Deleting an already deleted chapter returns `200`.

## What happens on a retry

| Situation | Response |
| - | - |
| Same key, same request | The saved response is replayed, with the header `Idempotent-Replayed: true` |
| Same key, different request (other path or body) | `409 idempotency_key_reused` |
| Same key while the first request is still running | `409 idempotency_in_progress`. Wait briefly and retry. |
| The first attempt failed validation (`400` or `422`) | Not saved. Fix the request and retry with the same key. |

All other responses, including `402` and `500`, are saved and replayed.

> [!WARNING]
>
> **Use a new key after a `402`.** If `start` returned `402 insufficient_credits`, buying credits and retrying with the same key just replays the saved `402`. Generate a fresh key for the new attempt.

## Generate one key per operation

Create the key once per logical operation and reuse it only for retries of that operation:

```python theme={null}
import time, uuid, requests

def start_chapter(cid, max_credits, attempts=5):
    key = str(uuid.uuid4())           # one key for this start, reused across retries
    for i in range(attempts):
        try:
            r = requests.post(
                f"{API}/chapters/{cid}/start",
                json={"max_credits": max_credits},
                headers={**HEADERS, "Idempotency-Key": key},
                timeout=30,
            )
        except requests.ConnectionError:
            continue
        if r.status_code == 409 and r.json()["error"]["code"] == "idempotency_in_progress":
            time.sleep(2 ** i)
            continue
        if r.status_code >= 500:
            time.sleep(2 ** i)
            continue
        return r
    raise RuntimeError("start did not complete")
```

## Starting is idempotent by state too

`start` on a chapter that is already `queued` or `processing` returns `200` with the chapter and holds nothing extra. A chapter can never be queued twice.


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