I was integrating with an internal service to query an order list. The docs said GET /orders, so I passed page=1 and got back the default page of data; then I passed page=2, and several of the ids in it overlapped with the first page. It took me a while to figure out: the default sort was by updated_at, and some orders in between had been modified, which shifted the entire ordering forward by one position.
The API itself wasn’t wrong; it just didn’t say whether the sort was stable. I’ve run into this kind of thing far too many times, and it almost always happens in the first two hours after integration. The designer thinks “isn’t this obvious?”, while the caller spends those two hours digging through logs.
Default values are the easiest API contract to overlook
What happens if you don’t pass page_size?
I’ve seen three implementations: return 10 records, return 20 records, return everything. The third one works fine in the test environment, but after going live, a single call pulls back hundreds of thousands of rows and the caller’s process OOMs immediately. The designer wasn’t trying to screw anyone over; they just assumed “not passing it means no limit.”
Now when I write APIs, the default value always goes on the first screen of the docs, and I stick to two rules: the default should be small, say 20; and there should be a hard cap, say 100, with an error if exceeded, not silent truncation.
Silent truncation is the hardest class of problem to debug. You pass limit=5000, the server returns only 1000 records, and there’s no hint of it in the response. The caller assumes that’s all the data there is, then runs statistics on this incomplete dataset. I’d rather it return a 400 and tell me directly “limit max 100.”
Sorting is the other side of the same problem. A paginated API must have a stable default sort, usually a combination like (created_at, id), guaranteeing a definite order between any two records. If you sort only by time, records created in the same second will jitter back and forth. Cursor pagination does avoid the duplicates and missed reads of offset pagination, but it has a precondition: the sort key must be unique and immutable. That precondition needs to be in the docs, otherwise callers will think that once they’re on cursor, everything is fine.
In an error response, what I most want to see is “can I retry”
When a caller gets a failure response, the first question is always: is it worth retrying?
Timeout failures are unavoidable, because when a timeout happens you have no idea whether the other side succeeded. So if the create API has no idempotency key, the client either waits it out or manually reconciles. That’s why I lean toward having all write operations accept a client-generated Idempotency-Key, with the server remembering which result it maps to within what time window, and documenting the window length and scope clearly.
For the error body structure, I generally do it like this:
1 | { |
code is for the program to branch on, message is for humans to read, request_id is for looking up logs. retryable is something I only added in the last two years; after adding it, integrators’ retry logic involved far less guesswork. Before that, they could only guess based on status codes: retry on 503, retry on 500 too. But half of 500s are parameter problems, and retrying ten thousand times won’t turn them into successes. Same for rate limiting: a 429 should ideally carry Retry-After so the client knows how long to wait, instead of inventing its own backoff curve.
Conversely, what bothers me most is “return 200 for all errors, with code=0 in the body indicating success.” That approach has its historical baggage, but for the caller, every request has to be checked at two levels, and missing either one is a production incident. I’ve written APIs like that myself, and later changed them.
One more detail: when parameter validation fails, don’t just throw out “invalid parameter.” Tell me which field and what’s wrong, ideally as an array listing field names and reasons item by item. This costs very little to implement and saves the caller an entire round of communication.
By the way, once code is shipped, it’s a contract. Rename it—even if the semantics haven’t changed and message hasn’t changed—and all the caller’s logic that branches on code will fall into the fallback branch, and this kind of error usually only surfaces on some rare path.
null, missing, and types: small decisions, long tails
In JSON, "field": null and a field that simply doesn’t exist are almost indistinguishable in JavaScript, but very different in Go or Kotlin: the former can deserialize into a zero value, while the latter may not even fill the struct.
My habit is: fields always exist, use null when there’s no value, and only allow omission when the semantics are genuinely polymorphic. That way the caller’s type definitions are stable and won’t fail to compile just because some branch has no data one day.
I’ve stepped in a few type-related pits.
Always use strings for ids. A 19-digit snowflake ID parsed as a number in JavaScript loses precision, with the last few digits turning into 0. Then you query with a wrong id, get nothing, and think the data was deleted. This is not a theoretical problem.
Use integers in the smallest currency unit for amounts, with a separate currency field, not floats. The 0.1 + 0.2 story doesn’t need repeating, yet every year I still see APIs with "amount": 19.99.
Use ISO 8601 or UTC timestamps uniformly for time, not 2024-05-01 10:00:00 without a timezone. The caller may be in another timezone and has no way to know whether it’s server local time or UTC—they can only guess.
Enums need room for extension. Adding a new enum value is a compatible change from the server’s perspective, but may be a crash from the client’s perspective if they wrote switch but forgot default. So in the docs I explicitly write: enum values may be added; do not assume the value set is fixed.
Partial failures in batch APIs need to be thought through in advance
Batch-create a hundred records, the third one fails—what do you return?
One approach is all-or-nothing; the other is per-item results. I lean toward the latter, especially when the batch operation itself is slow, because all-or-nothing means the caller has to rerun the entire batch, and rerunning brings idempotency into play again.
The per-item result format is roughly each item with an index and a status:
1 | { |
Using index rather than letting the caller assume “response order equals request order” is because the order really isn’t guaranteed to match, especially with internal concurrent processing.
The HTTP status code for partial failure also needs to be nailed down in the docs. Do you return 200 uniformly and rely on per-item status, or return an overall 207? Both are used, but they mean completely different things to the caller: the former means they have to iterate over results on every request. I lean toward 200 plus per-item status, and then state in one sentence in the docs that “each item must be checked.”
The boundaries of change
Adding response fields is compatible; the client can just ignore them. But a few things look like additions while actually being breaking: adding a required request parameter; changing a field from nullable to non-nullable; changing the default page size; changing the default sort.
I’ve seen all of these quietly happen in “minor version upgrades.” So to judge whether a change is compatible, I don’t look at whether it’s an “addition,” but at whether it could make a call that worked fine yesterday return a different result today.
There’s another hard-to-notice source: after a serialization framework or ORM upgrade, some field goes from nullable to non-nullable, or some default value quietly changes—not a single line of API code has moved, but the contract has already changed.
Whether to put the version number in the URL or the header—I have a preference but wouldn’t call it settled. A /v2 in the URL is intuitive; you can spot it at a glance in logs. In the header it’s cleaner, but unfriendly for debugging. What really determines success isn’t where the version number goes, but whether there’s a place that clearly records the differences between versions.
Now when I look at an API, I first flip to the “Error Codes” page in the docs. If that page has just one line—“For other errors, contact the administrator”—I can roughly guess how much time this is going to cost me later.
Next time you’re about to release a new API, do one small thing first: write the default page size, the default sort order, and the three most common error codes into the docs before you start on the implementation. As you write, you’ll find that some parameters you hadn’t planned to give default values to were left that way because you yourself hadn’t figured out what should be returned when they’re omitted.
Comments