# Safe retries

How the Idempotency-Key header makes tiktornado API changes safe to retry. Which requests need it, replays, 409 and 422 answers, and the 24-hour window.

A request can fail or time out after the change was made. The `Idempotency-Key` header lets you send it again without making the change twice: you get the first answer back instead.

## Which requests need it [#which-requests-need-it]

| Request | Needs `Idempotency-Key` |
| --- | --- |
| `POST /posts` | Yes |
| `PATCH /posts/{postId}` | Yes |
| `DELETE /posts/{postId}` | Yes |
| `PUT /accounts/{accountId}/queue` | Yes |
| `POST /comments/{commentId}/replies` | Yes |
| `POST /uploads` | No. A repeat simply gives you a new upload link |
| Every `GET` | No |

A change sent without the header, or with a value that isn't 1 to 255 visible ASCII characters (no spaces), answers `400` `idempotency_key_required` and nothing runs.

In MCP, every tool that changes something takes an `idempotencyKey` argument and sends it as this header.

## How it works [#how-it-works]

Make up a new value for each change, such as a UUID, and send it with the request:

```bash operation="DELETE /posts/{postId}" kind="request"
curl -X DELETE https://api.tiktornado.com/api/v1/posts/k174wq9nrm6t2f0bd3xh8pz5a87v1c2e \
  -H "Authorization: Bearer $TIKTORNADO_API_KEY" \
  -H "Idempotency-Key: 0c8e2a4f-1b7d-4e5a-9c3f-6d2b8a1e7f40"
```

Then, for the next 24 hours:

- **Same key, same request:** the same method, path and body get the first answer back, with the same status and the header `Idempotent-Replayed: true`. The change is not made again.
- **Same key, different request:** a different method, path or body answers `422` `idempotency_key_reused`.
- **Same key while the first is still running:** answers `409` `idempotency_in_progress`. Wait a moment and send it again.

"Same body" means the same bytes. Build the JSON once and resend that exact text: the same fields in another order, or with other spacing, count as a different body.

Keys belong to the API key that sent them. The same `Idempotency-Key` sent with two different API keys is two separate requests.

## What is stored [#what-is-stored]

Once a change has passed the key, scope and rate limit checks, its answer is stored under the `Idempotency-Key` for 24 hours. That includes refusals: if `POST /posts` answered `400` `schedule_too_soon`, the same key and body get that `400` again. To try again with a new time, send the new body with a new key.

These answers are not stored, so the same key runs the request again:

- `429` `slow_down`: wait, then retry with the same key.
- `500` `unknown_error` and `503` `service_unavailable`: our side failed. Retry with the same key.
- `401`, and a `403` or `409` or `422` from the checks before the change runs.

If a request is cut off before it finishes, its key frees up after 15 minutes and a retry runs it again. tiktornado still makes the change only once: the first request can no longer save once the retry has taken the key over.

## What is safe to retry [#what-is-safe-to-retry]

| Situation | What to do |
| --- | --- |
| A `GET` failed or timed out | Send it again |
| `POST /uploads` failed or timed out | Ask for a new link |
| A change timed out, or the connection dropped | Send it again with the same key and the same body |
| A change answered `429`, `500` or `503` | Wait, then send it again with the same key and the same body |
| A change answered `409` | Wait a moment, then send it again with the same key |
| A change answered `400` or `404` | Fix the request, then send it with a new key |

## Example [#example]

```bash operation="POST /posts" kind="request"
curl https://api.tiktornado.com/api/v1/posts \
  -H "Authorization: Bearer $TIKTORNADO_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: 5b0f9c3e-8d21-4f6a-9a7e-2c4d1e8b7f30" \
  -d '{"accountId": "j971x2c4ajrq0w8sd5e8fxz3a57v2k8m", "kind": "video", "mediaRefs": ["https://media.example.com/uploads/2b81de/morning-run.mp4"], "postNow": true}'
```

If this times out, run the exact same command again. Either the post was saved and you get its first answer, or it wasn't and it is saved now. Both times you end up with one post.