E-commerce and rules 5 min read Updated
Charging once, not twice
In short
Idempotency keys let the server recognise a repeated payment request and return the first result, so a retry never creates a second charge or order.
A customer clicks Pay, the connection drops, and the browser sends the request again. Without protection, the shop may charge the card twice or create two orders for one purchase. Idempotency means that sending the same request again has the same effect as sending it once. Safe payment rests on that property, so start with a key that lets every retry be recognised.
A server can finish the work and the reply still gets lost
A request can fail after the server has done the work but before the answer reaches the client. The client cannot tell whether the first attempt succeeded, so it repeats the request. Stripe’s documentation describes the consequence directly: when creating or updating an object, use an idempotency key. Then, if a connection error occurs, you can safely repeat the request without risk of creating a second object or performing the update twice.
Retries can come from the browser, the network or your own code. The server therefore has to assume that some requests will arrive more than once.
The key recognises a retry of the same request
The client creates a unique key for each logical operation and sends it with the request in an Idempotency-Key header. The server uses the key to recognise a retry of the same request. For API v1, Stripe saves the status code and body of the first request made with a given key, once the endpoint starts executing. Retries with the same key return the saved response, including a 500 error.
A draft IETF specification, The Idempotency-Key HTTP Header Field, defines the same header for non-idempotent methods such as POST and PATCH. It says that a key must not be reused with a different request payload. The server should then answer 422 (Unprocessable Content), and a retry that arrives while the original request is still being processed should receive 409 (Conflict). The draft is an Internet-Draft, not an RFC, so treat it as guidance rather than a settled standard.
The rules for keys in the Stripe API documentation are short and practical:
| Rule | What it means |
|---|---|
| Length | Keys can be up to 255 characters long |
| Choice of value | Use a V4 UUID or another random string with enough entropy to avoid collisions |
| Personal data | Do not use email addresses or other personal identifiers as keys |
| Lifetime | For API v1, keys can be removed after at least 24 hours; a key reused after removal is treated as a new request |
| Methods | For API v1, all POST requests accept keys; GET and DELETE are idempotent by definition, so keys have no effect on them |
Working on something similar in your own product? A few honest sentences are enough – we read every enquiry and reply by email. Apply with your idea
Store the result, not only the key
A key helps only if the server keeps enough to answer a retry. Stripe compares the incoming parameters with those of the original request. If they differ, it returns an error, which prevents accidental misuse of a key. It also saves a result only after execution has begun. If validation fails, or the request conflicts with a concurrent request, no result is saved and the request can be retried.
Build your own store on the same principles. Record the key, a fingerprint of the request body, the state (processing, done or failed) and the response. Make the key unique in the database, so that two simultaneous requests cannot both create a charge. This is general engineering practice, not a finding from the cited sources.
Webhooks and background jobs
The same problem appears when a payment provider delivers a webhook twice, or when your own scheduled job runs again after a timeout. Before changing an order, check whether that event or operation has already been handled, and record that it has been handled in the same transaction. The cited sources cover API requests; applying the pattern to webhooks and jobs is general practice. For the job side, see Scheduled jobs that do not fail silently.
Checklist for a payment request
- Generate a unique key for each payment attempt, and send the same key with every retry of that attempt.
- Store the key with a fingerprint of the request and the response it produced.
- Return the saved response when the same key arrives again, and reject a key that is reused with different data.
- Keep personal data out of the key, and keep the key out of logs that many people can read.
- Force a timeout in a test environment, retry the request, and confirm that only one charge and one order exist.
The behaviour described here follows Stripe’s documentation for API v1 at the time of checking. Stripe’s newer API v2 handles retries differently, so check the version your integration calls. This text does not replace the documentation of your payment provider.
Retry handling is only one part of a reliable payment. For the customer’s side of the journey, read a checkout that keeps shoppers to the end.
Common questions
- Why does a retry happen if the customer clicked only once?
- A request can fail after the server has finished the work but before the answer reaches the client. The client cannot tell whether the first attempt succeeded, so it sends the request again.
- What should the server do if a key is reused with different data?
- The IETF draft says the key must not be reused with a different payload, and that the server should answer 422 (Unprocessable Content). Stripe returns an error when the parameters differ from the original request.
- Can I use the customer’s email address as the key?
- Stripe advises against it, because keys should not contain personal identifiers. Use a random V4 UUID or another random string with enough entropy.
Sources
- Idempotent requests, Stripe. Accessed 24 November 2026
- The Idempotency-Key HTTP Header Field, IETF HTTPAPI working group (Internet-Draft). Accessed 24 November 2026
Further reading
-
Scheduled jobs that do not fail silently
A scheduled job fails silently unless each run is recorded, a missing run raises an alert and a repeated run does no harm.
-
Rate limiting that protects forms and logins
Rate limiting caps how often a client may repeat an action, and a 429 response with Retry-After tells honest clients when to try again.
-
Surcharges for card payments: what the EU bans
Traders may not charge customers extra for paying by consumer credit or debit card, online or in a shop, and any allowed fee must not exceed the cost of processing the payment.
Have an idea that should exist? Or a working product that needs more customers, a new website or a complete rework? Bring us anything. Apply with your idea