What is API .? how do I create my own API? 🤔
Yep, you read it right. You can create your own API !!!!
Why..? I was just preparing for interviews like everyone and I came about the term "API". Although I have worked in API for my assignments or projects I started to wonder why we are using it and how people create an API. That search led me to create an API and publish it for others to use it. Before we get into knowing about how to create an API, we will look into what API is at first. API stands for Application Programming Interface which is nothing but a simple interface between two applications that allows those two to talk with each other.
Let me explain the USE of API of a web application with a real-world example. Let's consider you are going to a restaurant to have food. Now you enter the restaurant sit in a table and look at the menu. After choosing what you want to eat, the food server comes to take your order. After getting the details of your order the server goes to the kitchen staff requesting the same. The kitchen staff prepares the order and gives it through the same food server. Now we connect it with a web application: - Yourself --> User of the web application - Menu --> Front end of the web application - Food Server --> API
- Kitchen Staff --> Backend/Server
When you visit a web application and want to see some information the front end is responsible for showing it to select or enter details while the API fetches those details from the database(backend) and shows it to you through the front end.
I hope the above example gave an overview of an API. Now let us go into how you can create one.
I will shortly explain how you can create your own API, if you have any doubts you can refer to the video which I used for creating the same and I will be uploading the GitHub link of the code. Like all my blogs almost most of the works will be left for you to research and finish because I believe that researching and applying is the best way to learn.
Veryyyyy short explanation: We can create an API by creating a NodeJS project with the help of express, Axios and cheerios ( we use this to get data from the web pages). Yes, that's all you need to create an API. The functionality of the API, for now, is to fetch news related to Covid-19 from the news website and give them to us in JSON format. The program searches the given website for scraping information and using cheerios gets the keyword which we have given (Covid-19 in our case) from the <a> tag of the webpage. We get the URL, Title, and the Source of the news and save it in an array. We send the array back in the res.json() function to the client calling the API. I have hosted the API in RapidAPI from where you can use it in your application. ( Hoping someone will try it 😋 ). Links for everything is below:
API (Hosted in RapidAPI)