API Design Guidelines
The criteria we hold every endpoint to, so the API stays small, predictable and honest, plus a review checklist that turns them into questions.
A good API is small, predictable and honest. A caller who learns one endpoint can guess the rest, gets what one need requires in one request, and can trust what every response says.
Use these criteria when designing an endpoint, extending one, or reviewing a change. Each section gives the rule and the reason for it, and the checklist at the end turns them into review questions.
Keep the surface small
Grow an endpoint with parameters that combine freely before adding a route.
- Parameters that compose. A need that is "the same question, cut another way" is a parameter on the existing endpoint. Make it work with every other parameter, not only the case that prompted it, so the other combinations come for free.
- Shared inputs everywhere. Filters that make sense across a family of endpoints are accepted by all of them, with the same names, meanings and defaults.
- One endpoint per action, taking a collection. "The same thing for many" is the existing endpoint accepting an array, not a batch twin beside it.
- A new route only for a new kind of thing. Add one when the input or the output really differs in kind, not because the use case is new.
A sign you broke this: two routes whose handlers differ by one clause.
Speak one language
What a caller learns on one endpoint should hold on every other.
- One record shape. Results of the same kind share one shape across endpoints. Richer queries add fields; they never rename, remove or retype existing ones.
- Higher levels are made of the same records. A summary or group returned next to detail rows is exactly what the plain query for that level would return, plus only what is new. Callers reuse their parsing and can cross-check the two.
- One envelope. Every response wraps the same way: the result, pagination when it is a list, and a status. Anything else at that level must be part of the result.
- Same names, defaults and limits. A new size or cap parameter mirrors the existing ones: same default, same maximum, same clamping. Pick one casing per layer and keep it.
- Define each concept once. A derived value, like a normalized identifier or a computed key, has one definition used by every filter, grouping and output. Keys from different endpoints then always match.
Return facts, honestly
The server states what is true; the client decides what it means.
- Raw facts, interpreted on the client. The server filters, aggregates and caps. Folding values into categories, hiding self-references or labeling belongs to the client, which can change its mind without an API release.
- Missing is a value. "No value" is a real group with its own row, counted like any other: not dropped, not merged into another.
- Say when you cut. Any list the server caps comes with what the caller needs to see it was cut, usually the full count. Totals count exactly what the rows could have shown, missing values included, or a cut list can look whole.
- Cap where fairness needs it. When results are grouped, cap each group rather than the whole list, so one big group can't crowd out the small ones.
Answer with the result
The top level of a response holds the result; everything else there must earn its place.
- Echo what the server decided, never what the caller sent. A default the server applied, a value it clamped or a cursor it computed is news to the caller: return it. A verbatim copy of the request tells the caller nothing it doesn't already have.
- Keep what is needed to read the result. If rows can't be understood without a parameter, such as the unit behind a bucketed key, that parameter is part of the result, even when the caller sent it.
- Nothing for show. Every field is payload, documentation and a compatibility promise. A field nobody needs costs all three and gives nothing back.
One request per need
Cost is part of the design, measured in requests on the client and queries on the server.
- Shape for the need, not the storage. If a client has to send a burst of requests, one per item or per period, and throttle them, the API is shaped wrong. Move the fan-out to the server.
- One query per request where possible. Count round-trips and scans. Totals, groups and rows can usually come from one statement.
- Page in the unit of the result. When results are grouped, page the groups rather than the rows, so no group is split across pages.
- Offsets with a total, cursors when counting is too expensive. Offset pages with a total are the default. Switch to a cursor only when a total costs too much, as on a large append-only log, and say why.
Change by adding, reject loudly
An existing request keeps getting the same answer; a nonsensical one gets told.
- Additive by default. New parameters are optional and new fields sit beside old ones. A request that worked before gets the same response after.
- Reject what can't mean anything. A parameter that needs another, two that conflict, or an unknown value gets a 400 naming the parameter. Silently ignoring it hides the caller's bug.
- Clamp what has a sensible bound. Out-of-range sizes clamp to the limit, the same way everywhere.
- Break deliberately. Removing or changing something is a decision with a reason, checked against the real consumers and called out in the change. It's cheapest before release, so fix a shape before it ships.
Status codes say whose fault it is
A caller should know from the status alone whether to fix their request or retry later.
- 4xx: the caller can fix it. Bad input, a missing permission, a resource that doesn't exist. The message names what to change.
- 5xx: the server failed. A failed query or upstream call is never a 400. Reporting it as one sends callers debugging a request that was correct.
- One error shape. Every error uses the same envelope and message style as the rest of the API. Return, Don't Throw is one way to keep it that way on the server.
The spec and the tests are the contract
What is published and what is tested must be what the API does.
- The spec matches behavior. Auth schemes, parameters, response shapes and every status a route can return are in the published spec, exactly as the route accepts and answers them.
- Tests read the spec. Assert against the generated document, so the spec can't drift from the code.
- Tests lock invariants, not only examples. Check the properties the design promises: a summary equals the plain query's record, totals agree with the rows, old requests answer unchanged.
- Tests cover the edges. Caps and truncation, missing values, empty and out-of-range pages, rejected combinations, and server failures.
Review checklist
Ask these of every new endpoint or change; a "no" needs a stated reason.
| Area | Question |
|---|---|
| Surface | Could this be a parameter on an existing endpoint, and does it combine with every other parameter? |
| Shape | Do results reuse the existing record shape, only adding fields? |
| Consistency | Are names, defaults, limits, casing and the envelope the same as the rest? |
| Facts | Does the server return raw facts, with missing values as real rows? |
| Truncation | Can the caller tell when a list was capped, and is the cap fair across groups? |
| Response | Is every top-level field the result, or something the server decided? |
| Cost | How many requests does the client need for this need, and how many queries per request? |
| Change | Does every existing request still get the same answer, and are nonsensical inputs rejected? |
| Errors | Are 4xx codes only for caller faults and 5xx for the server's? |
| Contract | Does the spec match the behavior, and do tests check the invariants and the edges? |
The rule
Keep the API small, predictable and honest: grow it with parameters that compose, speak one shape everywhere, return raw facts and say when you cut them, serve each need in one request, change it only by adding, let the status say whose fault it is, and hold the spec and the tests to what it actually does.