Have you ever heard of the word API and wondered what it is exactly? Got a vague idea, but want to know more about what you could do with an API and how you could create one? In this article, I will walk you through how we can build an API with Python.
What is an API?
API stands for Application Programming Interface. Simply put, APIs simply allow applications to communicate with each other. When people talk about APIs, they sometimes generalize to mean a publicly available web API that returns data, likely in JSON or XML format. The API is not the database or even the server, it is the code that governs the access point of the server.
Also, Read – How to use Machine Learning for Startups?
They allow us to seek data from external sources. We can send a request to an API detailing the information we want.
APIs also allow our sites to modify data from other applications. For example, you’ve probably seen Share on Facebook or share on Twitter buttons on various websites. If you click on one of these buttons, the site you are visiting can communicate with your Facebook or Twitter account and modify its data by adding a new status or a tweet.
Creating An API with Python?
The digitally connected world uses automated messages extensively. Invoices, rent, unique passwords, there are unlimited scopes for automated text.
Say you have a mini-store and a new item arrives and you want to notify 50-100 of your customers. How would you do it? Easy, text them, automatically. Now, as an example of creating an API with Python I will set up an SMS client for quick learning, I found this website www.textlocal.in. It provides bulk message options.
Just create an account, they will give you 10 credits. After registration, go to Settings> API> Generate a new API key and save it in a safe place. You can allow specific IP addresses for security, but let’s keep the fields blank for now.
Then the Requests will go to link https://api.textlocal.in/{command}. Now let’s get into the coding part to create an API with Python.
Let’s Code to Build API with Python
We need only one library to build an API with Python. Let’s import it and get started with the task:
import requests
url = ‘https://api.textlocal.in/'
params = {‘username’:’yourusername@mailid.com’,
‘apiKey’:’yourGenerated-APIKey’
}
Code language: JavaScript (javascript)
Writing the necessary functions:
def check_balance(url):
url = url+'balance'
response = requests.get(url,params=params)
return response.json()
def send_sms(url,params):
url=url+'send'
#Phone numbers inside braces {} in commas
numbers={'9911111xxxx'}
message = {'Hi, This is a Sample message'}
params['numbers'] = numbers
params['message'] = message
response = requests.post(url,params=params)
return response.json()
def inbox(url):
url = url+'get_inboxes'
response = requests.get(url,params=params)
return response.json()
Code language: PHP (php)
So the code completed In five minutes you can configure an SMS message for single or bulk messages. Hope you liked this article on building an API with Python. Please feel free to ask your valuable questions in the comments section below.
Also, Read – Send Custom Emails with Python.