# Idempotency

> Why every write carries an Idempotency-Key, what happens when you send one again, and how secrets are kept out of a replay.

Source: https://docs.clouduxe.com/api/idempotency/

A network can drop the answer to a request that did go through. If you simply
send it again, a server restarts twice or an order is placed twice. An
`Idempotency-Key` makes the retry safe: the API recognises the key, and the
write happens once.

## Send a key with every write

Every `POST`, `PUT`, `PATCH` and `DELETE` needs an `Idempotency-Key` header.
The one exception is previewing an order, which writes nothing. The reference
shows the header on each operation that takes it; a write without one answers
`400` [`idempotency_key_required`](https://docs.clouduxe.com/api/errors/#idempotency_key_required).

The key is 1 to 128 characters of letters, digits, `_`, `.`, `:` and `-`. Make a
new UUID for each logical request, and send the same one when you retry that
request.

`POST /services/{serviceId}/power` (reference: https://docs.clouduxe.com/api/reference/server-actions/power-service/)

```bash
curl -X POST https://api.clouduxe.com/v1/services/c3a1f6d2-8b4e-4a9c-b7d5-2e1f0a3c6b8d/power \
  -H "Authorization: Bearer $CLOUDUXE_API_KEY" \
  -H "Idempotency-Key: 6f1c2e9a-4b7d-4e3a-9c8f-1a2b3c4d5e6f" \
  -H "Content-Type: application/json" \
  -d '{"action":"restart"}'
```

##### Response (`202`)

```json
{
  "object": "action",
  "id": "f5c8b2a1-6e3d-4c9f-b1a7-3d2e9c0f4b6a",
  "type": "restart",
  "status": "running",
  "serviceId": "c3a1f6d2-8b4e-4a9c-b7d5-2e1f0a3c6b8d",
  "orderId": null,
  "invoiceId": null,
  "failure": null,
  "secretAvailable": false,
  "secretExpiresAt": null,
  "createdAt": "2026-12-04T09:20:11Z",
  "startedAt": "2026-12-04T09:20:11Z",
  "completedAt": null,
  "livemode": true
}
```

The Node and PHP SDKs make a key for every write and reuse it on their own
retries, so you never handle one there.

## What a retry gets

| What you send | What you get |
| --- | --- |
| The same key and the same request, after the first one finished | The first answer again, with `Idempotent-Replayed: true`. The write happened once. |
| The same key while the first request is still running | After up to 4 seconds, `409` [`idempotency_in_progress`](https://docs.clouduxe.com/api/errors/#idempotency_in_progress) with `Retry-After: 1`. Retry with the same key. |
| The same key with a different request | `422` [`idempotency_mismatch`](https://docs.clouduxe.com/api/errors/#idempotency_mismatch). Use a new key for a new request. |
| The same key after the first request failed (any `4xx` or `5xx`) | The request runs again: a failure stores nothing. |
| The same key after `503` [`outcome_unknown`](https://docs.clouduxe.com/api/errors/#outcome_unknown) | The retry settles it: it answers what happened, or does the write once. |

A write commits its whole effect and its stored answer together, so you never
get a `4xx` for a write that took effect.

Keys belong to the token that sent them, so two tokens never collide, and they
are kept for 48 hours.

## Secrets are never replayed

A replay never repeats a secret:

- Creating a webhook endpoint and rotating its secret answer the signing secret
  in the first response only. A replay answers `secret: null`. If you lost the
  secret, rotate it.
- No write ever answers a server password. Provisioning, reinstalling and
  resetting a password answer an action, and the password, when there is one,
  is read from the action itself; see
  [Long-running actions](https://docs.clouduxe.com/api/actions/#one-time-secrets).
