REST API Tutorial: Build Your First API with Node.js and Express
Build your first REST API with Node.js and Express in this step-by-step beginner tutorial. Learn GET, POST, PUT, and DELETE routes and test your API with real examples.
Understanding how to build a REST API is one of the most important skills a web developer can have. APIs are the backbone of modern web applications — they power everything from mobile apps to single-page applications to third-party integrations.
In this tutorial, you will build a complete REST API from scratch using Node.js and Express.
What is a REST API?
A REST API (Representational State Transfer Application Programming Interface) is a way for two systems to communicate over the internet using standard HTTP methods. When your front-end needs data from a server, or when two applications need to share information, they do so through an API.
The four main HTTP methods used in REST APIs are:
- GET — retrieve data
- POST — create new data
- PUT or PATCH — update existing data
- DELETE — remove data
Prerequisites
Before starting this tutorial, you should have:
- Node.js installed on your computer (version 18 or higher recommended)
- A code editor (VS Code is recommended)
- Basic knowledge of JavaScript
- npm (comes with Node.js)
Step 1: Set Up Your Project
Open your terminal and create a new project folder:
mkdir my-first-api cd my-first-api npm init -y
Now install Express:
npm install express
Create a new file called server.js in your project folder.
Step 2: Create Your Basic Express Server
Open server.js and add the following code:
const express = require(‘express’); const app = express(); const PORT = 3000;
app.use(express.json());
app.get(‘/’, (req, res) => { res.json({ message: ‘Welcome to my first API’ }); });
app.listen(PORT, () => { console.log(‘Server is running on port ‘ + PORT); });
Run your server with:
node server.js
Open your browser and visit http://localhost:3000 — you should see your JSON response.
Step 3: Create Sample Data
For this tutorial, we will use an in-memory array as our data store. In a real application, this would be a database. Add this to your server.js above the routes:
let products = [ { id: 1, name: ‘Laptop’, price: 999, category: ‘Electronics’ }, { id: 2, name: ‘Headphones’, price: 79, category: ‘Electronics’ }, { id: 3, name: ‘Coffee Mug’, price: 15, category: ‘Kitchen’ } ];
Step 4: Build the GET Routes
Add these routes to retrieve your products:
// Get all products app.get(‘/api/products’, (req, res) => { res.json(products); });
// Get a single product by ID app.get(‘/api/products/:id’, (req, res) => { const product = products.find(p => p.id === parseInt(req.params.id)); if (!product) { return res.status(404).json({ error: ‘Product not found’ }); } res.json(product); });
Step 5: Build the POST Route
Add a route to create new products:
// Create a new product app.post(‘/api/products’, (req, res) => { const { name, price, category } = req.body;
if (!name || !price || !category) { return res.status(400).json({ error: ‘Name, price, and category are required’ }); }
const newProduct = { id: products.length + 1, name, price, category };
products.push(newProduct); res.status(201).json(newProduct); });
Step 6: Build the PUT Route
Add a route to update existing products:
// Update a product app.put(‘/api/products/:id’, (req, res) => { const product = products.find(p => p.id === parseInt(req.params.id)); if (!product) { return res.status(404).json({ error: ‘Product not found’ }); }
const { name, price, category } = req.body; if (name) product.name = name; if (price) product.price = price; if (category) product.category = category;
res.json(product); });
Step 7: Build the DELETE Route
app.delete(‘/api/products/:id’, (req, res) => { const index = products.findIndex(p => p.id === parseInt(req.params.id)); if (index === -1) { return res.status(404).json({ error: ‘Product not found’ }); }
products.splice(index, 1); res.json({ message: ‘Product deleted successfully’ }); });
Step 8: Test Your API
Use a tool like Postman, Insomnia, or Thunder Client (VS Code extension) to test all your endpoints:
- GET http://localhost:3000/api/products — returns all products
- GET http://localhost:3000/api/products/1 — returns product with ID 1
- POST http://localhost:3000/api/products with a JSON body — creates a product
- PUT http://localhost:3000/api/products/1 with a JSON body — updates product 1
- DELETE http://localhost:3000/api/products/1 — deletes product 1
Next Steps
Now that you have a working REST API, here is what to learn next:
- Connect to a real database using MongoDB with Mongoose or PostgreSQL with Prisma
- Add authentication using JSON Web Tokens (JWT)
- Add input validation using a library like Joi or Zod
- Structure your code into separate route files and controllers
- Deploy your API to a cloud platform like Railway, Render, or DigitalOcean
Final Thoughts
You have just built a fully functional REST API with Node.js and Express. This is the foundation that powers thousands of real-world web applications. The patterns you learned here — routes, controllers, HTTP methods, and JSON responses — are universal and apply to any framework or language you use in the future.


