0%
API Design Interviews: REST, gRPC, GraphQL, Pagination, Idempotency, Versioning and Webhooks
Errors, Validation and Failure Contracts
Errors are part of the API contract, not an afterthought. A client spends far more code handling the ways a call can fail than the one way it can succeed, and most production incidents involving APIs come down to a client that misread an error: it retried something that could never succeed, gave up on something that would have worked a second later, or showed a user a stack trace. In an interview, a candidate who designs the happy path and then says errors will return 400 or 500 is leaving one of the most practical parts of the design blank. This lesson covers how to choose status codes, how to shape error bodies, how to report validation problems, how batch endpoints fail partially, and how to tell clients whether to retry.
Start with the family
HTTP status codes are grouped by their first digit, and the family is the most important decision. A 2xx says the request succeeded. A 4xx says the client must change something before the request can succeed: the input, the credentials or the target. A 5xx says the server failed to handle a request that may have been perfectly valid. That split decides how clients, proxies, monitoring and on call engineers react. Clients do not retry most 4xx responses and may retry 5xx. Dashboards count 5xx as your fault and alert on them, while a spike of 4xx usually means a broken client release.
The most common mistake is to put a client problem in the 5xx family, for example returning 500 because an unvalidated field crashed a parser. That pages your on call engineer for a client bug and encourages clients to retry a request that will fail forever. The reverse mistake, returning 400 when your database is down, hides an outage from your own alerts and tells clients there is no point retrying.
The codes you actually need
You do not need most of the registered codes. A small, well chosen set covers almost every API, and using it consistently is worth more than precision in rare cases.
400 versus 422
APIs differ on whether validation errors use 400 or 422, and either is defensible. A common convention, and the one this course uses, is 400 for requests the server cannot even parse, such as invalid JSON, a string where a number is required or a missing required header, and 422 for requests that parse fine but break a rule, such as an end date before the start date or an unknown country code. Some large APIs use 400 for both and distinguish the cases with an error code in the body. In an interview, pick one convention, state it once and apply it consistently. Consistency is what clients depend on, and interviewers mainly want to see that you have a rule.
401, 403 and 404
These three are frequently confused. A 401 means the server does not know who you are: no credentials, a malformed token, or an expired one. The client should obtain credentials or refresh a token, then retry. Despite its name, 401 is about authentication. A 403 means the server knows who you are and has decided you may not do this: a read only key trying to write, or a member trying an admin action. Retrying with the same credentials will not help.
The subtle case is a resource that exists but belongs to someone else. Returning 403 confirms that the resource exists, which can leak information: that a user id is registered, or that a private repository with that name exists. Many APIs return 404 for resources the caller is not allowed to see, and reserve 403 for resources the caller can see but cannot modify. Decide this per resource type and document it. Lesson eight covers the authorization model behind these decisions.
409 and the state of the world
A 409 Conflict means the request is valid on its own but conflicts with the current state of the resource. Creating a user with an email that is already registered, cancelling an order that has already shipped, or moving a booking into a slot that was just taken are all conflicts. The fix is not to change the syntax of the request but to look at the current state and decide what to do. Pair a 409 with a specific error code, such as email_taken or order_already_shipped, so the client can respond correctly without parsing prose. For version mismatches on conditional writes, 412 Precondition Failed is more precise, as lesson five explained.
500, 502, 503 and 504
The 5xx codes tell clients and operators what kind of server failure occurred. A 500 is an unexpected error inside your own service, usually a bug. A 502 means a gateway or proxy got an invalid response from an upstream. A 503 means the service is temporarily unable to handle the request, because it is overloaded, shedding load or in maintenance, and it pairs naturally with Retry-After. A 504 means a gateway gave up waiting for an upstream. Returning 503 rather than 500 when you deliberately shed load is useful: it tells clients the condition is temporary and expected, and lets your monitoring separate planned degradation from crashes.
Success codes deserve care too
The success codes carry meaning as well. A 201 with a Location header tells the client where the new resource lives. A 202 says the work has been accepted but not done, and should come with a link to an operation resource, as lesson five described. A 204 tells the client not to expect a body. Returning 200 with a body that says the operation failed is a classic anti pattern, sometimes called 200 with an error inside. It breaks every generic tool, from proxies to monitoring to SDK retry logic, because they all look at the status code first. If something failed, the status code must say so.
A decision order that avoids debates
When you are unsure which code fits, walk a fixed order. Could the request be parsed at all? If not, 400. Is the caller authenticated? If not, 401. Is the caller allowed to know this resource exists? If not, 404. Are they allowed to perform this action? If not, 403. Is the input valid by the rules? If not, 422. Does it conflict with current state or a precondition? Then 409 or 412. Is the caller over a limit? 429. Only then does the server try the work, and anything that goes wrong after that point is a 5xx. Checking in this order also has a security benefit: an unauthenticated caller never learns anything about validation rules or resource state, because the request is rejected before those checks run.
What the interviewer is checking
When you choose a status code in an interview, the interviewer is checking three things. Does the family match whose fault it is? Is the specific code one a client can act on? And are you consistent across endpoints? A short statement covers all three: invalid input returns 422 with field errors, missing auth returns 401, permission failures return 403 except on hidden resources, which return 404, conflicts with state return 409 with a specific code, overload returns 503 with Retry-After, and unexpected failures return 500 with a request id. Saying that once, near the start of the cross cutting concerns step, saves you from debating codes endpoint by endpoint.
Codes in gRPC and GraphQL
Other API styles have their own vocabularies. gRPC has a fixed set of status codes, such as INVALID_ARGUMENT, NOT_FOUND, PERMISSION_DENIED, UNAUTHENTICATED, FAILED_PRECONDITION, RESOURCE_EXHAUSTED, UNAVAILABLE and DEADLINE_EXCEEDED, which map closely onto the HTTP set above. Its rich error model lets you attach structured details such as field violations and retry information. GraphQL usually returns HTTP 200 and lists errors in an errors array alongside any partial data, so the error codes must live in each error's extensions field. When designing a GraphQL API, say that you will put a machine readable code in extensions and keep transport level failures, such as authentication or rate limits, on real HTTP status codes where your gateway can see them. Mentioning these mappings shows that your error model is a design, not a habit tied to one protocol.
In the cross cutting concerns step, give a one breath error policy: 422 with field errors for invalid input, 401 versus 403, 404 for hidden resources, 409 with a specific code for state conflicts, 503 with Retry-After for overload, 500 with a request id. Then apply it without re-arguing each endpoint.