# Idempotency keys explained: why customers get charged twice

> A payment can succeed while its response is lost, so the client retries. Idempotency keys, unique constraints and webhook handlers stop the second charge.

Web page: https://davidewins.com/posts/idempotency-keys-explained

10 November 2026.

## The response is the part that gets lost

A request has five steps: the client sends it, the server receives it, the server does the work, the server sends a response, and the client receives it. A network failure can happen between any of them.

The difficult case is the last one. The work is done and the card has been charged, but the response never reaches the client. From the client's side, "the request never arrived" and "the request succeeded but the reply was lost" look identical. Stripe's error-handling docs describe exactly this: after a network error, the client does not know whether the server received the request.

Retrying is the right response to that uncertainty. Retrying without any way for the server to recognise the retry is how a customer ends up charged twice.

## What idempotent means

An operation is idempotent if performing it twice leaves the system in the same state as performing it once. Setting an order's status to "paid" is idempotent. Adding ten euro to a balance is not.

Stripe draws the same line. Its `GET` and `DELETE` requests are idempotent by definition and always safe to retry. `POST` requests, which create things such as charges and PaymentIntents, are not idempotent unless the request carries an idempotency key.

## How an idempotency key works

The client generates a unique string and sends it in the `Idempotency-Key` header. Stripe suggests a V4 UUID or another random string with enough entropy to avoid collisions. Keys can be up to 255 characters, and Stripe advises against using sensitive data such as email addresses as keys.

The first time Stripe receives a key, it runs the request and saves the resulting status code and body, whether the request succeeded or failed, including `500` errors. Any later request with the same key returns that saved result without running again. Replayed responses carry the header `Idempotent-Replayed: true`.

With a key in place, a retry is safe. Either Stripe has seen the key and returns the original result, or it has not and runs the request once.

## The rules around keys

Three details matter in practice.

Keys expire. Stripe states that retries with the same key are safe within 24 hours. It keeps keys for at least that long and can prune them after that, and a key reused after pruning can produce a new request.

Parameters must match. Stripe compares incoming parameters with the original request and returns an error if they differ.

Results are saved only once execution begins. If a request fails validation, or conflicts with another request using the same key at the same moment, no result is saved and the request can be retried.

## Automatic retries and 500 errors

Most of Stripe's official client libraries can generate idempotency keys and retry failed requests automatically once configured to do so. They retry with exponential backoff. Stripe also returns a `Stripe-Should-Retry` header when it knows whether a retry would help, and the libraries respect it.

Server errors need more care. Stripe caches `500` responses under the idempotency key, so a retry with the same key returns the same error. Stripe describes the result of a `500` as indeterminate: the operation may have partly happened. It advises against retrying with a new key, because the original request may have had side effects. Its suggestion is to attach a local identifier in the object's metadata, so that if a webhook later reports an object that never came back in an API response, it can be matched to the order that created it.

## A key that was not unique enough

On my delivery app, checkout used idempotency keys, but the key was built from the user id, the vendor id and the amount, because the app never sent an order id for the key to be built from. This failed in two ways. When a request matched a recent checkout's total but another parameter differed, such as delivery versus collection or the commission metadata, Stripe rejected the request, since keys for idempotent requests can only be used with the same parameters they were first used with, and the payment sheet failed to open with a 500. When a customer placed an identical repeat order within 24 hours, Stripe returned the saved PaymentIntent from the earlier checkout, and the new checkout received the old payment.

Stripe behaved correctly in both cases. The key did not identify a single attempt, so Stripe either rejected two different checkouts as mismatched or treated them as one and the same.

The fix was to only attach an idempotency key when a real order id exists to build it from. Without an order id, every attempt creates a new PaymentIntent; because the PaymentIntent uses manual capture, an abandoned one simply expires on its own.

Stripe's docs do list deriving a key from an object such as a shopping cart ID as a valid strategy against double submissions. That works when the object really does exist once per attempt, which an order id does and a user-vendor-amount combination does not.

## Unique constraints on your side

Stripe's key protects Stripe's side. The application database needs its own protection. If a server writes an order and fails before responding, the client's retry can write a second order.

A unique constraint handles this. Give each attempt an ID, add a unique index on that column, and the database rejects the second insert. In Postgres, `insert ... on conflict do nothing` turns the rejection into a no-op. The order exists once however many times the request arrives.

The order of operations matters as well. Creating the checkout attempt row, with its idempotency key, before calling Stripe means that a retry finds the existing attempt and reuses its key. Generating the key inside the request handler and discarding it on failure gives every retry a fresh key, which removes the protection the key was meant to provide.

## Webhook handlers must be idempotent too

Stripe states that webhook endpoints might occasionally receive the same event more than once. In live mode, Stripe retries undelivered events for up to three days with exponential backoff, and it does not guarantee delivery order.

Stripe's recommendation is to log the event IDs that have been processed and skip any already logged. A table of processed event IDs with a unique constraint applies the same pattern again: record the ID and do the work, or find the ID already present and return a success status without repeating anything.

## Summary

Responses get lost, and retrying is correct. Each retry needs a marker that identifies it as the same attempt: an idempotency key per attempt for Stripe, a unique constraint for the database, and the event ID for webhooks.

## Sources

- [Stripe docs: Idempotent requests](https://docs.stripe.com/api/idempotent_requests)
- [Stripe docs: Low-level error handling](https://docs.stripe.com/error-low-level)
- [Stripe docs: Webhooks](https://docs.stripe.com/webhooks)
- [PostgreSQL docs: INSERT](https://www.postgresql.org/docs/current/sql-insert.html)
