How to Build a Simple REST API with Node.js and Express (Beginner Friendly)

APIs power almost everything on the internet.

From mobile apps and SaaS platforms to eCommerce stores and CRMs — they all rely on APIs to send and receive data.

In this tutorial, you’ll learn how to build a simple REST API using Node.js and Express in under 30 minutes.

No fluff. Just practical steps.

What We’re Building

We’ll create a basic API that:

  • Returns a list of users
  • Allows you to add a new user
  • Responds with JSON
  • Runs locally on your machine

Step 1: Install Node.js

If you don’t already have it installed, download Node.js from:

https://nodejs.org

To confirm installation, run:

				
					node -v
npm -v
				
			

If you see version numbers, you’re ready.

Step 2: Create a New Project

Create a new folder:

				
					mkdir simple-api
cd simple-api
				
			

Initialize a Node project:

				
					npm init -y
				
			

This creates your package.json file.

Step 3: Install Express

Now install Express:

				
					npm install express
				
			

Express is a minimal web framework that makes building APIs easy.

Step 4: Create Your Server File

Create a file called:

				
					index.js
				
			

Add the following code:

				
					const express = require("express");

const app = express();
const PORT = 3000;

// Middleware to parse JSON
app.use(express.json());

// Temporary in-memory data
let users = [
  { id: 1, name: "John" },
  { id: 2, name: "Sarah" }
];

// GET route - Fetch all users
app.get("/users", (req, res) => {
  res.json(users);
});

// POST route - Add new user
app.post("/users", (req, res) => {
  const newUser = {
    id: users.length + 1,
    name: req.body.name
  };

  users.push(newUser);
  res.status(201).json(newUser);
});

// Start server
app.listen(PORT, () => {
  console.log(`Server running on http://localhost:${PORT}`);
});
				
			

Step 5: Run the Server

In your terminal:

				
					node index.js
				
			

You should see:

				
					Server running on http://localhost:3000
				
			

Step 6: Test Your API

Test GET Request

Open your browser and go to:

				
					http://localhost:3000/users
				
			

You should see:

				
					[
  { "id": 1, "name": "John" },
  { "id": 2, "name": "Sarah" }
]
				
			
Test POST Request

Use a tool like Postman or VS Code Thunder Client.

Send a POST request to:

				
					http://localhost:3000/users
				
			

With JSON body:

				
					{
  "name": "Michael"
}
				
			

You should get:

				
					{
  "id": 3,
  "name": "Michael"
}
				
			

And if you refresh /users, the new user appears.

What Just Happened?

You built a working REST API with:

  • Express server
  • JSON middleware
  • GET endpoint
  • POST endpoint
  • Basic data storage

 

This is the foundation of:

  • SaaS platforms
  • Mobile backends
  • Admin dashboards
  • CRM systems
  • Custom business tools

Next Steps

To level this up, you can:

  • Add PUT and DELETE routes
  • Connect to MongoDB or PostgreSQL
  • Add validation
  • Add authentication
  • Use environment variables
  • Deploy to a cloud platform

Get in touch with us!

Ready to bring your digital vision to life? Drop us a message, and our expert team will get back to you to discuss how we can turn your ideas into reality. Let’s create something amazing together!

Explore how we can help you succeed. Check out our full range of services now!