As in the previous article we went over the concept of how the data is being transferred between server and client, along with the concept of status codes, used for acknowledging the requests and corresponding responses. If you haven’t read that, I would recommend to read the previous article, as this continues from where it left.
To read previous article: CLICK HERE
So, What is an API ?
Definition -: API stands for application programming interface. APIs are the little pieces of code that make it possible for digital devices, software applications, and data servers to talk with each other, and they’re the essential backbone of so many services we now rely on.
Now, that is the actual definition, but it is not very clear for many of us to understand. So, lets try to understand it with help of analogy. Let’s go with the most common analogy used to explain API - The Waiter.
You went to a high-class restaurant and want to eat something. So, what you do, go to the kitchen directly to order food ? I don’t think so. The normal workflow is that you give order to a waiter, who takes your order request back to the kitchen and brings back the food you ordered to your table from kitchen.
So, API are like waiters. They act as a middle-man who takes our request to the server and gets the respective response back from the server. As per analogy-:
Kitchen = Server, Customer = Client, Order = Request and Food = Response.
Benefits of API’s over Direct Server calls-:
But doesn’t having a middle-man slows down the response time ?
Yeah it does!! But it has it’s benefits-:
1. Migrating-: Response are usually extracted from database storing multiple data files and records. So, if there occurs any need to switch the database, it is quite easy to do in API as you just need to change the database URL and credentials in API and no more changes are required.
2. Security: Proper authorization can be applied to with API’s which definitely provides much more secure access than giving a user direct access to the server database having many more data, because you can apply restrictions to the end points which run only the queries you want to.
Let’s understand this with help of an example-:
Have you ever used apps like Zomato, UBER, OLA, etc. in which you can see the map and the current location of the cab or order. Where does these apps get map data from ? Do they have their own satellites ?
That’s not the case. They use data from Google Maps server to show it. But they access the data through a Google Maps API, because if they would have direct access, it may cause security breach as Google servers has lots of Data.
So, Google only provides the data required by them through API in JSON format which they use to show the map.
3. Maintainability-: If there is a bug in an API when client makes a call, or some update need to be made in the API response fetched from server for a particular request. We just need to make the changes to the API and deploy the new version and it will work for both server and client. If, we used direct calls, we would need to make changes to both server and client software.
4. Abstraction-: APIs provide level of abstraction, as you don’t have to know the process how it is fetching data from server, how the API is structuring and filtering data. They just need to make a call and they get the data in JSON format, without having to worry about complexities. It also ensures Encapsulation as there may be internal filtering to stop important or unwanted data to return.
Components of an API call-:
As we saw earlier, API is a set of protocol(rules). It is a method to get response from the server.
A- API URL-: The API URL to which you make the request. It has some components of its own-:
1- Protocol-: This specifies the protocol being used while making the API call or Request.
2- Domain-: This specifies the domain name on which the API is hosted. API’s are also lines of codes which control and direct traffic on the internet, so to make them accessible to all, we need to host it on a Cloud/Domain.
3- API Endpoint-: This refers to the particular resource function of the operation we want to do. In an API there are many functions calibrated to do different to request various data from the server. (It may not make full sense now, but it will when you start making one).
4- Parameters-: This optional part of the URL, this passes some query parameters or some values required by API as inputs while fetching data. Though this method is not preferred as data values are given in URL, hence reducing security.
B- API Methods-: It is the type of request method used when calling the API.
For example-: GET, POST, DELETE, PUT etc.
C- Request Headers-: A request header is an HTTP header that can be used in an HTTP request to provide information about the request context. It is like a Metadata of the request.
D- Payload-: This is the data-packet usually send along with requests. This is mainly used in POST and PUT Request methods where we need to send data to the server.
API Development -:
Now we have understood what is an API and how it works. Now moving to main part how to make an API.
For making an API many different languages can be used, each having their own frameworks. Some of the commonly used ones are-:
Language - Python → Frameworks - Flask , Django
Language - JAVA→ Frameworks - Spring
Language - Ruby → Frameworks - Ruby On Rails
and many more……….
The API’s are both public when you are just interested in some common data, but can be custom built for some specific purposes. As shown below-:
This is an example of an API written in Python using FLASK Framework.
So if you focus on first method base. We see 2 lines before the function name-:
(a) app.route -: It has two parameters firstly the API endpoint (discussed above) and methods which store the methods the particular endpoint will accept. If you try any other method other than specified on this endpoint it will give error.
(b)cross_origin -: This is used to allow cross platform calls. For example -:
(c)Function Method -: This is main part of the API this defines what the API does. Everything from how it works, be it from receiving data from server, which data to request from server and pass on to Client.
This cover most of the understanding part of API, now making of API is separate topic of its own. So, I’ll try to cover it in another article. But after reading this article I do hope that most of you would have been able to get a grasp of what is an API and how its function and components.
So till next article, Ciao Adios !!!
Connect with me on-:
and let’s have some geeky talks. 🤓