APIs explained in layman terms
What is API ? What is Client-Server architecture ? What are the uses of APIs ?
Introduction
APIs (Application Programming Interface) have ushered a revolution in the 21st century. Every second, there are more than trillions of API invocations over the globe. APIs have eased our lives in different ways. We are always using APIs when we either watch our favourite movie online or chatting with our friend on WhatsApp.
I came across the term API almost a decade ago. At that time, I was a novice programmer & didn’t really understand the term. It took me a few days to completely grasp the concept and clearly visualise it in my mind. Like other terms in Computer Science, the term API is also a bit confusing for the beginners.
In this article, I am going to explain what an API actually is in simple words. We will look at different types of APIs. Also, we will understand what a Client-Server architecture is. Finally, we will look at some common usages of APIs. So, let’s begin our journey to understand the concept of APIs.
What is an API ?
In simple words, API is an interface that accepts a set of inputs and produces an output. It consists of code that gets input from the user, processes it and produces some result as an output.
Analogy
A simple analogy is that of a restaurant. In restaurants, we tell the waiter the items we want to order. The waiter then makes a note and places the list in the kitchen. Once the food is prepared, the waiter comes to our table and serves us the meal.
In this case, the waiter doesn’t know which chef prepared the food. Also, he is unaware of the ingredients used for preparation. He just takes the list of food items (input). And then bring the prepared food (output) from the kitchen to the table. The waiter’s job is similar to an API.
Library functions as APIs
Let’s say that we are building a software program for processing employee’s salary. Let’s assume that the data is spread across multiple csv files. We want to combine the files and output some stats. If we decide to write the program in python, we would use a popular library called Pandas. We would first read the csv files using this library. This is how we will write the code to read this file :-
import pandas as pd
file = pd.read_csv(“employee.csv“)
In the above code, the library function read_csv
took the fileName
as the input and returned a data structure known as Dataframe
in python. This library function performed the job of an API. Library functions are also known as language APIs.
APIs over the internet
In the early days of computing, software applications used to reside on a single machine. Before the advent of the internet, there was no way to transfer the data from one device to another.
With advancements in networking and proliferation of the internet, it became possible for devices to talk to each other. This led to emergence of protocols such as TCP and UDP at the network level. Application protocols like HTTP were developed to share information between different computers.
Let’s take the example mentioned in the previous section. Assume that we have a python program running and processing the files on Bob’s laptop. Now, let’s say his friend Alice, also joins and she wants to view the results on her laptop. Also, she wants to sit in a different cabin and view the results whenever she wants. How do we achieve this ?
Client-Server architecture
The problem statement is to fetch the data from one machine and show it on another machine. This is what we are supposed to do in a nutshell. We will break down the problem into two parts.
We will need to run a program on Alice’s machine. This program will fetch the data from Bob’s machine and display it in the right format. Alice can start a browser like Chrome, Firefox or Safari on her laptop. Further, whenever she wants, she can perform an action (click a button) and get the data from Bob’s laptop.
Next, Bob’s program will respond whenever it gets a request from Alice’s machine. In addition, it will also process the csv files.
Since we have two devices that want to communicate over the network, we also need to establish a contract between the two. The contract will define the format of request and response exchanged between the two devices. We can diagrammatically represent the interaction as shown below :-
The entity shown on the left is the Client. The program running Alice’s computer is known as the Client. The program running Bob’s machine is known as the server. The contract between the Client and the Server is API.
Client
Any entity that requests information from another entity is known as a Client. The entity is nothing but a software program. For eg:- In case you are reading this article on browser, then browser is the client. If you are using a mobile app, then the app is a client.
Programs such as command line tools also are clients. There is a renowned tool called curl, that developers use to test APIs. Also, tools like Postman, used for API testing falls under the category of client.
Server
Server is the entity that responds to a client’s request. The server receives an input from the client, then performs some processing and sends the response back.
In case you want to start a server on your machine, you can do it with a single line in python. You need to make sure that python is installed before running the below command on your machine.
python -m http.server
It will produce the below output on the terminal :-
This means that the HTTP server is started on port 8000 on your machine.
You can open a browser tab now and type http://localhost:8000/.
Once you hit enter, it will show the below output:-
Depending on the directory where you run the command python -m http.server
, you will see the files listed in your directory. In my case, I have created three different directories in my home folder.
You can refresh the browser page a couple of times. Now, you can go back to the terminal and see the output.
In my case, I refreshed the page twice. On the terminal, the server program prints the request and the time at which it received the request.
You can try running the curl command to access the same information. Run the following command :-
curl localhost:8000
You will see the below output on the terminal :-
The server returned a HTML document. The only difference between curl and browser is that Browser processes the HTML document and renders it in a user-friendly way.
Every time you visit a website, there is a server responsible for handling your requests. Apache Tomcat, Jetty, Nginx, and Internet Information Services (IIS) are a few well known servers.
Real-world usage of APIs
You are able to access most of the websites on the internet because of APIs. Many websites such as Google, Facebook, Instagram, Twitter, etc provide their own APIs to the public. People can use these APIs to build applications on top of it. Following are few common use cases :-
Google Maps APIs
Companies such as Uber, Grab, and Lyft use Google Maps APIs to show nearest drivers and track our location.
Similarly, food delivery startups such as DoorDash leverage Map’s APIs to find nearby restaurants for us. And track the location of the delivery executive.
Twitter APIs
We can use Twitter APIs to gather the tweet data and perform sentiment analysis. These APIs can be used for predicting the outcomes of elections or stock’s performance.
People often use Twitter APIs to build bots and automate the process of posting tweets.
Stripe APIs
Stripe APIs enable merchants on the internet to accept payments and process payouts.
Cloud APIs
Cloud providers such as Azure, AWS, GCP provide APIs to access client resources on the cloud.
Users can create, update, fetch and delete resources in their cloud environment.
Summary
APIs form the building block of all the websites on the internet. In simple words, API is a contract between the client and the server. It takes in a set of inputs, and produces an output.
The websites on the internet are based on the Client-Server architecture. Clients such as Browsers and Mobile Apps send the request to the server. The server processes the client request and returns a response.
APIs are extensively used over the internet. APIs hide lots of complexity from the end users. They are built using different architectural styles. REST (REpresentational State Transfer) is a popular architectural style and the majority of the websites are built on it. We will explore and dive deep into REST APIs in the next article.
References
If you enjoyed reading the article, consider doing any of these:
❤️ Share it — Share the article with your team or with someone to whom it might be useful!
✉️ Subscribe to the newsletter - To receive more such posts.