docs / articles / Simple REST API Example

Simple REST API Example

Sara Jul 09, 2026 2026-07-09 04:40:45

Welcome to our comprehensive guide on creating a simple REST API. REST, or Representational State Transfer, is an architectural style that defines a set of constraints for building web services. In this article, we'll walk you through the process of creating a simple REST API using Node.js and Express.js, a popular web application framework for Node.js.

REST API Fundamentals.
REST API Fundamentals.

Before we dive into the code, let's understand why REST APIs are important. REST APIs allow different applications to communicate with each other, enabling them to share data and functionality. They are lightweight, cacheable, and scalable, making them a popular choice for building web services.

GitHub - zero-equals-false/node-rest-api-template: Simple Template for Building a REST API using Node.js and Fastify
GitHub - zero-equals-false/node-rest-api-template: Simple Template for Building a REST API using Node.js and Fastify

Setting Up the Project

To start, we need to set up a new Node.js project and install the necessary dependencies.

REST-API методы и их назначение 🚀
REST-API методы и их назначение 🚀

First, initialize a new Node.js project by running `npm init` in your terminal. This will create a `package.json` file in your project directory. Then, install Express.js by running `npm install express`.

Creating the Server

#restapi #backenddevelopment #apidesign #webdevelopment #programmingtips… | Mohammed Surguli | 17 comments
#restapi #backenddevelopment #apidesign #webdevelopment #programmingtips… | Mohammed Surguli | 17 comments

Now, let's create a simple server using Express.js. In your main JavaScript file (e.g., `app.js`), import Express and create an instance of it:

```javascript const express = require('express'); const app = express(); ```

Next, define the port number where your server will listen for requests:

```javascript const PORT = process.env.PORT || 3000; ```

Then, start the server by using the `listen` method:

REST API Basics Explained — Simple Developer Guide (2025)
REST API Basics Explained — Simple Developer Guide (2025)

```javascript app.listen(PORT, () => { console.log(`Server is running on port ${PORT}`); }); ```

Now, when you run your application, it will start a server that listens for requests on the specified port.

Defining Routes

In a REST API, routes define the endpoints that clients can use to interact with your application. Let's define a simple route for a "Hello, World!" message:

Python: Simple Rest API Example and String Formatting
Python: Simple Rest API Example and String Formatting

```javascript app.get('/hello', (req, res) => { res.send('Hello, World!'); }); ```

In this example, we're defining a GET route at the `/hello` endpoint. When a client sends a GET request to this endpoint, the server will respond with the message "Hello, World!".

Handling HTTP Methods

Simple REST API With PHP MySQL (Step-by-Step Example)
Simple REST API With PHP MySQL (Step-by-Step Example)
How REST APIs Work (Beginner-Friendly Guide)
How REST APIs Work (Beginner-Friendly Guide)
How APIs Work for Beginners | API Basics Explained
How APIs Work for Beginners | API Basics Explained
REST API Basics with Express.js — How Endpoints Are Structured
REST API Basics with Express.js — How Endpoints Are Structured
REST API Methods Explained Simply – GET vs POST vs PUT vs DELETE Guide
REST API Methods Explained Simply – GET vs POST vs PUT vs DELETE Guide
What is API and REST APIs? – In easy language
What is API and REST APIs? – In easy language
Build Your First Python API in 6 Steps | Flask Tutorial for Beginners
Build Your First Python API in 6 Steps | Flask Tutorial for Beginners
REST API Complete Guide: HTTP Methods, JSON, Status Codes | CoderCrafter
REST API Complete Guide: HTTP Methods, JSON, Status Codes | CoderCrafter
DocPatient Nexus REST API
DocPatient Nexus REST API
the rest api application is shown in this diagram, which shows how to use rest apis
the rest api application is shown in this diagram, which shows how to use rest apis
A Beginner's Tutorial for Understanding RESTful API [MLSDev]
A Beginner's Tutorial for Understanding RESTful API [MLSDev]
Como construir una API RESTful - Guía paso a paso
Como construir una API RESTful - Guía paso a paso
What is an API? A Simple Guide for Beginners (With Real Examples)
What is an API? A Simple Guide for Beginners (With Real Examples)
When to use PUT or POST in a RESTful API and Web Service? Answer
When to use PUT or POST in a RESTful API and Web Service? Answer
Announcing the Remote Config REST API
Announcing the Remote Config REST API
Mastering RESTful API Design: A Practical Guide
Mastering RESTful API Design: A Practical Guide
Best language and framework to create Rest API
Best language and framework to create Rest API
REST API or SOAP API, Which one is for you ?
REST API or SOAP API, Which one is for you ?
🔍Check out this Flow Diagram on REST API Authentication Methods!🔐
🔍Check out this Flow Diagram on REST API Authentication Methods!🔐
Retrofit Android Tutorial - Using Retrofit 2.2 with PHP Restful API
Retrofit Android Tutorial - Using Retrofit 2.2 with PHP Restful API

REST APIs use different HTTP methods to perform different actions. Let's explore how to handle the most common methods in Express.js.

GET Requests

GET requests are used to retrieve information from the server. We've already seen an example of a GET request in the previous section. Here's another example that retrieves a list of users:

```javascript let users = [ { id: 1, name: 'John Doe' }, { id: 2, name: 'Jane Doe' }, ]; app.get('/users', (req, res) => { res.json(users); }); ```

In this example, the server responds with a JSON array of users when a client sends a GET request to the `/users` endpoint.

POST Requests

POST requests are used to create new resources on the server. Let's add a route to create a new user:

```javascript let currentId = 3; app.post('/users', (req, res) => { const newUser = { id: currentId++, name: req.body.name }; users.push(newUser); res.status(201).json(newUser); }); ```

In this example, the server creates a new user with the provided name and responds with a 201 status code and the newly created user in the response body.

PUT Requests

PUT requests are used to update existing resources on the server. Let's add a route to update an existing user:

```javascript app.put('/users/:id', (req, res) => { const userId = parseInt(req.params.id); const userIndex = users.findIndex(user => user.id === userId); if (userIndex === -1) { return res.status(404).send('User not found'); } users[userIndex].name = req.body.name; res.json(users[userIndex]); }); ```

In this example, the server updates the user with the specified ID and responds with the updated user in the response body.

DELETE Requests

DELETE requests are used to remove resources from the server. Let's add a route to delete a user:

```javascript app.delete('/users/:id', (req, res) => { const userId = parseInt(req.params.id); const userIndex = users.findIndex(user => user.id === userId); if (userIndex === -1) { return res.status(404).send('User not found'); } users.splice(userIndex, 1); res.sendStatus(204); }); ```

In this example, the server removes the user with the specified ID and responds with a 204 status code.

And there you have it! You've created a simple REST API using Node.js and Express.js. This API allows clients to retrieve, create, update, and delete users using the appropriate HTTP methods. As your application grows, you can add more routes and functionality to your API.

Remember, the key to a good REST API is to keep it simple and focused on the needs of your clients. By following the principles of REST and using a framework like Express.js, you can build APIs that are easy to use, maintain, and scale.

Now that you've learned the basics of creating a simple REST API, it's time to put your knowledge into practice. Start by building a simple API for a project you're working on, or use this guide as a starting point to learn more about REST APIs and Express.js.