How to Create A REST API With JSON Server ?
Last Updated :
15 May, 2024
Setting up a RESTful API using JSON Server, a lightweight and easy-to-use tool for quickly prototyping and mocking APIs. JSON Server allows you to create a fully functional REST API with CRUD operations (Create, Read, Update, Delete) using a simple JSON file as a data source.
Approach
- First, Create a JSON file that represents your data model. This JSON file will serve as your database.
- Run JSON Server and point it to your JSON file using the command npx json-server --watch users.json.
- Create four different JavaScript files to perform the operations like Create, Read, Update, Delete.
- To Send a GET Request use the command in the terminal node get_request.js that returns a list of all users stored on the server, Send a POST request to create a new user, this will create a new user with the provided data.
- Similarly, Send a PUT request to update an existing user. This will update the user with the provided data and for DELETE Request, The endpoint deletes the user with the specified id from the server. After deletion, the user will no longer exist in the database.
Steps to create a REST API with JSON Server
Run the below command to Create a package.json file:
npm init -y
Run the below command to Install the JSON Server globally using npm:
npm i -g json-server
Run the below command to Install Axios:
npm install axios
Project Structure:

Example: The example below shows the JSON file that represents your data model.
JavaScript
{
"users": [
{
"id": "1",
"first_name": "Ashish",
"last_name": "Regmi",
"email": "ashish@gmail.com"
},
{
"id": "2",
"first_name": "Anshu",
"last_name": "abc",
"email": "anshu@gmail.com"
},
{
"id": "3",
"first_name": "Shreya",
"last_name": "def",
"email": "shreya@gmail.com"
},
{
"id": "4",
"first_name": "John",
"last_name": "aaa",
"email": "John@yahoo.com"
},
{
"first_name": "Geeks For",
"last_name": "Geeks",
"email": "gfg@gmail.com",
"id": "5"
}
]
}
Run the below command to start JSON Server and point it to your JSON file:
npm json-server --watch users.json
GET Request Returns a List of all Users
Example: In this example the endpoint returns a list of all users stored on the server. Each user object contains properties such as id, name, and email.
JavaScript
//get_request.js
const axios = require('axios');
axios.get('http://localhost:3000/users')
.then(resp => {
data = resp.data;
data.forEach(e => {
console.log(`${e.first_name},
${e.last_name}, ${e.email}`);
});
})
.catch(error => {
console.log(error);
});
Run the below command to test the GET request:
node get_request.js
Output:
Ashish, Regmi, ashish@gmail.com
Anshu, abc, anshu@gmail.com
Shreya, def, shreya@gmail.com
John, aaa, John@yahoo.com
Geeks For, Geeks, gfg@gmail.com
POST Request to create a New User
Example: In this example, Send a POST request to create a new user will create a new user with the provided data.
JavaScript
//post_request.js
const axios = require('axios');
axios.post('http://localhost:3000/users', {
id: "6",
first_name: 'Geeks for',
last_name: 'Geeks',
email: 'gfg@gmail.com'
}).then(resp => {
console.log(resp.data);
}).catch(error => {
console.log(error);
});
Run the below command to test the POST Request:
node post_request.js
Output:
{
"first_name": "Geeks For",
"last_name": "Geeks",
"email": "gfg@yahoo.com",
"id": "5"
}
PUT Request to Update an Existing User
Example: In this example, send a PUT request to update an existing user this will update the user with the provided data.
JavaScript
//put_request.js
const axios = require('axios');
axios.put('http://localhost:3000/users/5/', {
first_name: 'Geeks For',
last_name: 'Geeks',
email: 'gfgofficial@gmail.com'
}).then(resp => {
console.log(resp.data);
}).catch(error => {
console.log(error);
});
Run the below command to test the PUT Request:
node put_request.js
Output:
{
first_name: 'Geeks For',
last_name: 'Geeks',
email: 'gfg@yahoo.com',
id: '5'
}
Above data is modified as
{
first_name: 'Geeks For',
last_name: 'Geeks',
email: 'gfgofficial@gmail.com',
id: '5'
}
DELETE Request to Delete a User
Example: In this example the endpoint deletes the user with the specified id from the server. After deletion, the user will no longer exist in the database.
JavaScript
//delete_request.js
const axios = require('axios');
axios.delete('http://localhost:3000/users/3/')
.then(resp => {
console.log(resp.data)
}).catch(error => {
console.log(error);
});
Run the below command to test the PUT Request:
node delete_request.js
Output:
{
id: '3',
first_name: 'Shreya',
last_name: 'def',
email: 'shreya@gmail.com'
}
Similar Reads
How to Create A REST API With JSON Server ?
Setting up a RESTful API using JSON Server, a lightweight and easy-to-use tool for quickly prototyping and mocking APIs. JSON Server allows you to create a fully functional REST API with CRUD operations (Create, Read, Update, Delete) using a simple JSON file as a data source. Table of Content GET Re
4 min read
How to create a REST API using json-server npm package ?
This article describes how to use the json-server package as a fully working REST API. What is json-server? json-server is an npm(Node Package Manager) module/package, used for creating a REST API effortlessly. Data is communicated in JSON(JavaScript Object Notation) format between client and server
4 min read
How to Create a MySQL REST API
Creating a REST API is important for enabling communication between different software systems. MySQL is one of the most popular relational database management systems which serves as the backbone for data storage in web applications. In this article, we will learn how to create a REST API using MyS
6 min read
How to Post JSON Data to Server ?
In modern web development, sending JSON data to a server is a common task, especially when working with RESTful APIs. JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy to read and write for humans and easy to parse and generate for machines. This article will gu
4 min read
How to Test API with REST Assured?
REST Assured is a Java library that provides a domain-specific language (DSL) for writing powerful, easy-to-maintain tests for RESTful APIs. It allows you to specify the expectations for HTTP responses from a RESTful API, and it integrates seamlessly with JUnit, the most popular testing framework fo
5 min read
Using Restify to Create a Simple API in Node.js
Restify is an npm package that is used to create efficient and scalable RESTful APIs in Nodejs. The process of creating APIs using Restify is super simple. Building a RESTful API is a common requirement for many web applications. Restify is a popular Node.js framework that makes it easy to create RE
6 min read
How to Create and Verify JWTs with Node?
In this article, we will see how to create JWT tokens in Node.js. We will implement secure authentication in Node.js by creating and verifying JSON Web Tokens (JWTs) using libraries like `jsonwebtoken`. Prerequisites:Good knowledge of JavaScript.Basic knowledge about Express JS.Basic knowledge about
5 min read
How to create mock servers using Postman
Postman is a comprehensive API platform used by developers; this platform provides a set of tools that support API design, testing, documentation, mocking, and API Monitoring. It also simplifies each step of the API lifecycle and streamlines collaboration, enabling developers to create and use APIs
7 min read
How to create and write tests for API requests in Postman?
Postman is an API(utility programming interface) development device that enables to construct, take a look at and alter APIs. It could make numerous varieties of HTTP requests(GET, POST, PUT, PATCH), store environments for later use, and convert the API to code for various languages(like JavaScript,
3 min read
How to Build an API With Ruby on Rails?
Ruby on Rails API refers to the application programming interface (API) framework provided by the Ruby on Rails (Rails) web application framework. It allows developers to build and expose APIs for their web applications efficiently. Ruby on Rails is a popular web development framework written in the
4 min read