When creating a Hosted Checkout session via POST /checkout/v1/sessions, you can include the x-idempotency-key header to prevent duplicate charges in case of network errors or client retries.
x-idempotency-key is optional but recommended for any integration that retries session creation requests.
How It Works
If the server receives two requests with the same x-idempotency-key within the protection window, it treats the second as a retry and returns the original session without creating a new charge. After the window expires, a new request with the same key creates a new session.
Implementation
Add x-idempotency-key alongside your other headers when calling the session endpoint:
Generating a Key
Use a combination of your internal order identifier and a timestamp to create a key that is unique per session attempt and human-readable in logs:
Example:
Full Request Example
When to Reuse vs. Regenerate a Key
Reuse the same key only when retrying the exact same session creation request after a network error or timeout. Generate a new key whenever the request body changes — for example, if the customer updates their cart or switches currency before retrying.
Generating a new key on each retry defeats idempotency protection. Store the key alongside your order record before sending the first request so you can retrieve it on retry.
Next Steps