Guides › Status Codes and Errors: What the Server Is Telling You
Status Codes and Errors: What the Server Is Telling You
When you call an API, the server sends back a three-digit status code before anything else. Think of status codes like postal return stamps on an envelope. Before you even open the envelope to read the letter, the colored stamp on the outside tells you what happened: delivered safely (200), address not found (404), or return to sender because the address format was invalid (422).
The three codes you will see every day
- 200 OK: The request succeeded and the data you asked for is in the response body.
- 404 Not Found: The URL path points to an item or route that does not exist, like
/v1/names/999or/v1/keywords/zzz. - 422 Unprocessable Entity: The request reached the server, but one of the parameters broke a validation rule, like
/v1/names/0or/v1/names?limit=51.
See the errors three ways
1. In your browser
Open https://api.computewithpour.com/v1/names/999 in a browser tab. You will see a clean JSON error explaining that no person has id 999. Next, try https://api.computewithpour.com/v1/names/0 to see how the server responds when an id is below the minimum allowed value.
2. With curl
Use the -i flag in curl to print the HTTP status line and headers alongside the error body:
curl -i https://api.computewithpour.com/v1/names/999
3. With Python
Always inspect r.status_code in Python before assuming the response contains the data you expected:
import requests
r = requests.get("https://api.computewithpour.com/v1/names/999")
print(r.status_code) # 404
print(r.json()) # {'detail': 'No name with that id. Try /v1/names to see the ids.'}
Try it in the API Lab
Switch over to the API Lab and click the pre-set button for /v1/names/999. Notice the red status badge that displays 404 Not Found and inspect the returned detail message.
What to notice
An error response is not a broken server or a program crash: it is a structured, predictable answer. High-quality APIs provide a detail field in the JSON body that explicitly tells you what went wrong and how to fix your query. Notice also that 404 means the entity is missing, whereas 422 means your request was shaped incorrectly.
From errors to production code
Handling status codes, network timeouts, and structured error responses cleanly in Python is what separates fragile scripts from production-ready software. To discover how the requests library fits into the broader ecosystem of popular web and data tools, explore Python Top Ten Uses in our library.
Challenge
Predict the HTTP status code for each of these requests before running them: (a) /v1/names/3, (b) /v1/names/0, (c) /v1/names?limit=51, and (d) /v1/keywords/zzz. Then run the Python script to verify your predictions.
Show the answer
(a) 200 OK: id 3 exists (Linus). (b) 422 Unprocessable Entity: the API requires id ≥ 1. (c) 422 Unprocessable Entity: page limit is capped at 50. (d) 404 Not Found: zzz is not in the sample keywords. 404 means the item does not exist; 422 means your request broke a validation rule.
import requests
base = "https://api.computewithpour.com"
endpoints = ["/v1/names/3", "/v1/names/0", "/v1/names?limit=51", "/v1/keywords/zzz"]
for path in endpoints:
r = requests.get(f"{base}{path}")
print(f"{path} -> Status {r.status_code}: {r.json()}")