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": "[email protected]"
},
{
"id": "2",
"first_name": "Anshu",
"last_name": "abc",
"email": "[email protected]"
},
{
"id": "3",
"first_name": "Shreya",
"last_name": "def",
"email": "[email protected]"
},
{
"id": "4",
"first_name": "John",
"last_name": "aaa",
"email": "[email protected]"
},
{
"first_name": "Geeks For",
"last_name": "Geeks",
"email": "[email protected]",
"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, [email protected]
Anshu, abc, [email protected]
Shreya, def, [email protected]
John, aaa, [email protected]
Geeks For, Geeks, [email protected]
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: '[email protected]'
}).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": "[email protected]",
"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: '[email protected]'
}).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: '[email protected]',
id: '5'
}
Above data is modified as
{
first_name: 'Geeks For',
last_name: 'Geeks',
email: '[email protected]',
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: '[email protected]'
}
Similar Reads
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.I
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 Create a REST API using Java Spring Boot?
Representational State Transfer (REST) is a software architectural style that defines a set of constraints for creating web services. RESTful web services allow systems to access and manipulate web resources through a uniform and predefined set of stateless operations. Unlike SOAP, which exposes its
4 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
Create a CRUD API With PostgREST
In today's fast-paced world, businesses rely heavily on efficient data management systems to streamline operations and deliver optimal services. One such tool that has gained popularity for its simplicity and effectiveness is PostgREST. In this article, we'll explore how you can harness the power of
5 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
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 API in Ruby on Rails?
Building APIs with Ruby on Rails: A Step-by-Step GuideRuby on Rails (Rails) is a popular framework for web development, known for its convention over configuration approach. It also excels in creating robust and maintainable APIs (Application Programming Interfaces). APIs act as intermediaries, allo
3 min read