Blogs


What exactly is an API?

 March 26, 2021, 02:23 PM

 5 min read

API
Restful-API
Rest

I have always had new programmers asking me, "Arafat, what is an API?" . For some reason, I struggle to give a reasonable explanation about APIs whilst also trying not to scare them away. In this blog, I will try my best to offer you an introduction to APIs, why we use them and what are some general ways of implementing them.


An API or Application Programming Interface is a medium through which two or more different systems or computer programs might interact or communicate. It acts as a bridge between programs to offer a service. A famous non-technical real life example of an API is going to a restaurant and ordering food. When you go to the restaurant, you are the client. You get your food from the kitchen, where it is processed, cooked, etc., and then served to you. It is a provider or a server. But you need to communicate with the kitchen to order your food. How do you do that? Through a waiter of course, who will be there to take your order and then pass it on to the kitchen. Once your food is prepared, the waiter will bring your food from the kitchen and serve it in a manner specified by the restaurant managers, which is called specifications. The waiter bridges the gap between the client and the server through a communications channel, or an API.
(The following example is taken from mulesoft.)


When do we use APIs

We looked at a real life example of an API, now let us look at a more technical use case. If you go to the blogs section to browse through all the blogs, you will notice that there is a search bar for all the blogs on my website. How does it work? It works by sending the query from the search bar, or the payload, to the server through an API channel, performing various security checks along the way, where the payload is then analyzed, and the relevant blogs are retrieved from the database and sent back through the communications channel or the API to the user or the client, where it is then displayed.

Usage of API is more mainstream in our daily lives than just browsing through blogs.

  • Searching for a user in twitter: where the user name is the payload which is sent to the server and some relevant users are returned back as reponse.

  • Attempting to login to your Gmail account: where your login credentials are the payload, and a message if they login was a success is returned as response.

  • Uploading a photo to Instagram: the image is the payload which is sent to the database to upload and store.

The aforementioned examples are just some of the use cases of APIs through internet APIs in our day-to-day lives.

However, API is not necessarily restricted to cross internet communications. It can also be used within systems for two different applications to interact.

  • Your phone has a physical camera, which can take photos and videos. When you download an app that utilizes the camera, it requests permission to do so. Here there is a camera API on your phone which allows the third party app to communicate with the camera and take photos and videos.

  • Every client-sided device has a geolocation API, which allows other third party apps to access information about the users current location.


How to implement APIs

You will need to build a client side and a server side. Client side is relatively simple. For web apps all you have to do is to use the XMLHttpRequest API or the more recent fetch API to send requests to an endpoint, which will be the server. The server, which can be written either in django, node.js, ruby, java, etc, will retrieve and process the request to produce a response in JSON or JavaScript Object Notation which will be sent back to the client. Modern APIs are governed by a set of rules and constraints for APIs called REST API or Representational state transfer. APIs which are in compliance with the REST protocol are called REST APIs.


Arafat

Mohammad Arafat Zaman

"Technophile"


Go back

Mohammad Arafat Zaman © 2024


All rights reserved