Everything you need to know about API

Everything you need to know about API

API stands for Application Programming Interface. It acts as a mediator and facilitates communication between two applications. An API is basically a set of rules/protocols which sends a request to a database and retrieves data from it. This mechanism can be best explained in terms of client and server.

client-server-1.png

A client is an end-user who uses the data and the server is the one that provides the data. Whenever there is an event ( eg: clicking on a button) happening on a web page (client side), there is some kind of action that takes place. That action is most probably a call to a database (server side) to extract the required data and this is where an API comes. An API is a bridge between a client and the server which helps in connecting the two entities. In general, a call to a database is not performed directly but instead using an API as a moderator. This is to enhance the security since each API has a private key using which the client requests the data from a database.

Capture.JPG

Whenever an end-user makes a request, an API call is made with the requested data. For instance, if a user wants to extract weather data for his city, he/she would be calling an API that contains the required arguments like name of the city, latitude & longitude, etc. along with his private API key. Now that the API call has all the information needed to ask for the data, it requests a database to give the weather data as per what the client has asked. If the database doesn't have data for requested parameters, then there is an error generated and that error is displayed on the client side. But, if the requested parameters match the contents in the database, then the corresponding data is provided by the database to the API as a response to the request and the API carries the data back to the requester and delivers it.

weatherstack-forecast.gif

Some real-world examples of this mechanism are flight booking, google maps, food ordering, etc. One of the main reasons behind using an API is that it provides a layer of security. The client's data is never exposed to the server and the server's data is never exposed to the client. Instead, each of these entities communicate using small packets of data and shares only what is necessary.