What exactly is API?
Being a developer, we all must have come across a common term called “API”. We all must have used API in our project or must have heard of the term “API” from our team members while developing a project. But the question that arises here is “What is API?”. Is it some language or tool or framework or method or something else? What exactly is it? Don’t worry, in this blog, we will learn “ What exactly is API? ” in the best and easiest possible way. So, without wasting our time let’s get started.
To make the blog easier to understand for you, we have divided the whole blog into the following topics:
- Learning API from the conceptual point of view
- Advantages of API
- Learning API from a development point of view
- Software library and framework
- Operating System
- Remote APIs
- Web APIs
- Learning API from the web development point of view
- Closing notes
If you want to go through the video on “What exactly is API?”. You can watch here .
Learning API from the conceptual point of view
API stands for Application Programming Interface . To understand the meaning of API, we must have to understand the meaning of Interface. Let’s take a real-life example to understand the meaning of Interface and after learning interface, we will learn the meaning of API.
In the above image, you can see one radio having two knobs. One knob is responsible for changing the frequency of the radio channel to connect with different stations and the other knob is responsible for changing the volume of the songs played on the radio. Apart from the external things of the radio, there is some internal mechanism that is responsible for connecting your radio to different radio channels with the help of an antenna.
But if you think from the user perspective, you have only two options available with you i.e. either you can change the station on the radio or you can increase or decrease the volume of the music that is playing on the radio.
So, if you consider the radio as a system and yourself as the user of the system, then you are not bothered about the internal working of the radio. All you want is listening to music from different channels on the radio. That’s it. You are not going to connect wires to change the frequency or you are not going to add some registers to change the volume of the radio. All you need to do is just use the knob that will change the frequency and volume for you without performing any internal work on the radio.
In the above example, the radio is our system and it is providing two things i.e. two knobs for changing the frequency and for increasing/decreasing the volume. So, the radio here is an example of a system and the two knobs that are present on the radio are the APIs (Interface) for that system. You can consider the first knob that can change the frequency as API1 and the second knob that can change the volume as API2.
So, API is something which a system provides to the user so that the user can easily achieve what it wants, without bothering about the system architecture or the internal working of the system.
Advantages of API
We have seen the conceptual point of view of API. Let’s look at some of the advantages of using API.
- No bothering of internal working: With the help of APIs, the users perform their task without bothering about the internal working of the system. For example, while using the radio, we are less bothered about the working of the frequency receivers or the speakers. We want to listen to music and nothing else.
- No effect of an internal change on users: If there is some internal change in the system then, the user need not bother about that. The user will achieve the desired thing in the same manner as done before. For example, if the frequency receiver of the radio is changed to something else like a capacitor or register, then the user need not bother about that. All he/she needs to do is just use the knobs present on the radio and enjoy the music.
Learning API from a development point of view
Till now, we have seen the conceptual point of view of APIs and some advantages of API. Now, in this section of the blog, we will learn API from a development point of view because this is most important. Everyone uses API in development. So, we must understand API from the developer’s view also. We can categorize the usage of API in development into the following categories:
- Software Library and Framework
- Operating System
- Remote APIs
- Web APIs
Let’s learn all of them, one by one.
Software Library and Framework
The software library is what you develop. For example, if you are writing some code for your Android application then, you will interact with the OS of Android. You have got various methods from the Android framework and with the help of those methods, you can interact with the OS.
So, in simpler words, if you have written some software library to achieve something and uploaded your library to GitHub or somewhere else, and if a user is using your software library with the help of methods that you have provided then it is called as API. Here, the user using your library is not bothered about the code of the library. He/she is only using the methods provided by you.
What does API means for procedural language and object-oriented language?
If you have written a library in some procedural language, then the user of your library will use a function (with various parameters) provided by you to get the desired output from the library. For example, if you have built a library to process an image, then the user can pass an image bitmap with filter flag to a function that produces an image by applying that filter on it.
For an object-oriented language, API from the software library and framework means the classes and their methods which are exposed by the library or framework to the user of that framework. The user only uses that methods and he/she is least interested in the internal working of the library.
In Android Framework, we have many libraries that are exposed to us. For example, to use a camera in an Android device, we can use the API provided by the Android Framework to capture an image.
Operating System
An Operating System provides a way to interact with the system resources for an application through various exposed functions.
For example, if you want to write something on a file on your computer, then the operating system will provide you with a function that will open the file for you and you can read or edit it. All you need to do is pass the path of the file and the rest of the thing will be handled by the OS. You need not worry about the internal working involved in opening a file from the storage. So, the methods provided by the OS to interact with the system resources is APIs.
Remote APIs
APIs can also be categorized from the perspective that how you are trying to access the resources. So, you can have remote APIs also.
For example, Java provides database connection framework which helps to connect databases which are functional in remote servers. The best part about this is you need not worry about the type of the database. For example, your database can be MySQL or PostgreSQL or something else. But with the help of methods provided to you, you can access the database of any type and can change accordingly.
Web APIs
Most of the time, APIs are referred from the perspective of the Web APIs and this is the most general context in which people talk about APIs. Web APIs comes into the picture when there is an interface between the enterprise and the application.
For example, you have your server and it is providing some services like streaming videos or accessing your social networks or something else. So, that enterprise will provide you with some APIs and with the help of those APIs, you can interact with those services present on your server.
To access the services provided by the enterprise, APIs provides URLs. So, form the Web perspective, if I am talking about API then it can be said that I am going to use HTTP protocol and in return, I am going to get the desired response.
Learning API from the web development point of view
In this part, we are going to learn about APIs from the web development point of view because APIs are mostly used in web development.
Basically, we create some web services and we provide means for the client to access these web services. Let’s understand this with the help of an example.
Suppose you are a company and you are having a list of products. So, if your user wants to get the list of products available in your company then you must provide some way or mechanism to do so. In this case, to access the web service i.e. the list of products, you will provide an API to the users or in simple words, you will provide an HTTP request because your user is trying to access the services over the internet.
So, you may be having a service running at your backend and you expose one method of that infrastructure that you have created which can be accessed by the HTTP request or simply a URL. The URL may look something like this:
http://company.com/flight?date=” ”
Here, you have to supply the below things:
- HTTP endpoint for the API.
- GET or POST methods that will be called.
- Request format (here the query parameters).
- headers such as authentication headers.
- Response format i.e. to indicate the format of the response i.e. it can be in JSON or XML or something else.
- Type of error (401, 403, 500 etc) because if some error happens then it should be known and the system can respond accordingly.
In modern systems, these APIs plays an important role. Suppose you are an aggregator company and you have a web portal where a user can see the list of flights available from various companies. So, you can call the APIs provided by all the companies one by one and get the list of flights from all the companies. We can call multiple companies APIs here and in return, we can make a hybrid system.
Closing notes
In this blog, we learned a lot of things about APIs. We saw the conceptual view of API. We saw how developers should understand the concept of API. In the end, we saw the meaning of API from the perspective of web developers also. That’s it for this blog.
We hope you have understood what API is and how it is being used in the development environment.
Keep Learning :)
Team AfterAcademy!