Idempotency
Safely retry API requests without creating duplicate charges, customers, or other resources. Learn how to use idempotency keys to build reliable payment integrations.
Overviewโ
Network issues, timeouts, and server errors can cause API requests to fail or return unclear results. Idempotency allows you to safely retry requests without worrying about duplicate operations. By providing an Idempotency-Key header, Omise guarantees the same request will produce the same result, even if sent multiple times.
- Add
Idempotency-Keyheader to POST/PATCH requests - Use unique key per operation (UUID recommended)
- Same key returns same result (cached for 24 hours)
- Essential for charge creation and money operations
- Prevents duplicate payments during network issues
What is Idempotency?โ
Idempotency means an operation can be performed multiple times with the same result. In payment processing, this is critical:
Without Idempotencyโ
1. Send charge request โ Network timeout
2. Did it succeed? Unknown. Retry?
3. Retry โ Duplicate charge! Customer charged twice ๐ฅ
With Idempotencyโ
1. Send charge with idempotency key โ Network timeout
2. Retry with same key โ Same result returned
3. No duplicate charge โ
How Idempotency Worksโ
- You send a request with an
Idempotency-Keyheader - Omise processes it and stores the result
- If you retry with the same key within 24 hours:
- Omise returns the cached result
- No new operation is performed
- Same response status code and body
Example Flowโ
# First request (network timeout)
curl https://api.omise.co/charges \
-X POST \
-u skey_test_...: \
-H "Idempotency-Key: 550e8400-e29b-41d4-a716-446655440000" \
-d "amount=100000" \
-d "currency=thb" \
-d "card=tokn_test_..."
# Response: (timeout - unclear if succeeded)
# Retry with same key
curl https://api.omise.co/charges \
-X POST \
-u skey_test_...: \
-H "Idempotency-Key: 550e8400-e29b-41d4-a716-446655440000" \
-d "amount=100000" \
-d "currency=thb" \
-d "card=tokn_test_..."
# Response: Returns the original charge (not a new one)
When to Use Idempotencyโ
Always Use For:โ
โ Creating Charges
POST /charges
Most important - prevents duplicate payments
โ Creating Customers
POST /customers
Prevents duplicate customer records
โ Creating Refunds
POST /charges/:id/refunds
Prevents duplicate refunds
โ Creating Transfers
POST /transfers
Prevents duplicate payouts
โ Creating Recipients
POST /recipients
Prevents duplicate recipient records
โ Any POST Request All POST requests that create resources should use idempotency keys
โ PATCH Requests Updates can be retried safely with idempotency
Not Needed For:โ
โ GET Requests Reading data is already idempotent (no side effects)
โ DELETE Requests Deleting is naturally idempotent (deleting twice = same result)
Idempotency-Key Headerโ
Header Formatโ
Idempotency-Key: <unique-string>
Key Requirementsโ
| Requirement | Description |
|---|---|
| Format | Any string up to 255 characters |
| Uniqueness | Must be unique per operation |
| Characters | Alphanumeric and hyphens recommended |
| Case Sensitive | key-1 โ KEY-1 |
| Lifetime | Stored for 24 hours |
Recommended: Use UUIDsโ
# UUIDv4 format (recommended)
Idempotency-Key: 550e8400-e29b-41d4-a716-446655440000
Why UUIDs?