# Idempotency (/docs/api/idempotency)

Networks fail. If a request to create a checkout or a refund times out, you can't tell whether it ran. Send an `Idempotency-Key` header and retry with the same key: RunePay runs the request once and returns the same result to every retry.

```bash
curl https://runepay.co/api/v1/refunds \
  -H "Authorization: Bearer $RUNEPAY_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: refund_order_1042" \
  -d '{ "payment": "pay_01KA4Y0Z9V3T6N8Q2W5E7R1T3Y" }'
```

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

- Every `POST`, `PATCH` and `DELETE` accepts the header. `GET` requests don't need it.
- Use a value that identifies the operation in your system, such as an order id, or a random UUID you store before sending. Keys are scoped to your account and mode.
- The first request with a key claims it. Its response, **success or error**, is stored for **24 hours**.
- A retry with the same key and the same request (method, path and body) gets the stored response, with the header `Idempotent-Replayed: true`. The operation does not run again.
- A request that fails validation (a malformed body or parameter) does not claim the key, so you can fix it and resend with the same key.

## Conflicts [#conflicts]

| Status | Code | When |
| --- | --- | --- |
| `422` | [`key_reused`](/docs/api/errors#key_reused) | The key was used with a different request. Use a new key for a new operation. |
| `409` | [`in_progress`](/docs/api/errors#in_progress) | The first request with this key is still running. Retry shortly. |
| `409` | [`secret_not_replayable`](/docs/api/errors#secret_not_replayable) | See below. |

## Retrying after an error [#retrying-after-an-error]

Because errors are stored too, a retry with the same key returns the same error, including a `500`. A replayed error keeps the original `request_id`, in the body and the `Request-Id` header. That is deliberate: a request that failed part-way may already have reached the card processor, and replaying the error guarantees the operation is never attempted twice. Before retrying with a **new** key, check the object's state, for example by listing the payment's refunds or reading the payment.

## Responses that hold a secret [#responses-that-hold-a-secret]

Creating a webhook endpoint and rotating its secret return a signing secret. RunePay stores it encrypted so it can sign deliveries, but does not store the plaintext secret or replay it. These routes claim the key without storing the successful response in the idempotency cache (the `claim_only` mode). A retry with the same key does not create or rotate again; it gets `409 idempotency_error` with code `secret_not_replayable` and `object_id` set to the endpoint's id:

```json
{
  "error": {
    "type": "idempotency_error",
    "code": "secret_not_replayable",
    "message": "This request already succeeded, but its response held a secret that is not stored. Retrieve the object, or rotate its secret to get a new one.",
    "object_id": "we_01KA9F6G8H0J2K4M6N8P0Q2R4S",
    "request_id": "req_01KA4XZY8T2R5M7P1Q4W6E8R0T",
    "doc_url": "https://runepay.co/docs/api/errors#secret_not_replayable"
  }
}
```

The endpoint exists. If you didn't save the secret from the first response, rotate it to get a new one.

`DELETE /webhook_endpoints/{id}/previous_secret` ignores the header: deleting twice has the same effect as deleting once.