How to create a RESTful API with JSON Server
august-jk
Posted on October 1, 2022
What is an API?
An API, or Application Programming Interface, is a set of functions and procedures allowing the creation of applications that access the features or data of an operating system, application, or other service.
In other words, an API is an interface that allows applications to communicate with each other and exchange data.
Developers will use APIs to send and retrieve data from servers to use on their applications.
What is a RESTful API?
REST stands for Representational State Transfer and is a type of API that conforms to the constraints of REST architectural style.
In order for an API to be RESTful, it must conform to specific criteria such as:
- A client-server architecture made up of clients, servers, and resources, with requests managed through HTTP
- Stateless client-server communication, meaning no client information is stored between get requests and each request is separate and unconnected
- Cacheable data that streamlines client-server interactions
- A uniform interface between components so that information is transferred in a standard form
RESTful APIs are faster and more lightweight than other, older types of APIs.
Why set up a RESTful API using a JSON Server?
Sometimes front-end developers will need to simulate a back-end REST service to deliver data in JSON format to their front-end application in order to make sure everything is working properly.
Of course, you could set up a full back-end server, but that would take some time to complete.
Creating a RESTful API using a JSON Server is simpler and faster.
Setup
Continue reading to learn how to set up a RESTful API using JSON Server!
Installing JSON Server
JSON Server can be installed as an NPM package.
Type into your terminal:
$ npm install -g json-server
By adding the -g we are installing it globally on your system.
JSON File
Now we'll create a new file called db.json
This file will hold all of your data!
Here is an example of how this file should look:
{
"cats": [
{
"id": 1,
"name": "Fluffy",
"color": "white",
"favorite_toy": "fuzzy ball"
},
{
"id": 2,
"name": "Oreo",
"color": "black and white",
"favorite_toy": "toy mouse"
},
{
"id": 3,
"name": "Luna",
"color": "brown and grey tabby",
"favorite_toy": "laser pointer"
}
]
}
You can see we have a JSON file has an object labelled "cats". Inside the object is an array of 3 different cat objects each containing their name, color, and favorite toy.
This is how the data inside of a JSON Server looks.
Running the Server
Type into your terminal:
$ json-server --watch db.json
Now your server is running and you can begin to make any FETCH requests to http://localhost:3000/cats
If you make POST, PUT, PATCH or DELETE requests, changes will be automatically saved to db.json.
A POST, PUT or PATCH request should include a Content-Type: application/json
header to use the JSON in the request body. Otherwise it will result in no changes being made to the data.
Posted on October 1, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.