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

# Idempotency

> Repeat a request without sending anything twice.

Send an `Idempotency-Key` header so you can repeat a request after a timeout or a dropped connection:

```bash theme={null}
curl -X POST https://rest.newlead.ai/v1/messages \
  -H "Authorization: Bearer $NEWLEAD_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: reply-ana-ferrer-001" \
  -d '{
    "type": "text",
    "lead_id": "<LEAD_ID>",
    "text": "Hi Ana, thanks for your interest."
  }'
```

These operations accept the header:

* `POST /v1/leads`
* `POST /v1/messages`
* `POST /v1/calls`
* `POST /v1/webhook_endpoints`

## Key rules

* A key has up to 255 characters.
* Use a different key for each operation. A UUID v4 works, or a format such as `<event>/<entity-id>`.
* Do not put personal data or secrets in a key.
* Each API key has its own idempotency keys.
* The API remembers a key for 24 hours.

## Retrying with the same key

| Case | Response |
| - | - |
| Same key and same body | The original response, with the header `Idempotent-Replayed: true` |
| Same key with a different body or route | `409 idempotency_key_reused` |
| The first request is still in progress | `409 idempotency_key_in_use`, with `Retry-After: 1` |

The order of the keys in the JSON body does not matter.

## Which responses are replayed

| Response | Stored | What a retry with the same key does |
| - | - | - |
| `2xx` | Yes | Returns the stored response |
| `4xx`, except the three below | Yes | Returns the stored error |
| `402`, `409`, `429` | No | Runs the request again, once you fix the cause |
| `5xx`, except `send_outcome_unknown` | No | Runs the request again |
| `502 send_outcome_unknown` | Yes | Returns the same error and sends nothing |

## `send_outcome_unknown`

The provider did not confirm the send or the call, so it may have gone out.

> [!WARNING]
>
> Do not retry with a new key until you know what happened. A new key sends the message or places the call again.

Check the result first:

| You sent | How to check |
| - | - |
| A call | `GET /v1/calls/{call_id}`, with the `call_id` from `error.details` when it is present. Otherwise, list the calls with `GET /v1/calls`. |
| A message to a lead | `GET /v1/leads/{lead_id}/messages` |
| A template to a phone | `GET /v1/leads?phone=%2B34600111222` to find the lead, then read its messages |

Use a new key only after you confirm that nothing went out.

## When the key cannot be checked

If the API cannot check the key, `POST /v1/calls` does not place the call and returns `502 provider_unavailable`. Retry with the same key.

The other operations process the request without idempotency protection.


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