# Idempotency

> Safely retry writes without double-processing

Source: https://app.relvoerp.com/docs/product/guides/idempotency/

Every `POST` to `/api/v1/public` requires an `Idempotency-Key` header. This includes creates and actions such as `/contracts/{id}/activate`. Relvo uses the key to process a request at most once, even if you retry after a timeout or a network error.

```
Idempotency-Key: 2f6a1e4c-9b3d-4a11-9e2f-6c1a8b0d5e77
```

There are two exceptions: `POST /usage-events` and `POST /seat-events`. Batch ingestion carries an `idempotency_key` **on each event** instead of in a header.

## How it works

- Send a unique key with every write. A UUID works well.
- If Relvo has already completed a request with that key, it returns the **original result** and does not process the request again.
- A missing header is rejected with `400 idempotency_key_required`. A malformed key is rejected with `400 invalid_idempotency_key`.
- If you reuse a key with a **different** body, the response is `409 idempotency_key_reused`. Either your retry logic generated the wrong key, or two unrelated operations collided.

## Idempotency and If-Match

`PATCH`, `PUT`, `DELETE` and actions on existing resources also need `If-Match: W/"<version>"`. See [Errors](https://app.relvoerp.com/docs/product/guides/errors.md#optimistic-concurrency). Keep the same `Idempotency-Key` when you retry a request. When `If-Match` was stale, read the resource again, then send a new request with a new key.

## Practical guidance

- Generate the key once per logical operation, before the first attempt. Every retry of that operation must reuse it.
- Keys normally belong to a specific operation. The compatibility `/credit-grants` creation contract uses a permanent account-scoped marker; keep its keys unique across customer grant purchases. See [Credits](https://app.relvoerp.com/docs/product/guides/credits.md).
- If you retry while Relvo is still processing the original request, you get a fast, safe rejection rather than a duplicate write.

## Example

```bash
curl -X POST "https://app.relvoerp.com/api/v1/public/customers" \
  -H "Authorization: Bearer rk_live_xxxxxxxxxxxxxxxxxxxx" \
  -H "Idempotency-Key: 2f6a1e4c-9b3d-4a11-9e2f-6c1a8b0d5e77" \
  -H "Content-Type: application/json" \
  -d '{ "...": "..." }'
```
