# Idempotency

A network failure can leave you unsure whether a request landed. The operations that create money state take an `Idempotency-Key` header so you can retry safely: the first request runs; a retry with the same key gets the first answer back, nothing runs twice.

## Which operations

The reference marks them `x-kit-idempotent: true`; each says "Requires `Idempotency-Key`" in its description:

| Operation | Creates |
|---|---|
| `POST /v1/checkout-sessions` | a hosted checkout session |
| `POST /v1/orders` | a payment on a saved card and its order |
| `POST /v1/refunds` | a refund |
| `POST /v1/subscriptions/{subscription}/charge` | a payment for the next period of a merchant-managed subscription |
| `POST /v1/subscriptions/{subscription}/resume` | a payment when resuming a paused subscription |
| `POST /v1/api-keys` | an API key |

Other writes (fulfil, cancel, pause, archive, endpoint changes) are idempotent by nature or carry a row `version`, and take no key.

## The rules

- The key is yours to choose: 1 to 255 characters, unique per intended action. A UUID v4 or v7 is the usual choice. Derive it from your own record when you have one (`order-INV-1001-charge`) so a retry from a fresh process still matches.
- A replay with the **same key and the same body** returns the stored response: the same status, headers and body as the first time, for **24 hours**.
- The same key with a **different body** is `409 idempotency_conflict`. Never reuse a key for a different action.
- A missing key on an operation that requires one is `422 idempotency_key_required`.
- Only 2xx answers are stored. A `4xx` or `5xx` first attempt is not replayed; retrying with the same key runs the request again.
- Keys are scoped to your merchant account; another merchant's key never collides with yours.

## Example

```bash
curl -X POST https://api.sandbox.staging.regoria.com/v1/refunds \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: refund-INV-1001-2026-10-21" \
  -d '{"charge_id": "chg_01HZX5Y3V8N2K7QJ4R9T6B3M1C", "amount_cents": 1000, "reason": "requested_by_customer"}'
```

Run it twice: the second answer is byte for byte the first, and only one refund exists.

## In the SDKs

The TypeScript and PHP SDKs add a UUID v7 `Idempotency-Key` on these operations when you pass none, and accept yours as an option. Pass your own when a retry may come from another process.
