Reading Responses
Every API response has two parts: a status code (did it work?) and a body (here's the data). Learning to read both will make you a much more effective developer.
HTTP Status Codes
Status codes are 3-digit numbers that tell you the result of your request at a glance. You don't need to memorize them all — but knowing the major groups will save you hours of debugging.
OK
Request succeeded. Data is in the response body.
Created
New resource was successfully created (POST).
No Content
Success, but nothing to return (common for DELETE).
Bad Request
Something is wrong with your request — bad syntax or missing fields.
Unauthorized
You need to authenticate first.
Forbidden
You're authenticated but don't have permission.
Not Found
The resource doesn't exist.
Conflict
Request conflicts with current state — e.g. duplicate email.
Unprocessable Entity
Validation failed — data is structurally valid but semantically wrong.
Internal Server Error
Something broke on the server. Not your fault.
Service Unavailable
Server is down or overloaded.
Understanding JSON
Almost every modern web API returns data as JSON (JavaScript Object Notation). It looks like this:
// A JSON object — key/value pairs in curly braces
{
"id": 1,
"name": "Alice Johnson",
"email": "[email protected]",
"completed": false,
"tags": ["api", "rest", "beginner"],
"address": {
"city": "Chicago",
"state": "IL"
}
}JSON Data Types
| Type | Example | Notes |
|---|---|---|
| String | "Alice Johnson" | Always in double quotes |
| Number | 42 or 3.14 | No quotes needed |
| Boolean | true or false | Lowercase, no quotes |
| null | null | Represents no value |
| Array | ["api", "rest"] | Ordered list in square brackets |
| Object | {"city": "Chicago"} | Nested key/value pairs |
The Anatomy of an API Response
Most well-designed APIs wrap their data in a consistent structure. Our training API uses a common pattern:
// GET /api/training/users — a list response
{
"data": [...], // ← The actual records you asked for
"meta": { // ← Information about the result set
"total": 10, // How many total records exist
"limit": 10, // How many were returned this time
"offset": 0, // How many were skipped
"count": 10 // How many are in this response
}
}// GET /api/training/users/1 — a single resource
{
"data": { "id": 1, "name": "Alice Johnson", ... }
}// When something goes wrong — an error response
{
"error": {
"status": 404,
"message": "User not found"
}
}Checking Status Codes in Code
When you make API calls in JavaScript, always check the status code — don't assume success just because the request completed:
const response = await fetch('/api/training/users/1');
// ✓ Check if the request succeeded
if (response.ok) { // true for 200-299
const json = await response.json();
console.log(json.data); // Alice Johnson's record
} else {
const error = await response.json();
console.error(response.status, error.error.message);
}