Choosing the right status code for a login API, one request at a time
Status codes are easiest to learn by following one small API through a few requests. Picture a service with a POST /login endpoint, a GET /admin page and a rate limit. Each request below gets one code, and the reason for each choice is the same reason you would use in your own code.
First, a client sends POST /login with a body that is not valid JSON: a missing closing brace. The server cannot even read what was asked, so the answer is 400 Bad Request. Next, the client fixes the JSON but leaves the password field empty. Now the body parses, but a value is wrong, so many APIs answer 422 Unprocessable Content, with a message naming the field. Then the client sends a wrong password. The request is well formed and complete, but the server cannot say who this is, so the answer is 401 Unauthorized. The right password finally gets 200 OK, with a token in the body.
With that token, the client asks for GET /admin. This user is a normal member, not an admin. The server knows exactly who is asking and still says no, so the answer is 403 Forbidden, not 401. Logging in again would not change anything, and that is the test for telling the two apart: if new credentials could fix it, use 401; if they could not, use 403. Some APIs answer 404 Not Found instead, so outsiders cannot even learn that an admin page exists. That is allowed, and the explanation for 404 above says so.
Now the client goes wrong and sends 100 login attempts in a minute. The server's limit is 10, so the 11th attempt gets 429 Too Many Requests, with a Retry-After: 60 header telling it to wait 60 seconds. If the server itself is down for maintenance, the right code is 503 Service Unavailable, also with Retry-After. And if the app crashed behind a load balancer, the client sees 502 Bad Gateway, produced by the load balancer, which is why the app's own logs may show nothing at all.
This table has limits worth knowing. It covers the codes registered with IANA, using the RFC 9110 names, and leaves out vendor codes because their meaning depends on the vendor. It also cannot tell you what a particular server meant: plenty of APIs return 200 OK with an error inside the body, or 500 for what is really a bad request. The code is a promise about meaning, and the explanations here describe the promise. When a server breaks it, the response body and the server's logs are the next place to look.