# Idempotency

> Retry a call safely with the Idempotency-Key header, so nothing is uploaded, filled or sent twice.

A network error can leave you unsure whether a call worked. Send an `Idempotency-Key` header and you can simply send the call again: AppliFlo returns the first answer instead of doing the work twice.

```bash
curl -X POST https://appliflo.com/api/v1/documents/$DOCUMENT_ID/send \
  -H "Authorization: Bearer $APPLIFLO_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: send-order-4821" \
  -d '{"subject":"Please sign your lease","routing":"parallel","recipients":[{"name":"Lan Nguyen","email":"lan@example.com","role":"signer"}]}'
```

## How it works

- The key is any string you choose, unique to the action. A UUID, or your own record id plus the action, both work.
- A repeat within 24 hours with the same key and the same request returns the first response. Nothing new is uploaded, filled or sent.
- A repeat while the first call is still running gets `409` with `request already in progress`. Wait a moment and retry.
- The same key with a different body or target is refused with `422` and `idempotency_key_reused`. Use a new key for a new request.
- If the first call failed, nothing is stored, so a retry with the same key runs again.
- After 24 hours the key is forgotten and can be used again.

Keys are kept per workspace and per endpoint.

## Which calls take it

| Call | What a retry avoids |
| --- | --- |
| `POST /api/v1/documents` | A second copy of the upload. |
| `POST /api/v1/fill` | Filling the PDF twice. |
| `POST /api/v1/documents/{id}/detect` | Reading the form twice. |
| `POST /api/v1/documents/{id}/fill` | Filling twice. |
| `POST /api/v1/documents/{id}/send` | Emailing the recipients twice. |
| `POST /api/v1/templates/{id}/create-document` | A second draft. |
| `POST /api/v1/templates/{id}/send` | A second envelope. |
| `POST /api/v1/templates/{id}/bulk` | A second batch. It returns the batch the key already made. |
| `POST /api/v1/saved-templates/{id}/create-document` | A second draft. |

On the MCP server, `upload_document`, `fill_document`, `send_envelope`, `create_from_template`, `send_template` and `send_template_bulk` take an `idempotency_key` input that works the same way.

## Retry rules of thumb

- Always send a key on calls that email people.
- Retry on network errors, `429` and `5xx`, with the same key.
- Do not retry `4xx` errors other than `409 request already in progress` and `429` without changing the request, and then use a new key.
