Blog / Fintech and payments

Idempotency keys for payment APIs, explained simply

A customer taps “Pay”. The request reaches your server, the payment goes through, and then the network drops before the app hears back. The app does what apps do: it tries again. Without protection, your server sees a brand new request and charges the customer a second time.

An idempotency key is how the server tells the difference between a new request and the same request arriving again.

The idea in one sentence

The client generates a unique key for each action, sends it with every attempt at that action, and the server promises that any number of requests with the same key have the same effect as one.

“Idempotent” is the maths word for an operation you can repeat without changing the result after the first time.

Where retries come from

  • mobile networks dropping mid-request
  • apps and libraries that retry automatically on timeouts
  • users tapping a button twice
  • payment gateways resending webhooks they think you missed
  • your own queue reprocessing a job after a crash

Every endpoint that moves money will see some of these. Not might. Will.

How to implement it

  1. The client creates a key when the user starts an action, such as pay_7f3a91, and keeps it for every retry of that action.
  2. The server stores the key in a table with a unique constraint, in the same database transaction as the work itself.
  3. If the insert succeeds, it is a new request: do the work and store the result against the key.
  4. If the insert fails because the key exists, it is a repeat: return the stored result and do nothing else.
BEGIN;
INSERT INTO idempotency_keys (key, user_id, request_hash)
VALUES ('pay_7f3a91', 4417, 'sha256:...');
-- a repeat fails here and the whole transaction rolls back
INSERT INTO ledger_entries (...) VALUES (...);
UPDATE idempotency_keys SET response = '{...}' WHERE key = 'pay_7f3a91';
COMMIT;

The mistake almost everyone makes first

Checking for the key in application code:

if not already_processed(key):
    process(key)

Two copies of the same request arriving at the same moment both run the check before either has finished, and both see “not processed”. The database constraint is the only check that cannot be raced. Put the guarantee where the data lives.

Details that matter

  • Scope keys to the user. One user’s key should never collide with another’s.
  • Store a hash of the request. If the same key arrives with a different amount, reject it as an error rather than returning the old result.
  • Expire old keys after a sensible period, such as a day or a week, depending on how long retries can arrive.
  • Handle in-progress requests. If a repeat arrives while the first is still running, return “still processing” rather than starting again.

Webhooks are the same problem

For incoming webhooks you do not choose the key: the gateway’s event or transaction reference is it. Store it with a unique constraint and the same pattern applies. We walk through the Paystack version in handling Paystack webhooks without crediting users twice, and our webhook tester checks yours.

Where this comes from. We do this work for clients. See fintech and payment systems, or the case study on Gamestakers.

Questions about this post? Email contact@naxfront.com. Nancy reads every one.

WhatsApp