top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-
Writer's pictureShruti Bhatnagar

How to Create your Own API

APIs are everywhere, and they play a vital role in modern-day technology. Application Programming Interface, which is a software intermediary that allows two applications to talk to each other. Each time you use an app like Facebook, send an instant message, or check the weather on your phone, you’re using an API. Everything is an API nowadays, and learning to create one yourself is a fundamental step.

In this blog, I will discuss beginner-friendly way to create your own API with json server. JSON Server is a Node Module that you can use to create demo REST JSON services within a short span of minutes. All we need to do is have a JSON file as sample data. Let’s learn more about how to install and set up a JSON server in our system.

Installing JSON Server

JSON Server is available as a NPM package. The installation can be done by using the Node.js package manager:

$ npm install -g json-server

By adding the -g option we make sure that the package is installed globally on your system.The next step involves the creation of a database with json. For the sake of learning, we have created an example database as shown below.

{

"dolls": [

{ "id": 1,

"name": "Barbie",

"movie": "Barbie A fairy Secret"

},

{ "id": 2,

"name": "Elsa & Anna",

"movie": "Frozen"

},

{ "id": 3,

"name": "Rapunzel",

"movie": "Tangled"

}

],

"lego": [

{ "id": 1,

"name": "Andrea",

"movie": "Andrea's Big Moment"

},

{ "id": 2,

"name": "Emma",

"movie": "Emma's Dilemma"

},

{ "id": 3,

"name": "Stephanie",

"game": "Stephanie's Surprise Party"

}

]

}

The next step is to start the json server and to do that use the below command.

json-server --watch db.json --port 8080

If we now make a request to http://localhost:8080/dolls/1, we should get back:

{ "id": 1, "name": "Barbie", "movie": "Barbie A fairy Secret" }

The following HTTP endpoints will be created automatically by JSON server too:

GET /dolls 
GET /dolls/{id} 
GET /lego 
GET /lego/{id} 
POST /dolls 
POST /lego 
PUT /dolls/{id} 
PUT /lego/{id} 
PATCH /dolls/{id} 
PATCH /lego/{id} 
DELETE /dolls/{id} 
DELETE /lego/{id}

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 a 200 OK but without changes being made to the data.

With JSON Server, we can filter results by adding query parameters to our requests. If we hit http://localhost:8080/lego?name=Stephanie, it should return an array of all the lego objects in the data that match the given criteria.

We can also do a full-text search with the q parameter:

(Returns the users which contain the string “Emma”).

Conclusion

And there you have it: your own REST API! I hope this blog helps you get started in developing your own API.




82 views0 comments

댓글

별점 5점 중 0점을 주었습니다.
등록된 평점 없음

평점 추가
bottom of page