REST API Basics: How REST APIs Work (For Frontend Developers)
What a REST API is, how HTTP methods and status codes work, and how to call one from JavaScript, explained simply for frontend developers.
- →A REST API lets a client (like your frontend) talk to a server over HTTP using URLs and methods.
- →The main HTTP methods map to actions: GET reads, POST creates, PUT/PATCH updates, DELETE removes.
- →Each response comes with a status code: 200 OK, 201 Created, 404 Not Found, 500 Server Error.
- →REST APIs are stateless, each request carries everything the server needs to handle it.
- →You call a REST API from JavaScript with fetch, then read the JSON response.
Almost every app you build talks to a REST API, to load data, save a form, log a user in. As a frontend developer you don't need to build one to use it well, but you do need to understand how it works. Here's REST explained in plain terms, with the pieces you actually touch.
What is a REST API?
A REST API is a way for a client and a server to communicate over HTTP using a consistent set of rules. You request a resource by its URL (an endpoint), using an HTTP method that describes the action, and the server responds with data (usually JSON) and a status code. That's it, REST is a convention, not a technology.
The HTTP methods (verbs)
How a REST API is used comes down to matching a method to an action on a resource:
| Method | Action | Example |
|---|---|---|
| GET | Read data | GET /users → list users |
| POST | Create data | POST /users → add a user |
| PUT / PATCH | Update data | PATCH /users/5 → edit user 5 |
| DELETE | Remove data | DELETE /users/5 → delete user 5 |
The URL names the resource (/users, /users/5); the method names what you're doing to it. This consistency is what makes REST predictable.
Status codes: what the server tells you
Every response includes a status code that tells you what happened. Learn the common ones and you can debug most API issues fast:
- •2xx success: 200 OK, 201 Created, 204 No Content.
- •4xx you made a mistake: 400 Bad Request, 401 Unauthorized, 403 Forbidden, 404 Not Found.
- •5xx the server broke: 500 Internal Server Error, 503 Service Unavailable.
A quick rule: 4xx means your request was wrong (fix the frontend), 5xx means the server failed (not your bug). 401 vs 403: 401 means 'not logged in', 403 means 'logged in but not allowed'.
Calling a REST API from JavaScript
You call a REST API with fetch. A GET is the simplest, request a URL, then parse the JSON response:
async function getUsers() {
const res = await fetch("https://api.example.com/users");
if (!res.ok) throw new Error(`HTTP ${res.status}`);
return res.json();
}To send data (POST), add a method, headers and a JSON body:
async function createUser(user) {
const res = await fetch("https://api.example.com/users", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(user),
});
return res.json();
}REST is stateless
A defining REST principle: the server keeps no memory of your previous requests. Each request must carry everything needed to handle it, including auth (usually a token in a header). Why REST is stateless matters: it makes APIs scalable and predictable, any server can handle any request.
fetch("/api/profile", {
headers: { Authorization: `Bearer ${token}` },
});Stateless means each request stands alone. That's why you send your auth token with every request, the server doesn't remember you from the last one.
The short version
A REST API lets your frontend talk to a server over HTTP: URLs name resources, methods (GET/POST/PUT/DELETE) name actions, and status codes tell you the result. Call them with fetch, send JSON, include your auth token every time, and remember REST is stateless, each request stands on its own.
Frequently asked questions
What is a REST API?+
A REST API is a way for a client and server to communicate over HTTP using consistent rules. You request a resource by its URL using an HTTP method (GET, POST, PUT, DELETE), and the server responds with data, usually JSON, and a status code. REST is a convention for structuring that communication.
What are the main HTTP methods in a REST API?+
GET reads data, POST creates new data, PUT and PATCH update existing data, and DELETE removes data. The URL identifies the resource (like /users/5) and the method describes the action you're performing on it. This mapping is what makes REST predictable.
What do HTTP status codes mean?+
Status codes report the result of a request. 2xx means success (200 OK, 201 Created), 4xx means the request was wrong (400 Bad Request, 401 Unauthorized, 404 Not Found), and 5xx means the server failed (500 Internal Server Error). 4xx is usually your bug; 5xx is the server's.
How do I call a REST API in JavaScript?+
Use fetch. For a GET, call fetch(url) and parse the response with res.json(). For a POST, pass an options object with method: 'POST', a Content-Type header, and a JSON.stringify'd body. Wrap it in async/await and check res.ok for errors.
What does it mean that REST is stateless?+
Stateless means the server keeps no memory of previous requests, each request must include everything needed to handle it, such as an auth token in a header. This makes REST APIs scalable and predictable, because any server can handle any request without shared session memory.
I build fast, SEO-ready sites and rank them on Google and AI search.