# Idempotency

A network can drop your request or its answer. If you repeat a call that changes data, you want it to run once, not twice. The `Idempotency-Key` header gives you that.

## How to use it

Send a unique value, such as a UUID, in the `Idempotency-Key` header on a POST, PATCH or DELETE. If you retry, send the same key and the same request.

```bash
curl -X POST https://api.seemly.com/v1/lists \
  -H "Authorization: Bearer $SEEMLY_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: $(uuidgen)" \
  -d '{"name": "Launch readers"}'
```

## Which operations need it

- Required on bulk, import and send operations (`bulk_upsert_subscribers`, `bulk_update_subscribers`, `create_import`, `request_send`). Without the header the answer is `idempotency_key_required`.
- Supported on every other operation that creates or changes something. Each operation page says which.
- Not needed on the read-style POSTs, which change nothing: `estimate_audience`, `render_design`, `render_mailing`, `preview_send` and `find_subscriber_by_email`. Send a key on any POST that creates or changes data.
- Reads and operations that are naturally repeatable (such as `unsubscribe_subscriber` and `add_subscriber_tags`) give the same result when you repeat them, with or without a key.

## What happens on a retry

| Situation | Answer |
| --- | --- |
| Same key, same request, the first call finished | The stored answer, with the header `Idempotent-Replay: true`. Nothing runs again. |
| Same key, the first call is still running | `409 request_in_progress` with `Retry-After`. Wait and repeat. |
| Same key, a different request | `409 idempotency_key_reused`. Use a new key for a new request. |
| The first call failed with a 5xx | Not stored. Repeating runs the request again. |

## Details

- A key is remembered for 24 hours and belongs to the credential that used it. Another key cannot see or reuse it.
- Two requests are the same when the method, path, query and body match; the order of JSON fields does not matter.
- An answer that contains a secret shown only once (a webhook signing secret) is never stored for replay, so a replay cannot expose it. Keep the secret from the first answer.
- A dry run (`dry_run=true`) never consumes a key.
- Use a fresh key per logical action, not one key for a whole script.
