# Guide: safe retries with Idempotency-Key

A network error after a POST leaves you not knowing whether Beplan acted. Send an Idempotency-Key and retry with the same key: you get the first answer back instead of a second post.

## How to use it

Any `POST` accepts an `Idempotency-Key` header: a value you make up, unique per intended action, 1 to 255 visible ASCII characters. A UUID is a good choice. The generated examples in this documentation send one on every operation that takes it.

```bash
curl -X POST "https://api.beplan.io/v1/organizations/ORGANIZATION_ID/workspaces/WORKSPACE_ID/posts/POST_ID/schedule" \
  -H "Authorization: Bearer beplan_pat_…" \
  -H "Idempotency-Key: 7d0c5c2e-1f3a-4b1e-9a51-2f8d6c4e0b17" \
  -H "Content-Type: application/json" \
  -d '{"scheduledAt": "2030-01-17T14:00:00.000Z"}'
```

Make the key when you decide to do the action, store it with the action, and send the same key on every retry of it. A new action gets a new key.

## What happens

The key is remembered for **24 hours**, per connection, together with a fingerprint of the request (the operation, the path with its query, and the exact body bytes).

| Situation | Answer |
|---|---|
| First time | The request runs. Its answer is stored if it can be replayed (below) |
| Same key, same request, finished | The stored status and body, with `Idempotent-Replayed: true`. Nothing runs again |
| Same key, same request, still running | `409` [`CONFLICT`](https://beplan.io/docs/api/errors/#CONFLICT) with `Retry-After: 1`. Nothing runs |
| Same key, different request | `409` [`IDEMPOTENCY_KEY_REUSED`](https://beplan.io/docs/api/errors/#IDEMPOTENCY_KEY_REUSED). Nothing runs |
| Key not 1–255 visible ASCII characters | `400` [`VALIDATION_FAILED`](https://beplan.io/docs/api/errors/#VALIDATION_FAILED) |
| Body over 1 MiB with a key | `413` [`PAYLOAD_TOO_LARGE`](https://beplan.io/docs/api/errors/#PAYLOAD_TOO_LARGE) (the body is held to fingerprint it) |

Access is checked every time, replays included: a replay from a token that was revoked meanwhile is refused like any other call.

### What is stored

- **Stored and replayed**: `2xx` and `4xx` answers with a JSON body, or a `204`.
- **Never stored**: `429`, every `5xx`, every `409` (they say "try again" or "someone else is editing"), and anything that isn't JSON. The key is released, so a retry with the same key runs again.
- A request still marked as running after an hour (a server died mid-request) can be taken over by a retry.

### Operations that never store their answer

Some answers carry signed upload URLs or a sign-in link, which must not be kept. These operations are marked `x-beplan-idempotency: single-flight` in the OpenAPI document and say so in the reference: [`POST …/media-library/uploads`](https://beplan.io/docs/api/media-uploads/#media.uploads.create), [`POST …/media-library/raw-footage/uploads`](https://beplan.io/docs/api/media-uploads/#media.rawFootage.uploads.create), [`POST …/media-library/items/{mediaId}/versions`](https://beplan.io/docs/api/media-uploads/#media.versions.create), [`POST …/media-library/items/{mediaId}/upload/parts`](https://beplan.io/docs/api/media-uploads/#media.upload.parts.sign), [`POST …/social-accounts/connect`](https://beplan.io/docs/api/social-accounts/#socialAccounts.connect), the logo upload starts of organizations and workspaces, and a few others.

For them, the key only stops a second run from starting while the first runs (`409` [`CONFLICT`](https://beplan.io/docs/api/errors/#CONFLICT)). Once the first finished, the same key runs again and returns fresh URLs.

## What it doesn't cover

- `GET` is safe to repeat anyway. `PATCH`, `PUT` and `DELETE` ignore the header. For edits, send `baseUpdatedAt` where the operation takes it: a stale one answers `409` [`POST_CHANGED_ELSEWHERE`](https://beplan.io/docs/api/errors/#POST_CHANGED_ELSEWHERE) instead of overwriting.
- Keys belong to a connection: two different tokens with the same key are two different requests.
- After 24 hours a key is forgotten and the request runs again.

---
HTML version: https://beplan.io/docs/guides/idempotency/
