· API (Application Programming Interface)
Have you ever wondered how a feature from one application is extracted and integrated in another?
Each time you check the weather, search for a location or get a notification on your phone you’re using an API. Useful and catchy APIs can bring a wow factor to the application.
Application Programming Interface(API) is a medium between two software which allows them to communicate. It fetches for the response from a system and delivers back to the user. APIs make it easier for users to intuitively find resources
Thus API enables us to extract features from other applications and integrate those with our application.
· Internal & External API
In our cloud era, the increase in mobile and the need of massive internal/external adoption of services, REST-based APIs have replaced SOAP Web services. REST APIs are HTTP based, lighter, easier to understand and integrate, and therefore, have become the de facto standard for creating enterprise APIs. Enterprise APIs can be internal APIs i.e. within or across LoB (Line of Business), or external APIs for partners and third-party developers.
An external API is an interface that has been designed to be easily accessible by the wider population of the web and mobile developers. This means an external API may be used both by developers inside the organization that published the API or by any developers outside that organization who wish to register for access to the interface.
· When to Create an API
1. When the users need to access the data
2. When the data changes or is updated frequently.
3. When users need to extract a feature from your application .
We all use twitter bots in various events or gatherings to show the participation and enthusiasm among all the people. This is one of the best application of APIs. Twitter bots are accounts that automatically tweet (or retweet), follow, and send direct messages based on software instructions.
All these bots are powered by the Twitter API. Aside from allowing you to execute simple actions— like Tweeting a certain phrase or following a user — the Twitter API can also tell bots when something specific happens on the platform. For example, you can ask the Twitter API to tell your bot whenever it receives a new follower. Then, you can program your bot to send a message to that follower using the API.
· Create API from scratch using flask
Lets learn to create API from scratch using flask !!!
While working on many projects we come across many softwares which have a feature named “Tip of the day”. This feature really benefits the user to know some useful tips and tricks while working on that software. It makes our life easier!!
In the similar way, if we integrate a feature called “Fact of the day” to any academic platform , don’t you think it would beneficial and exciting for the students to know various interesting facts that are related to the topics they are studying?
Here’s a surprise for you!
We’ll be creating a API from scratch. This API will randomly display some interesting facts related to “Electronics”. You can further integrate this API with your application.
· Software requirements:
Python IDE(integrated development environment )
ALL YOU NEED TO DO IS FOLLOW THESE STEPS !!!
· Step 1: Create a folder- “app”
· Step 2 : Open terminal
a. Create virtual environment — “env”-environment name
b. Activate virtual environment
This is how your terminal will look like.
· Step 3 : Dataset creation :
Create “facts.csv” containing facts in the “app” folder. You can find the dataset here :
It contains 30 electronics facts. https://github.com/darekarankita0/factt/blob/main/facts.csv
· Step 4 : FLASK:
Flask is a web application framework written in Python. Flask is based on the Werkzeug WSGI toolkit and Jinja2 template engine.
Flask code:”app.py” https://github.com/darekarankita0/factt/blob/main/app.py
This API will display random facts related to electronics. Each time the API is called a different fact will be displayed.
Importing the Flask module and creating a Flask web server from the Flask module.
This is the current file. This current file will represent our web application. We are creating an instance of the Flask class and calling it app.
It represents the default page. We can define various routes in our code depending on the features in our application.
When a route is called that function gets activated.
“ main ” :
When we run the python script, it assigns the name “ main ” to the script when executed. If we import another script, the if statement will prevent other scripts from running.
When we run app.py, it will change its name to main and only then will that if statement activate.
This will run the application. Having debug=True allows possible python errors to appear on the web page. This will help us trace the errors.
Why both HTTP methods, methods=[‘GET’, ‘POST’]?
Well, we will eventually use that same route for both GET and POST requests — to serve the facts.html page and handle form submissions, respectively.
· Step 5 : Install modules :
pip install flask gunicorn pandas
· Step 6 : Run flask api on local host:
· Step 7 :
Copy the local host url and paste in Postman to test the API : http://127.0.0.1:5000/
· Step 8 : Create “Procfile” in “app” folder
web: gunicorn app:app
web — web application
gunicorn — Gunicorn is a pure-Python HTTP server for WSGI applications. It allows you to run any Python application concurrently by running multiple
app — The first “app” after gunicorn is the name of our main file.
· Step 9 : Create “requirements.txt” in “app” folder
Run the below command in the terminal to create “ requirements.txt”
pip freeze > requirements.txt
This is how your requirements.txt will look like.
It contains various libraries required to run your application.
· Step 10 : Create “runtime.txt” in “app” folder
You need to check your python version using :
The “runtime.txt” file contains the python version For example : python-3.8.5
Now you must be thinking what is the purpose of creating all these files!!
We’ll be using all these files while deploying our API on heroku platform.
Lets move to the next step.
· DEPLOYING API :
Heroku is a container-based cloud Platform as a Service (PaaS). Developers use Heroku to deploy, manage, and scale modern apps. This platform is elegant, flexible, and easy to use, offering developers the simplest path to getting their apps to market.
· Deploy on heroku
· Step 1 : Create a free account on heroku. You can deploy 5 apps for free! https://id.heroku.com/login
· Step 2 : Choose any one method for deployment.
There are 3 ways to deploy apps on heroku cloud.
A. Heroku Git — using heroku cli
Ø Install heroku cli
Ø Initialize an empty repository, add the files in the repository and commit all the changes.
git commit -m “Initial Commit”
Ø Login to heroku CLI using
heroku login OR heroku login -i
Ø Push code from local to the heroku remote.
git push heroku master
GitHub is a code hosting platform for version control and collaboration. You can store your code in the repository
Ø Select deployment method as “GitHub” on heroku
Ø New-Create new app — App name — Create app
Ø Create a new repository on github and upload the files from the “app” folder to the repository
Ø Search for the repository and enable automatc deployment
Ø Click on Deploy branch
· 3. Container Registry — — using herok cli
Ø Install the Heroku CLI
Ø Download and install the Heroku CLI.
If you haven’t already, log in to your Heroku account and follow the prompts to create a new SSH public key.
Ø Log in to Container Registry
You must have Docker set up locally to continue. You should see output when you run this command.
Ø Now you can sign into Container Registry.
Ø Push your Docker-based app
Ø Build the Dockerfile in the current directory and push the Docker image.
heroku container:push web
Ø Deploy the changes
Ø Release the newly pushed images to deploy your app.
heroku container:release web
NGROK is a cross-platform application that enables developers to expose a local development server to the Internet with minimal effort. The software makes your locally hosted web server appear to be hosted on a subdomain of ngrok.com, meaning that no public IP or domain name on the local machine is needed.
Step 1. Create account on www.ngrok.com
Step 2. Download the NGROK application
Step 3. Integrate the authtoken by command ngrok authtoken (authtoken given on the dasboard of www.ngrok.com)
Step 4. Start a tunnel by using comman ngrok http : http://127.0.0.1:5000/
Here are some references for you !
NGROK : https://ngrok.com/docs
POSTMAN CANARY : https://learning.postman.com/docs/getting-started/introduction/
Further you can integrate this API to your application. Let us integrate the API to the chat bot in the second half!!