Introduction to API
API stands for Application Programming Interface. As the name suggests, it creates an interface between an application and a program. Let’s try to understand with an example: When you go on Google or Bing, you search for a Twitter website. You write Twitter.com, and directly a page gets opened up. And you see the homepage of Twitter. The question arises. How does it directly open only the homepage?
Twitter.com is a domain name. Behind every domain name, there is always an IP address. A domain name is like the mask of an IP address. That’s why when we search Twitter.com it calls the IP address of the server.
How does API works?
When you search for any person’s profile on Twitter, and you like that person’s profile you start following him. Immediately we noticed that the follow button switched to unfollow on the same location. And, that particular page is not refreshed. Then comes the part or role of API (Application Programming Interface). When you press the follow button, then that request goes to the server behind the scene that will be on the API endpoint.
The request will be in the form of twitter.com/api/follow/name_of_the_person that is the profile of the person you want to follow. Whenever you hit that endpoint that is the API-application programming interface endpoint then the server can do the logic of following and it can return the response. This response can be either a success response or a failure response. According to the response, you flip the button to follow or unfollow. If there is an error or an issue, then we can see an error message on our screen.
Now let’s talk about the benefit of using this API or API endpoint. We are on a browser, and we are not refreshing the page and still, we can update the application at that time. This API endpoint can be used to open the mobile application, and you would have seen that. When we open the Twitter mobile application, then we hit the follow button. Immediately that follow button switches to unfollow button.
How it is done?
This is all done with the help of API. The same process takes place on two different devices, one is a browser and the other one is a mobile application. You can also use an API between IoT devices like there is a fire alarm in your building. That fire alarm turns on and detects the fire somewhere in its range. Then it sends a request to the fire store or the people that are appointed to help you in the time of the fire. Those people will immediately get to know that there is a fire at that place. Then those people will rush to that place and will help the person who is in danger.
Nowadays, you are surrounded by different types of APIs. In the market, there are lots and lots of APIs available. Nowadays, we are having two different kinds of APIs these are:
- Restful API
- GraphQL API
Now let’s just talk about the Restful API. In the Restful API, you use different endpoints to perform different tasks. Suppose you want to delete something you hit the endpoint with the delete button. If you want to update something, then you will have two options- one is put, and the other is a patch. In the put method, you do changes in everything, and in the patch method, you update only one thing. Like if you want to update your username you can do that with the help of a patch. If you want to store something use the post method and if you want to get only the data then use the get method as the name suggests. This is very simple in the Restful API.
But the problem with the Restful API is that you have to create lots and lots of endpoints. For a big project, it will be difficult to manage all these endpoints. Here comes the need for the new type of API that is GraphQl API.
In the GraphQl API, you only have a single endpoint for the entire thing. The benefit of this is, the front-end people who are using this API have to hit only one and a single API endpoint and on that, you can define what exactly you want to do. Like, if you want to update something, you do the mutation things, and if you want to get something, you do query things. Here everything is on the single endpoint. That is the ease and benefit of using GraphQl API.
RESPONSE TYPE IN API:
Now to consider the API you need to consider the response type also because this is very important since we can use the API in different forms in different places like on devices like on the mobile application and the browser. Consistency should be there. Whenever you hit an API endpoint the format of that is generally in JSON format. How someone will be able to know what will be the format and what should be the format of an API. If you are a developer you may know that in the header we generally send the containing type as the application/JSON. This represents the content we are sending in the form of JSON. And you also have seen that the acceptance and value is application JSON again. This shows that the response we are accepting is going to be of type JSON.
Nowadays, some APIs are also using XML. XML is an old format, and JSON is easy to read, and that’s why it is in use everywhere.