Today in this article, we will learn about the topic called APIs. So let's start by understanding the meaning of API, why APIs matter, what you can do about them, and why we should learn them. Let's take a theoretical look that will help you connect with the concept of API because sometimes I feel API is a bit confusing; people try to understand API in many different ways, but it's really a simple concept. This is very easy to understand and very helpful when it comes to producing a product or solution through your server. So we will first try to understand API theoretically and then try to understand API in a practical way. In this article, we will try to create our own API. The full form of API is Application Programming Interface.
What is API, and Why is it Important?
Nowadays, whatever applications, websites, or any online things we use, it's an API. For any online transactions, we all use PhonePe, Amazon Pay, GooglePay, Paytm, or, nowadays, even WhatsApp. So let's suppose I am using PhonePe App and linking it with my bank account (let's say AXIS Bank) and trying to make a transaction of ₹ 500. Initially, this ₹ 500 was not available inside my PhonePe. What will PhonePe do? PhonePe will interact with my AXIS bank account whenever we try to make a transaction. We can say this PhonePe app will request my AXIS bank to validate a transaction of ₹ 500. If everything is validated, then this bank will allow a transaction of ₹ 500, and only in that situation will PhonePe be able to show a successful transaction; otherwise, it will give some short of error. How is this type of transaction possible? The problem is that PhonePe is being developed in one platform, which may be in react native or any other medium. The AXIS bank is using another software in the mainframe or maybe using some other languages or interfaces. So how is PhonePe able to connect with AXIS bank? The comprehensive technology used to develop PhonePe is different, and the platform used to create AXIS software infrastructure is entirely different. So how these two systems can communicate? So this is where API comes into the picture. API is an interface, or API is a bridge between two heterogeneous or may be a homogeneous application. Heterogeneous means an infrastructure or a platform built by using a different technologies stack or programming languages or maybe an interface. Homogeneous means similar types of technologies. Through an API, We will be able to make a request or able to get a response.
Let's discuss this in depth; in the above example, a validation process happened between PhonePe and AXIS bank. PhonePe will try to validate the ₹ 500 along with the User Id, Phone number, account details, or all the other details through which it will be able to validate that the user exists. So PhonePe is sending this type of data through some medium. On the other hand, what will the bank do after getting all the details? The bank will first validate whether this particular user exists in the database. So for sure, someone has written a function to check for user existence with respect to a user ID or any other arguments. This function will be able to receive, let's say, a user ID or account number and try to search inside the database. The function may return True or False. True if user ID or account number exists, false if it doesn't exist. According to the function, the bank system may return true or false. When the bank system returns true, PhonePe will request that if a user ID or bank account exists, try to make a transaction of ₹ 500. So what the bank system will do? It will try to check whether the balance is present or not. If the balance is available, it will try to authenticate via a PIN. After successful entry, it will try to send a response that the transaction is successful, and it will be credited to others user bank accounts, let's say in HDFC bank. Now it will try to call a function from HDFC where the user ID or account number is validated and deposit that particular amount. Here we can see that there are multiple interactions between different systems. AXIS has its own system, HDFC has its own system, and PhonePe has its own system, and it's not like that PhonePe will ask AXIS that I have written my code in Python, so you are supposed to write your code in Python. AXIS can't ask the same thing to HDFC. It is possible that someone has written a backend code in AXIS bank in Java or Python, HDFC may be in C or C++, or PhonePe may be in Python. So we are trying to interact from one language to another or between the same languages every time. At the end of the day, our primary requirement is to execute or call the functions written in the same or different languages in different interfaces or other systems available during the transaction. So our first priority here is to reach out from PhonePe to AXIS bank, and then only we will be able to execute the particular functions written inside a module or packages or class or just as a function inside AXIS bank. The way in which we are able to reach out from PhonePe to AXIS bank is called as API. It's a bridge between two applications theoretically. API is a standard procedure by which we are trying to create a route from one system to another system, one application to another application, to invoke methods or functions. API is nothing but a set of protocols and language independent. Protocol means URL (IP Addresses), which will help to reach out to the system. In addition to port number, whatever applications are running inside a single machine or cloud platform, it always tries to occupy with a port number. Inside the port number or inside Python, we have multiple functions. So it will reach out to a particular function by using the function name. As a result, these form a route (URL/port_number/func_name) that we always create to reach out from one system to another. Without an API, we will not be able to develop any full-fledged production-grade application, that is the importance of an API.
In every programming language, you will be able to find out a way or a framework to create an API. In Python, when we want to process a file, we use the Pandas library; for numerical applications, the Numpy library is used; for graphs, the matplotlib, seaborn or plotly libraries can be used. Similarly, for creating an API, there are multiple packages available that support the python language. Some are Django, Flask, etc. The core concept of creating an API is the same in all languages like C, C++, Java, or Python, only the syntactical way of creating those APIs is different.
Creating an API using Flask:
I will help you with the basics of creating an API using Flask. First, try to download and Install Pycharm Community Edition or any other IDE before creating an API. I am now going to create an API that will take a path of a CSV file from an HTML page, perform EDA, and show the result on another HTML page. Please follow the below steps when creating an environment variable.
First, create your project folder, and then inside your project folder, create a static folder and templates folder.
Secondly, open a terminal window in your project folder and install the Flask package: pip install Flask.
The static folder will have your CSS and image files, while templates will hold your HTML files. Please note that we always create a directory tree structure to run the API; otherwise, Flask will not work. Flask always looks for the templates folder for views where all HTML files we saved, and analysis.py is the main file in which you will have to run the API. The analysis.py file will contain all the routes linking your HTML and python codes. The directory tree structure will look like this:
Confusing? No worries, go with the flow or clone the repository from GitHub: Click Here or download the CreatingAPI ZIP file from GitHub. Unzip it and try to open it in Pycharm or any other IDE. Then go to File | Settings | Project: CreatingAPI | Python Interpreter | follow the below steps:
Finally, to run the API, run your analysis.py file and click on the link it provides after running. Open the link on the browser, then upload a clean .csv file and click on Analyze! You will get a statistical analysis of the uploaded dataset. You can add more functions and routes to the web pages and some CSS to improve it.
Please write any suggestions or feedback!