Idempotent Payments
Double-clicking "Pay" never double-charges
The problem: a user mashes the Pay button, or a flaky mobile connection retries the request — and the customer gets charged twice.
The fix: the action is defined with idempotency: { ttl: 60_000 }. Each request carries an Idempotency-Key; duplicates replay the stored result (same txId, one charge). Spam the button — the log proves the handler ran once. Change the amount with the same key to see the 422 conflict.
Idempotency-Key: f829f4eb-a552-41ec-9a82-fa826b695a7e
Charge log
No charges yet.
How it works
// server/actions/pay.post.ts
export default defineAction({
input: z.object({ amount: z.number().positive(), recipient: z.string() }),
idempotency: { ttl: 60_000 },
handler: async ({ input }) => charge(input),
})
// client — same key for retries of the same logical payment
useAction(pay, { headers: () => ({ 'Idempotency-Key': key.value }) })