REST API: The Complete Guide. Ever wondered how different applications talk to each other? The answer, more often than not, is an API. In the world of web development, building a robust backend that can serve data to a front-end application, a mobile app, or even another server is a fundamental skill.
This guide will walk you through building a complete Express REST API. We’ll cover the core concepts of REST, implement all four CRUD operations, and show you how to test your endpoints like a pro. Let’s dive in and start building!
What is a REST API? 🤔
REST stands for REpresentational State Transfer. It’s not a technology or a language, but an architectural style—a set of rules and constraints for creating web services. A REST API is simply an API that follows these REST principles.
Think of it like a waiter in a restaurant. You (the client) make a request for a specific dish (a resource) using a standard menu (the API endpoints). The waiter (the API) takes your request to the kitchen (the server), gets the dish, and brings it back to you (the response).
Key Principles of REST
- Client-Server Architecture: The client (front-end) and server (back-end) are separate and evolve independently.
- Statelessness: Each request from a client to the server must contain all the information needed to understand and complete the request. The server does not store any client session information.
- Uniform Interface: Requests for a resource are made using a consistent interface, primarily through standard HTTP verbs (GET, POST, PUT, DELETE).
Setting Up Your Express Project for REST API
Before we can build our API, we need a basic Express server. Make sure you have Node.js and npm installed.
- Create a project folder and navigate into it.
- Initialize a new Node.js project:
npm init -y
- Install Express:
npm install express
- Create a server file, for example `server.js`.
Your basic `server.js` should look something like this:
const express = require('express');
const app = express();
const PORT = 3000;
// Middleware to parse JSON bodies
app.use(express.json());
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
Creating CRUD Operations with Express
CRUD stands for Create, Read, Update, and Delete. These are the four basic functions of persistent storage. We’ll build an endpoint for each of these operations to manage a simple list of items. For this example, we’ll use an in-memory array to act as our “database.”
// Our in-memory "database"
let items = [
{ id: 1, name: 'Item 1' },
{ id: 2, name: 'Item 2' }
];
1. Create → POST /items
This endpoint will allow us to add a new item to our collection. We’ll get the new item’s data from the request body.
// CREATE a new item
app.post('/items', (req, res) => {
const newItem = {
id: items.length + 1,
name: req.body.name
};
items.push(newItem);
res.status(201).json(newItem);
});
2. Read → GET /items
This endpoint retrieves all items. We can have one route to get all items and another to get a specific item by its ID.
// READ all items
app.get('/items', (req, res) => {
res.json(items);
});
// READ a single item by ID
app.get('/items/:id', (req, res) => {
const item = items.find(i => i.id === parseInt(req.params.id));
if (!item) return res.status(404).send('Item not found.');
res.json(item);
});
3. Update → PUT /items/:id
This endpoint updates an existing item. We’ll identify the item by its ID (from the URL parameters) and update it with data from the request body.
// UPDATE an item by ID
app.put('/items/:id', (req, res) => {
const item = items.find(i => i.id === parseInt(req.params.id));
if (!item) return res.status(404).send('Item not found.');
item.name = req.body.name;
res.json(item);
});
4. Delete → DELETE /items/:id
Finally, this endpoint removes an item from our collection based on its ID.
// DELETE an item by ID
app.delete('/items/:id', (req, res) => {
const itemIndex = items.findIndex(i => i.id === parseInt(req.params.id));
if (itemIndex === -1) return res.status(404).send('Item not found.');
const deletedItem = items.splice(itemIndex, 1);
res.status(200).json(deletedItem);
});
Read More: Master EJS with Express.JS: A Beginner’s Guide 2025
Read More: ExpressJS Request & Response Handling: Powerful Guide 2025
How to Test Your REST API with Postman 🚀
You can’t just open your API endpoints in a browser (well, you can for GET requests). To properly test all HTTP verbs, you need an API client like Postman or Thunder Client (a VS Code extension).
- Download and install Postman.
- Start your Express server by running
node server.js
in your terminal. - To test the POST route: Set the method to `POST`, enter the URL `http://localhost:3000/items`, go to the “Body” tab, select “raw” and “JSON”, and enter `{“name”: “New Item”}`. Hit “Send”.
- To test other routes: Change the method (GET, PUT, DELETE) and modify the URL as needed (e.g., `http://localhost:3000/items/3` for a specific item).
Conclusion
Congratulations! You’ve just built a fully functional Express REST API with complete CRUD operations. You learned the core principles of REST, how to handle different HTTP requests with Express, and how to test your endpoints to ensure they work correctly.
This is the foundation for building the backend of almost any modern web application. The next step is to replace the in-memory array with a real database like MongoDB or PostgreSQL.
Frequently Asked Questions (FAQ)
What’s the difference between PUT and PATCH?
PUT is used to completely replace an existing resource. You must send the entire resource object in the request body. PATCH is used for partial updates; you only need to send the fields you want to change.
Why is REST stateless?
Statelessness means the server doesn’t store any information about the client’s previous requests. This makes the API more reliable, scalable, and easier to manage because any server can handle any client request without needing session context.
Do I need a database to build a REST API?
No, as this tutorial shows, you can start by using an in-memory array for testing and development. However, for a real-world application where data needs to persist after the server restarts, you will need to connect your API to a database.
External Resources: To test and document your APIs, check out the official Postman website.