Lesson 12 of 17
24. APIs and Idempotency

Memory card and where the analogy breaks

3 min read+10 XP

The memory card (tear this page out)

#QuestionMemory code
24.1API contract?The label is a promise: same button, same result
24.2API styles?Coins for everyone, cards between friends, a custom order for picky screens
24.3Versioning?Add freely; to change a button, put in a new machine and keep the old one running
24.4Pagination?Don't say "skip 20"; say "after this one"
24.5Errors and status codes?4xx: you fix it. 5xx: we're broken, try later. 429: slow down
24.6API auth?Keys for programs, tokens for people, and check every door, not just the front gate
24.7Double charge?A lost reply looks like a failure; retrying blindly pays twice
24.8Idempotency keys?Same receipt number, same result, no second charge
24.9Safe retries?Retry the temporary, only with a receipt, slower each time, and not forever
24.10Webhooks?Check it's really the bank, ignore the second SMS, and say "got it" fast

The whole chapter in one line: Write the contract down, change it only by adding, page with cursors, say clearly whose fault an error is, and give every write a receipt number so retries and webhooks can never pay twice.

Where the analogy breaks

  • A vending machine's buttons never change under you. An API's data does, between one page and the next, which is why cursors exist.
  • At a machine, you can see whether water came out. Over a network, the client often can't tell whether the server acted, which is the whole double-charge problem.
  • A bank SMS comes from one known number. Anyone can POST to your webhook URL, so only a signature proves who sent it.

Quick check

Five questions on this chapter. Pick an answer, then check it.

quiz