Skip to content

How to Use Idempotency Keys ​

Idempotency keys prevent duplicate operations when a request is retried due to network failures, timeouts, or client-side errors. This is critical for endpoints that create resources or trigger financial transactions.

CLI users

These instructions are for direct HTTP API clients. The official Chainabit CLI creates and reuses idempotency identities automatically for supported mutation commands; do not pass a key to the CLI.

Why Idempotency Matters ​

Without idempotency protection, retrying a failed request can result in:

  • Duplicate charges -- a customer is billed twice for the same purchase
  • Duplicate resources -- two identical discounts are redeemed
  • Inconsistent state -- parallel retries create conflicting records

Idempotency keys solve this by letting the server recognize repeated requests and return the original response instead of executing the operation again.

Set up authentication and the shared environment in Prerequisites.

Which Endpoints Require It ​

The following endpoints require (or strongly recommend) an Idempotency-Key header:

EndpointWhy
POST /billing/checkoutPrevents duplicate charges
POST /billing/discounts/redeemPrevents redeeming a discount code twice

Other POST endpoints accept the header but do not require it. It is good practice to include it on any mutating request where a duplicate would cause problems.

How to Use It ​

Send the Idempotency-Key header with a unique identifier. A UUID v4 is recommended.

Example: Checkout ​

Starting checkout requires an authenticated session and billing permission for the selected account. It does not require password, authenticator or passkey confirmation. Use an idempotency key to prevent duplicate checkout creation. Other sensitive billing actions may require additional verification.

curl -s -X POST "https://api.chainabit.com/api/v1/billing/checkout" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Idempotency-Key: $(uuidgen)" \
  -H "Content-Type: application/json" \
  -d '{
    "variantId": "00000000-0000-4000-8000-000000000001"
  }'

Checkout is created for the account the request acts in (selected with the x-workspace-id or x-account-id header). It requires the owner, admin, or billing role in that account; other members receive 403. Pass variantId (a variant UUID from the products list) or variantSystemKey.

If another checkout for the same plan and account is being created at the same moment, the request returns 409 with error code billing_checkout_in_progress; retry the request.

Example: Redeem a Discount ​

The discount is redeemed for the account the request acts in. This requires the owner, admin, or billing role in that account.

curl -s -X POST "https://api.chainabit.com/api/v1/billing/discounts/redeem" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Idempotency-Key: $(uuidgen)" \
  -H "Content-Type: application/json" \
  -d '{
    "code": "YOUR_DISCOUNT_CODE"
  }'

Generating Idempotency Keys ​

Use any UUID v4 generator. Here are a few options:

bash
# Linux / macOS
uuidgen
javascript
// Node.js 19+ / modern browsers
crypto.randomUUID()

// Older Node.js
require('crypto').randomUUID()
python
import uuid
print(uuid.uuid4())

Rules ​

  1. One key per logical operation. Generate a new key for each distinct user action. Do not reuse keys across different operations.

  2. Same key = same request body. If you retry with the same idempotency key but a different request body, the server returns a 409 Conflict error with code idempotency_key_reused_with_different_payload.

  3. Keys expire after 24 hours. After expiry, the same key can be used for a new operation. Do not rely on this -- always generate fresh keys.

  4. Keys are scoped to your account. Two different accounts can use the same key value without conflict.

Error Responses ​

StatusMeaning
200Idempotent replay -- returning the original response
409 Conflict (idempotency_in_progress)A request with this key is currently being processed. Retry after the Retry-After header
409 Conflict (idempotency_key_reused_with_different_payload)The key was already used with a different request body
428 Precondition Required (missing_idempotency_key)The endpoint requires an Idempotency-Key header and none was sent
400 Bad Request (invalid_idempotency_key)The key is not 8-128 characters from A-Z a-z 0-9 : _ - .

Conflict Example ​

If a previous request with the same key is still in progress:

json
{
  "error": {
    "code": "idempotency_in_progress",
    "message": "Idempotent request is currently in progress"
  },
  "meta": {
    "requestId": "<request id>"
  }
}

Wait briefly and retry. The original request will complete and subsequent retries will return the cached response.

Built with purpose. chainabit.com