Idempotency
Idempotency ensures that retrying a request multiple times has the same effect as making it once. This prevents accidental duplicate charges, transfers, or other critical operations.
What is Idempotency?
Idempotency keys allow you to safely retry API requests without worrying about performing the same operation twice. When you include an idempotency key with a request, Devdraft will return the same result for subsequent requests with the same key.
How It Works
1
Generate Unique Key
Create a unique idempotency key for each operation (recommended: UUID)
2
Include in Request
Add the key to your API request header as
Idempotency-Key
3
Safe Retries
If the request fails due to network issues, retry with the same key
4
Consistent Results
Devdraft returns the same response for requests with the same key
Supported Endpoints
Idempotency is supported on all state-changing operations:Payment Operations
- Create payment intents
- Process payments
- Refund transactions
- Cancel payments
Account Operations
- Create customers
- Update customer data
- Create invoices
- Generate payment links
Transfer Operations
- Initiate transfers
- Create withdrawals
- Process deposits
- Currency conversions
Product Operations
- Create products
- Update inventory
- Process orders
- Manage catalog
Implementation
Best Practices
Key Generation
Key Generation
Creating Effective Keys:✅ Do:
- Use UUIDs or other globally unique identifiers
- Generate new keys for each distinct operation
- Include operation context if needed (e.g.,
order_123_payment
)
- Reuse keys across different operations
- Use predictable sequences (1, 2, 3…)
- Include sensitive data in keys
Error Handling
Error Handling
Retry Logic:
Key Storage
Key Storage
Managing Keys:
- Store keys temporarily for retry scenarios
- Log keys for debugging and audit trails
- Don’t persist keys long-term unless needed
- Clean up old keys periodically
Key Behavior
Same Key, Same Result:When you retry a successful request with the same idempotency key:
- Returns the original response
- No additional processing occurs
- Same HTTP status code
- Identical response body
Response Example
Common Use Cases
Payment Processing
Payment Processing
Preventing Duplicate Charges:
Webhook Processing
Webhook Processing
Handling Duplicate Webhooks:
Batch Operations
Batch Operations
Processing Multiple Items:
Testing Idempotency
1
Create Test Scenario
Set up a test that makes the same request multiple times with the same idempotency key
2
Verify Response Consistency
Ensure all responses are identical (status, body, headers)
3
Check Side Effects
Confirm that the operation only occurred once (e.g., only one charge created)
4
Test Error Scenarios
Verify proper handling of conflicting keys and parameter mismatches
Use idempotency keys for all critical operations, especially in production environments where network issues and retries are common.
Idempotency keys are cached for 24 hours. After this period, using the same key will create a new operation rather than returning the cached result.