How to test idempotency keys
A mobile app sends POST /payments. The network drops before the response arrives. The app retries. Did the customer just pay twice?
Idempotency keys are the standard answer. The client sends a unique Idempotency-Key header with each logical operation. If the server sees the same key again, it returns the first result instead of doing the work twice. Stripe, Adyen and most payment APIs work this way, and many internal APIs should.
They're also easy to get subtly wrong, so here's how to test them.
Set up a key per test
Generate a fresh key in a pre-request script, so every run starts clean:
// Pre-request script of "Create payment (first)"
pm.environment.set('idemKey', pm.variables.replaceIn('{{$randomUUID}}'));POST {{baseUrl}}/payments
Idempotency-Key: {{idemKey}}
Content-Type: application/json
{"amount": 4200, "currency": "EUR", "customerId": "{{customerId}}"}Store what came back:
// Post-response script of "Create payment (first)"
pm.test('payment created', () => pm.response.to.have.status(201));
pm.environment.set('firstPaymentId', pm.response.json().id);Test 1: the retry returns the same result
Duplicate the request as Create payment (retry), without the pre-request script, so it sends the same key and the same body:
pm.test('retry returns the original payment', () => {
pm.expect(pm.response.code).to.be.oneOf([200, 201]);
pm.expect(pm.response.json().id).to.equal(pm.environment.get('firstPaymentId'));
});Some APIs also send a header such as Idempotent-Replayed: true on a replay. If yours does, check it.
Test 2: only one record exists
The response can look right while the database has two rows. Check the side effect directly:
GET {{baseUrl}}/payments?customerId={{customerId}}&limit=100pm.test('exactly one payment for this key', () => {
const matching = pm.response.json().data.filter((p) => p.id === pm.environment.get('firstPaymentId'));
pm.expect(matching).to.have.lengthOf(1);
});Better still, give the test customer a fresh ID per run and check the total count is 1.
Test 3: same key, different body
This is the case that gets missed. A client bug reuses a key for a different payment. The API must refuse, not return the old payment as if the new one succeeded.
Duplicate the request again as Create payment (key reused), same key, but change the amount to 9900:
pm.test('reusing a key with a different body is rejected', () => {
pm.expect(pm.response.code).to.be.oneOf([409, 422]);
});Test 4: no key, and a malformed key
- No
Idempotency-Keyheader: either the request works as a normal create, or it's rejected with400if your API requires keys. Write down which, and test it. - An empty or 10,000-character key should be a
400, not a500.
Test 5: concurrent retries
Real retries can arrive while the first request is still running. Sequential tests won't catch a race between two in-flight requests with the same key. For that, send two at once from a terminal:
KEY=$(uuidgen)
for i in 1 2; do
curl -s -o /dev/null -w "%{http_code}\n" -X POST "$BASE_URL/payments" \
-H "Idempotency-Key: $KEY" -H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d '{"amount": 4200, "currency": "EUR", "customerId": "cus_race_test"}' &
done
waitExpect one 201 and either the same result or a 409 for the other, then check that only one payment exists.
Put it in the suite
Keep the requests in one folder in this order: first, retry, count, key reused. The folder runs top to bottom, and values set by scripts flow to the next request:
requestarium run-collection "Payments API" --folder "Idempotency" --env StagingRun it in CI like the rest of your tests. Idempotency breaks quietly during refactors, for example when someone moves the key check after the database write.