✆+91-9916812177 | contact@beingdatum.com

API Programming in Python

RESTful APIs in Python

This article is all about API Programming in Python. Firstly, we should understand, what is an API? The answer is API, acronym for Application Programming Interface. Have you ever thought, how the social media, e-commerce and other big giants trace our data and search and get the best deals for us. How they manage such a large chunk of data. Here we will see how to interact with API’s and parse JSON in different ways with the use of Python, one of the popular programming languages.

So, all this happens just by making an HTTPS request. Just by making an HTTPS requests to the website we can fetch lot of information from it and get the data in most popular format i.e. JSON format. By now, we would have little idea how this integration works. Now, let’s understand what the definition of API is, “An API is a set of programming instructions and standards for accessing web-based software applications.” Hence, it is a standard that facilitates intercommunication between two or more computer programs.

Let’s try to understand with an example, suppose you are at a restaurant and you want to order a cake. You will be provided with a menu where you will have list of options available to order your cake or even you can customize it. Now, you chose your choice of cake and place an order. The waiter will take your order and send it to kitchen, where it is prepared by the Chef. Wow, your cake is ready now and it will be delivered to you. Enjoy your meal. 

Now, understand it in technical terminology, here you are the requestor, who has made an request to order cake, the kitchen is a service or website, waiter is an API, who basically facilitates the communication between two parties. The order you placed for cake is request, and the cake which is delivered to you is response.

Hope you would have understood the different concepts of API. Now, we will see the benefits of using API:

  • Easy to access
  • Easy to get the changing data
  • Retrieve data in different formats
  • Visibility between client and server
  • Independent Platform

There are basically four different types methods used to access any API:

  • GET: Requests data from a server. – Can be id, username, price, quantity, and much more.
  • POST: Think of this as you are adding some information at the server or like creating a new row in database. Hence, this method will send changes from client to server.
  • PUT: It adds to the existing data
  • DELETE: It deletes existing data.

Now, we will begin to use an API.  There are lot of different API’s such as Facebook, Amazon, Zomato, Google Drive and many more. There are different ways to talk to an API either by API Key or OAuth. However, there is also another way to interact with an API with help of a tool known as POSTMAN

These tools will help you to communicate with API’s and will retrieve the data for you in a structured way without writing code. These tools are quite useful as they will help you in API connectivity testing as well as you can understand the structure of data which will help in parsing of the data in your code.

Also, there is one more way to fetch the data by passing the URL in browser along with query parameter which will pull the information for you. For example, if we see the weather API “api.openweathermap.org/data/2.5/weather?q={city name}”

Making an API Requests in Python

To start with, you should have python installed on your system. Once it is installed, you will require a requests module to make an requests to API. It can be installed using below command:

pip install requests

Once installed, you can import it in your code using,

import requests

Now make a request to your API endpoint.

request = requests.get(“http://time.jsontest.com/”)

Now, you can check the status code of this endpoint by:

Status code 200 means Response is OK. Similarly, you will get the response code 404 if endpoint is Not Found.

Now, to print the data in JSON format, we will use:

Here is the complete snippet, attached for you:


Above example is a piece of code to understand how to get the data and make request with help of Python. You can try the same with other API, known as Datamule. Here we have passed query parameters as “mummy” which is a rhyming word. You can use any of your choice and play around.

Output for DataMule API with rhyming word “mummy”:


An API is useful for pulling specific information from another program. If you know how to read the data from an API, it will give you lots of data which can be useful for further analysis. Python has lots of modules which will help you to fetch the data from API and makes your task easier.

Please leave your comments for any kind of queries.

May 12, 2020

6 responses on "API Programming in Python"

  1. Great article Sachin & pleasing to see you have a expanded your early devops experience into this highly interesting area, automation when used appropriately can be a huge assistance in an operational environment.

    Well done.

  2. Nice article, well written and informative.

  3. Nice article and learned something new … Well written and easy to understand…

  4. Keep it up

Leave a Message

Your email address will not be published. Required fields are marked *

© BeingDatum. All rights reserved.