Idempotency
What is Idempotency?
Idempotency is the ability to ensure that the same operation can be made multiple times with the same effect as a single execution. This is critical for building reliable integrations that can safely retry failed requests without creating duplicate resources or triggering unintended side effects.
- Safe Retries: Retry failed requests without creating duplicates
- Network Resilience: Handle network failures and timeouts smoothly
- Prevent Duplicates: Avoid duplicate contracts, invoices, or payments
- 24-Hour Cache: Responses are cached and reused for 24 hours
When to Use Idempotency Keys
Use idempotency keys for any operation that creates or modifies resources:
Idempotency keys are supported for POST and PATCH requests only.
How It Works
When you include an Idempotency-Key header in your request:
- First request: Deel processes the request normally and caches the successful response (2xx status codes only)
- Duplicate request (same key within 24 hours): Deel immediately returns the cached response without processing again
- Cached responses include an
x-original-request-idheader indicating the response is from cache
Implementation
Generating Idempotency Keys
Always use a randomly generated UUID v4 for idempotency keys:
Key Requirements:
- Must be unique for at least 24 hours
- Can be up to 64 characters long
- Use UUID v4 for best results
- Never reuse keys across different operations
Making Idempotent Requests
Include the Idempotency-Key header in your POST or PATCH requests:
Handling Cached Responses
When Deel returns a cached response, you can identify it by checking for the x-original-request-id header:
Handling Concurrent Requests
If you send multiple requests with the same idempotency key while a request is still in progress, you’ll receive a 429 error:
How to handle:
If you intentionally cancel a request and want to retry, wait 2 minutes before using the same idempotency key.
Best Practices
Store idempotency keys with requests
When making critical requests, store the idempotency key in your database alongside the request data:
Benefits:
- Track which requests succeeded
- Retry failed requests with same key
- Audit trail of all attempts
Use idempotency for all mutations
Always include idempotency keys for operations that create or modify data:
Don't reuse idempotency keys
Each unique operation should have its own idempotency key:
Respect the 24-hour cache window
Cached responses last for 24 hours. Plan your retry logic accordingly:
- Immediate retries: Safe (returns cached response)
- Retries within 24 hours: Returns cached response
- After 24 hours: Key can be reused (cache expired)
For intentional operation retries (not network failures), generate a new key.
Handle errors appropriately
Only successful responses (2xx) are cached. Failed requests should be retried:
Common Scenarios
Scenario 1: Network Timeout
Scenario 2: Uncertain Request Status
Scenario 3: Batch Operations
Troubleshooting
Getting duplicates despite using idempotency keys
Possible causes:
- Generating new keys for each retry (should reuse key)
- Not including
Idempotency-Keyheader - Using different keys for same operation
Solution:
Receiving 429 errors
Cause: Concurrent requests with same idempotency key
Solution: Wait for Retry-After duration before retrying:
Want to retry with same data but get new result
Scenario: First request created wrong resource, want to create a new one
Solution: Generate a new idempotency key:
Testing Idempotency
Test that your integration handles idempotency correctly: