API Design Basics
Before this, it helps to know: DNS, HTTP, TCP in 10 Minutes.
Read a little, play a little. No scary maths, and no rush.
APIs are promises to strangers
An API is a contract: "send me this shape, I'll return that shape, forever." Every breaking change breaks someone's app at 2 AM. Good API design is mostly about keeping promises cheap to keep.
The methods and what they mean
GET— read. Safe to retry, never changes anything.POST— create. Not safe to retry blindly: two taps on "Pay" must not charge twice.PUT— replace. Same result no matter how many times you send it (idempotent).DELETE— remove. Deleting twice is fine — the thing is just gone.
That word, idempotent, is interview gold: "can I safely retry this?" For POST /payments, the answer comes from an Idempotency-Key header — the client generates one UUID per payment attempt, the server stores it, and a retried request with the same key returns the original receipt instead of charging again.
Lists: paginate or perish
Never GET /orders with no limits — one day that table has 10 million rows and your response is 800 MB. Instead:
GET /orders?cursor=eyJpZCI6MTAw&limit=50
The server returns 50 orders plus the next cursor. Cursors (not page numbers) stay stable when new rows arrive mid-scroll — page 2 of "page=2" shifts, cursors don't.
The boring parts that save interviews
- Versioning:
/v1/ordersfrom day one, so v2 can exist without breaking v1. - Errors:
{ "code": "RATE_LIMITED", "message": "...", "retryable": true }— machines readcode, humans readmessage. - Throttling: return
429 Too Many Requestswith aRetry-Afterheader instead of falling over.
A worked example: an orders API
GET /v1/orders?cursor=...&limit=50lists the caller's orders, newest first.GET /v1/orders/{id}returns one order, or 404 if it does not exist or belongs to someone else.POST /v1/orderswith anIdempotency-Keyheader creates an order and returns 201 Created with the new order.POST /v1/orders/{id}/cancelcancels it. Canceling an order that has already shipped returns 409 Conflict with a clear error code, because the request is valid but the order's state does not allow it.
Each endpoint answers the questions a client developer will have: what can I retry, how do I page through results, and what does each failure mean.
Naming and data conventions
- Resources are nouns, usually plural:
/orders,/users/42. The method supplies the verb. - Nest sparingly.
/users/42/ordersis clear; four levels deep is a maze. - Timestamps in ISO 8601, in UTC:
2026-10-08T14:30:00Z. Let clients convert to local time. - Money as integers in the smallest unit plus a currency code:
{"amount": 1999, "currency": "USD"}. Floating-point numbers cannot represent many decimal values exactly, and money is where that bites. - One casing style everywhere, such as
snake_caseorcamelCase, never a mix.
REST, GraphQL, and gRPC
REST over JSON, as described here, is the default for public APIs because every language and tool understands it. GraphQL lets the client ask for exactly the fields it needs in one request, which suits apps with many different screens. gRPC uses a compact binary format and generated client code, which suits fast communication between internal services. Start with REST unless one of those needs is real.
Remember this
- Retries are a fact of networks; design every endpoint assuming they happen.
- Cursor pagination, idempotency keys, versioned paths — say all three and you sound senior.
Check your understanding
5 questions · first-try answers earn the most XP
My notes
Saved in this browser. Highlight a line above and save it, or write it in your own words.
Nothing saved yet. Your highlights will live here.
References
Finished reading?
Ticking it here also ticks the chapter in the sidebar, the section count and your streak — it is all one number.
Practice this in an incident
Try a design drill
Related chapters
Spotted a mistake or want a topic covered? Report an issue