What is idempotency?
Idempotency ensures that making the same API request multiple times has the same effect as making it once. This is crucial for preventing duplicate operations when network issues, timeouts, or retries occur. When you include anIdempotency-Key header in your request, Rainβs API will:
- Process the request only once, even if you send it multiple times
- Return the same response for subsequent requests with the same key
- Prevent duplicate charges, transactions, or other operations
How it works
When you make a POST, PUT, PATCH, or DELETE request with anIdempotency-Key header:
- First request: Rain processes your request normally and caches the response for 24 hours
- Duplicate requests: If you send the same request again with the same idempotency key, Rain returns the cached response immediately without re-processing
Specifications
Idempotency-Key must be 64 characters long at most. A longer key doesnβt fail the request; Rain processes the request normally but skips idempotency entirely, so retries with that key are not deduplicated.
Rain supports idempotency for:
- POST
- PUT
- PATCH
- DELETE
Response headers
All responses include these headers to help you understand the idempotency status:Best practices
Follow these practices to use idempotency keys safely:- Generate unique keys: Use UUIDs or similar unique identifiers for each operation
- Reuse keys for retries: If a request fails or times out, retry with the same idempotency key
- Donβt reuse keys: Never reuse an idempotency key for different operations
- Set reasonable timeouts: Cached responses expire after 24 hours
- Handle 429 responses: If you receive a concurrent request error, wait briefly and retry
Example
Hereβs a sample request creating a card with an idempotency key:Idempotency-Key. The second request will return the same response as the first request, including the Idempotency-Cached: true header.
Error handling
Server errors (5xx)
If your request results in a server error (status code 500 or higher), Rain does not cache the response. You can safely retry with the same idempotency key, and Rain processes the request again.Client errors (4xx)
Rain caches client errors (status code 400-499). If you retry with the same idempotency key, youβll receive the same error response.Concurrent requests
If you send multiple requests with the same idempotency key at the same time, Rain processes only the first request. Other requests will receive a429 Too Many Requests response:
See also
- Authenticating with the API: API keys, roles, and IP restrictions.
- Rain API overview: conventions, rate limits, and endpoint groups.