API design introduction
easy 24 minAn API is a contract: it says what a program may ask for and what it gets back, and it hides how the work is done.
When it comes up
The moments this idea belongs in a design interview, and the moments it does not.
Signals
When to bring it up
In a system design interview, the API comes after scope and numbers and takes about five minutes. Three to five endpoints are enough.
| You hear | Say | Because |
|---|---|---|
| Users can create and view | Two endpoints, with method and address | It fixes the scope in writing |
| Mobile clients | Few round trips, paging, small payloads | Each round trip is slow |
| Third-party developers | Stable, documented, versioned | You cannot update their code |
| Payments or orders | An idempotency key on POST | Replies get lost |
| Many servers | Stateless requests | Any server can answer |
| A long list | A limit and a cursor | No endless responses |
Options
Compare your moves
A shopper places an order. What does the endpoint look like?
BreaksGET /createOrder?product=1&qty=2
- How
- One link that creates an order.
- Limit
- GET is meant to be safe. Crawlers, previews and retries may call it, and each call may create an order.
- Choose when
- Never.
WorksPOST /orders with a JSON body
- How
- Creates an order, returns 201 and Location.
- Limit
- A lost reply leaves the client unsure. Retrying may order twice.
- Choose when
- Low-risk creations.
StrongPOST /orders with an idempotency key
- How
- The client sends a unique key; the server stores key and result and repeats the same answer.
- Limit
- Needs storage and an expiry rule.
- Choose when
- Anything with money or side effects.