# Retry writes safely

A **write** changes Mochi data. An **idempotency key** lets Mochi recognize a retry of the same write, so a lost
response does not accidentally create the same change twice.

Every public write request requires an `Idempotency-Key` header containing a UUID version 4 (UUID4):

```http
Idempotency-Key: 7ec24839-9134-4f51-9ee6-1e4c9049d9d3
```

## Choose when to create or reuse a key

- Create a new UUID4 for each new change you intend to make.
- Reuse the original key only when retrying the same HTTP method, path, and intended input.
- If the client times out, retry first with the original key. The first request may have completed after the client
  disconnected.
- Never use a new key merely because the response was lost. That can create a duplicate change.

Mochi keeps successful responses for 24 hours. A recognized retry returns the original status and body and includes:

```http
Idempotent-Replay: true
```

The saved result is also tied to the API key, method, and path. One organization cannot replay another
organization's response.

If the same request is still running, Mochi may return `409`. Wait briefly and retry with the same idempotency key.
Validation errors and other unsuccessful responses are not saved. Correct those requests before trying again.

For a `409`, timeout, or uncertain write result, follow [Troubleshoot API requests](https://themochi.app/docs/api/troubleshooting-and-safety/diagnose-api-failures).
Confirm current write operations in the
[canonical OpenAPI contract](https://themochi.app/docs/api/openapi.json).
