An API is a contract. You say: “ask me in this shape, and I’ll answer in this shape.” REST is the most common dialect of that contract — and it’s built on four verbs.
The four methods
Everything in this lesson hangs off this. Tap each card, look at the request and response:
Read data
You're asking for something, not changing anything. No body — just a URL.
Request
GET /api/usersResponse
[
{ "id": 1, "name": "Aiman", "email": "[email protected]" },
{ "id": 2, "name": "Siti", "email": "[email protected]" }
]How endpoints are usually named
Same URL, different method — different meaning.
- GET
/api/usersGet all users - GET
/api/users/:idGet one user by id - POST
/api/usersCreate a new user - PUT
/api/users/:idUpdate one user by id - DELETE
/api/users/:idDelete one user by id
The pattern: /api/ + the thing (plural) + optional id. The URL names the thing; the method says what to do with it.
If you remember one sentence: the URL names the thing, the method says what to do with it.
That’s why POST /api/users and GET /api/users hit the same address but mean completely different things. In a well-designed API, you almost never need to invent a URL for an action.
Naming rules that keep you out of trouble
You’ve seen the pattern in the component above. Three habits make it click:
- Prefix with
/api— separates your API routes from pages and static files./api/usersis data;/usersmight be a web page. - Name things with plural nouns —
/api/users, not/api/user, not/api/getUsers. - Put the id at the end —
/api/users/:id. The colon marks a placeholder: any id can live there.
Status codes: the backend’s tone of voice
Every response carries a status code. You only need a handful to start:
| Code | Meaning | When you’ll use it |
|---|---|---|
200 OK |
Success | GET, PUT, DELETE went fine |
201 Created |
Made a thing | After a successful POST |
400 Bad Request |
You sent nonsense | Missing fields, bad data |
404 Not Found |
Doesn’t exist | No user with that id |
500 Internal Server Error |
Server broke | Something threw an error |
The difference between 400 and 500 matters: 400 blames the request, 500 blames us. Never make the client take the fall for our bugs.
Our first API, no database yet
Before MySQL, let’s prove the routes work with an array pretending to be a database:
import express from "express";
const app = express();
app.use(express.json()); // lets Express read JSON bodies
let users = [{ id: 1, name: "Aiman", email: "[email protected]" }];
app.get("/api/users", (req, res) => {
res.json(users);
});
app.get("/api/users/:id", (req, res) => {
const user = users.find((u) => u.id === Number(req.params.id));
if (!user) return res.status(404).json({ message: "User not found" });
res.json(user);
});
app.post("/api/users", (req, res) => {
const { name, email } = req.body;
if (!name || !email) {
return res.status(400).json({ message: "Name and email are required." });
}
const user = { id: users.length + 1, name, email };
users.push(user);
res.status(201).json(user);
});
app.listen(5000, () => {
console.log("Server running on http://localhost:5000");
});
Try it right now, no setup needed: save as index.js, run node index.js, then open http://localhost:5000/api/users in your browser.
There’s an obvious problem though: restart the server, and every user you added is gone. An array lives in memory. Memory forgets.
That’s not a bug to fix — that’s the door to the next lesson.
Recap
URL names the thing; the method says what to do with it.
GET reads, POST creates, PUT updates, DELETE removes.
Naming: /api + plural noun, id at the end as :id.
Status codes are tone of voice — 200, 201, 400, 404, 500.
In-memory arrays forget on restart. Databases don't.
Next: put MySQL behind these routes with DBngin — a real database, one click away.
Up next
Database: DBngin + MySQL
Spin up a real MySQL server on your laptop with DBngin, then design the users table our API will live on.