APIs that survive contact
Designing an API that works in a demo is easy. Designing one that survives flaky networks, impatient clients, growing lists, and two years of change requests — that's the real craft. Four properties separate APIs that last from APIs that get rewritten. Each one exists because of a specific disaster, so let's meet the disasters first.
Idempotency: surviving the retry
Here's the nightmare scenario, and it is guaranteed to happen at scale: a client calls POST /payments to charge ₹500. Your server receives it, charges the card, and — right at that moment — the network hiccups. The response never reaches the client. The client sees a timeout. What does the client know? Nothing! Maybe the charge happened, maybe it didn't. So it does the only reasonable thing: it retries. Your server receives a second, perfectly legitimate-looking request. Double charge.
No transaction, no lock, no amount of server-side care prevents this — the second request looks brand new. The fix must involve the client: an idempotency key. The client generates a random UUID per logical operation (per "the user pressed Pay once") and sends it as a header. The server stores key → result. If the same key arrives again, the server doesn't re-execute — it returns the stored result. The retry becomes harmless. Keep the keys for at least 24 hours, because retries can arrive embarrassingly late.
POST /v1/payments Idempotency-Key: 5f1c0a44-9e2b-4c3d-8a71-0b2f7e9c1d33 // first call -> execute, store result under key // retry -> return stored result, do not charge again
A useful map of which verbs are naturally safe: GET (reading twice is fine), PUT ("set name to X" twice = same result), DELETE (deleting twice = still deleted). The dangerous ones are POST ("create a thing" twice = two things) and PATCH ("add ₹50" twice = ₹100). Those are the ones that need keys.
Pagination: surviving big, moving lists
The obvious approach — LIMIT 20 OFFSET 100000 for page 5,001 — has two diseases. First, performance: the database must count and throw away 100,000 rows to hand you 20, so deep pages get slower and slower. Second, correctness: offsets are positions, and positions shift. If one new item is inserted at the top between your page loads, everything slides down by one, and page 2 shows you an item you already saw on page 1 (or silently skips one). Every infinite-scroll feed with duplicate posts has this bug.
The fix is cursor pagination: instead of "give me items 100,000–100,020" (a position), say "give me 20 items older than this specific one" (a value). The server returns an opaque cursor encoding the last item's sort key; the next request passes it back:
WHERE (created_at, id) < (:cursor_ts, :cursor_id) ORDER BY created_at DESC, id DESC LIMIT 20
With an index, that's constant time no matter how deep you scroll, and inserts at the top can't shift your anchor — it's a value, not a position. (The id tiebreaker handles items sharing a timestamp.)
Versioning: surviving change
The day you ship a public API, someone builds on it, and now you can't break it without breaking them. The rule: additive changes are free — new optional fields and new endpoints don't hurt anyone, no version bump needed. Breaking changes — removing a field, changing its type, changing what it means — require a new version. Put it in the path (/v1/, /v2/): visible in every log line, trivially routable, unambiguous. Then actually commit: state how long v1 lives, and honour it. An API version is a promise with a duration.
Errors: surviving failure gracefully
A good error answers three questions: what happened (machine-readable code), how do I explain it to a human (message), and — the one everyone forgets — should I retry?
{"code":"insufficient_funds",
"message":"Balance is ₹120, charge was ₹500",
"retryable":false}
Why retryable matters: a client that can't distinguish "temporary blip, try again in a second" from "this will never, ever work" will either hammer you with pointless retries during an outage (making it worse) or give up on transient errors (dropping work it could have completed). One boolean field fixes both.
Returning HTTP 200 with an error inside the body. Every piece of infrastructure between you and the client — proxies, retry logic, monitoring dashboards, alerting — reads the status code, not your JSON. Return 200-with-errors and your monitoring shows a beautiful 100% success rate during a total outage. Lying to your own infrastructure costs you your own observability.