How do you design an API properly?
- Posted
- Tags
- system-design, api-design
Before writing a single endpoint, start from who uses it and what they need to know. Then think through parameters, pagination, idempotency, retries, and auth before you build.
My team answers a lot of inquiries in Slack, and they disappear into threads. I thought of an initiative: a small web app that tracks each inquiry so we can see how many we get and how long they take to resolve.
I wanted to do this well, not just get something working. So before writing any code, I spent time learning what actually goes into designing an API properly.
The question I had
What should I think about when designing an API?
What I found
The short version: design from the outside in. Start with the people and their questions, and treat the code as the last step. Here are the things to consider, with how each one played out in my app.
1. Start with users and questions, not endpoints
Before any URL, list who uses the system and what each of them needs to do or know. Those needs decide the API, including what data you have to store.
In my case: a responder needs to see inquiries, acknowledge one, and resolve it. A team lead wants to know how long inquiries take. That second question means I have to store when an inquiry was acknowledged and when it was resolved. Starting from endpoints, I'd have missed that.
2. Model the resources
The nouns from step 1 become resources. For each one, decide its fields and the states it can be in. Keep the states as few as the users actually need.
In my case: one resource, the inquiry: the Slack message, who asked, and a status of either open or resolved. Acknowledging doesn't change the status. It sets an acknowledged_at timestamp, next to created_at and resolved_at.
3. Pick endpoints and HTTP methods
Pure REST uses nouns for URLs and lets the HTTP method be the verb (PATCH /inquiries/{id} with a new status). Action-style URLs name the action instead (/ack, /resolve). They're less "pure", but they're common and easy to read when an action is more than a field change.
In my case:
| Endpoint | What it does |
|---|---|
GET /inquiries | Lists inquiries |
POST /ack?thread_ts=<slack_link> | Acknowledges an inquiry |
PUT /res?thread_ts=<slack_link> | Resolves an inquiry |
I went action-style, and I identify an inquiry by its Slack thread, since that's what a responder has in hand.
4. Path parameters vs query parameters
The usual rule:
- Path parameters identify which resource.
/inquiries/42is one specific inquiry. Without the42, the URL means something else. - Query parameters shape the request. Filters, sorting, pagination, and options:
/inquiries?status=open&sort=created_at. Leave them out and you still get a valid answer, just a broader one.
A quick test: if removing the parameter changes what you're talking about, it belongs in the path. If it only narrows or orders the result, it's a query parameter.
In my case: thread_ts identifies the inquiry, so by the rule it belongs in the path. I put it in the query string because a Slack link contains slashes, and putting it in the path would mean URL-encoding it. That's a fair trade, as long as I know I'm bending the rule and why.
5. Pagination
Any list endpoint will grow, so plan for pagination before the list gets big.
- Offset (
?page=3or?offset=100&limit=50) is simple and lets you jump to any page. But when new rows keep arriving, every insert shifts the rows after it, so items get skipped or repeated between pages. Large offsets also get slow, because the database still walks past every skipped row. - Cursor (
?cursor=...&limit=50) says "give me the next 50 after this item." Each response returns anext_cursorfor the following page. New rows don't shift anything, and the query stays fast.
Whichever one you pick:
- Cap the
limit, so nobody can ask for a million rows in one call. - Use a stable sort order, like
created_atplusidas a tiebreaker. Otherwise two rows with the same timestamp can swap places between pages.
6. Idempotency
A safe method doesn't change anything (GET). An idempotent method gives the same result whether you call it once or five times (GET, PUT, DELETE). POST is neither by default, and PATCH isn't guaranteed to be.
This matters because requests get repeated: a user double-clicks, a network blip triggers a retry, or a proxy resends. A repeated idempotent call is harmless. A repeated non-idempotent one might do the thing twice.
For a POST that must not run twice (like a payment), a common pattern is the Idempotency-Key header. The client sends a unique key with the request, and the server stores the result. If the same key arrives again, the server returns the stored result instead of doing the work again. Stripe's API works this way, and there's an IETF draft standardizing the header.
In my case: resolving uses PUT, so resolving twice leaves the same result. Acknowledging uses POST, but I made it idempotent anyway: if the inquiry is already in the database, a repeat ack counts as a success and does nothing. That was a useful realization for me. Idempotency is about what the server does with a repeat, not just which method you pick.
7. Retries and error handling
Clients will retry, so the API should make it clear when a retry is worth it.
- One error shape everywhere. RFC 9457 (Problem Details) is a standard one:
type,title,status,detail. Clients can then handle every error the same way. - The right status codes.
400bad input,401not logged in,403not allowed,404not found,409conflict,429too many requests,503temporarily unavailable. - Retryable vs not.
429,502,503,504, and timeouts are usually worth retrying, because the next attempt might work. Most other4xxerrors are not: sending the same bad request again gets the same answer. - Back off. Retry with exponential backoff and jitter (wait 1s, 2s, 4s, plus a bit of randomness), so a crowd of clients doesn't retry in lockstep and knock the server over again. If the server sends a
Retry-Afterheader, respect it. - Only retry freely when it's safe. This is where idempotency pays off. A retried
PUTis fine. A retriedPOSTis only fine if the server handles repeats, like my ack does.
8. Authentication
Two separate questions:
- Authentication: who are you? A failure here is
401. - Authorization: what are you allowed to do? A failure here is
403.
Common options:
- SSO session: for web apps used by people inside a company. The user logs in once and the browser carries a session cookie.
- OAuth 2.0 or OpenID Connect bearer tokens: for APIs called by apps on a user's behalf, sent as
Authorization: Bearer <token>. - API keys: for service-to-service calls. Simple, but treat them like passwords and rotate them.
Whatever you use, send credentials in the Authorization header, never in the query string. URLs end up in server logs, proxy logs, and browser history.
In my case: there's no auth yet, because the app is internal only. But internal isn't the same as protected. Anyone on the network could call /res and resolve an inquiry. Company SSO is the natural next step, and later maybe a check that only responders can ack or resolve.
9. Also worth deciding early
Two smaller things that are painful to change once people depend on the API:
- Versioning:
/v1/...in the path, so a breaking change later can live at/v2without breaking anyone. - Consistent naming: pick one casing (
snake_caseorcamelCase) and one timestamp format (ISO 8601 in UTC) and never mix them.
Takeaway
Designing an API properly is mostly thinking before coding. Start from who uses it and what they need. Then decide the details that are hard to change later: what goes in the path versus the query, how lists page, what happens when a request repeats, how clients should retry, and who is allowed to call it.