Rahul Sinha
Rahul Sinha Blog

Rahul Sinha Blog

How to Use COVID-19 API in Python | Use API in Python

How to Use COVID-19 API in Python | Use API in Python

Subscribe to my newsletter and never miss my upcoming articles

Listen to this article

image.png

In this blog, we gonna know how to use COVID-19 API in Python. We will fetch data using the API. API (Application Programming Interface) which I will be using is only for India. If you want to use another API to show the stats for your country just changed the API and do a small modification in the code.

Lets’s Start

  • First, install the requests module using pip for windows. If you are not a windows user google it once how to install requests. Open cmd or Powershell.
pip install requests

Basically, the requests module allows us to send HTTP requests using Python.

  • After installing requests, let's see which API we going to use to fetch the data. We are using Postman COVID19-India API. In this, you are using statewise stats API.
https://api.covid19india.org/data.json

image.png

Copy the link and open in new tab. Here go down and find statewise. It is a JSON format. We will use states' names to get the data like Active, Confirmed, and Deaths.

  • Now open your favorite text editor. I am using IDLE.

Let's start writing the code!!

import requests

we have to import the requests module.

state = 'Punjab'

write your state name and store it in a variable name state.

api_address = "https://api.covid19india.org/data.json"

Now paste the API address and store it in a variable name api_address. Be sure that you have pasted the API address correctly.

json_data = requests.get(api_address).json()

In this line, we are using get() method to send specified GET requests to URL which is store in api_address. Then we are using json() module to convert that JSON(JavaScript Object Notation) format to a Python object or format. Then storing the data in json_data.

state_indexing = range(0,40)

We are using a range function so that we can read or I would say to match all states which we writing in state variable to get the exact data. I used from 0 to 40 as there are both Indian states and Union territory.

In JSON, there are objects which have keys/values pairs like in Python we have a dictionary that uses keys/values pairs. It starts from 0 to n.

for n in state_indexing:
    if state == json_data['statewise'][n]['state']:
        Confirmed_Case = json_data['statewise'][n]['confirmed']
    n+= 1

We are using a For Loop. In which n is used for indexing the states in statewise object. Then we are matching that if the given state is in the given json_data by indexing.

image.png

You can see in this image that the object statewise having a list and in that list, we have keys/values pairs. So n represents the whole keys/values pairs in that {}.

if state == json_data['statewise'][n]['state']:
        Confirmed_Case = json_data['statewise'][n]['confirmed']
    n+= 1

If state matches then store the confirmed in Confirmed_Cases variable. Then I used n+=1, we can also write it like n = n + 1. If the state not matches it goes on finding the state.

print(f"Active Case in {state} is {Confirmed_Case}")

At last, I printed the value. I used string formatting to print the value. It will print like → “ Active Case in Punjab is 2000 ”

You can match the whole code here!

import requests
state = 'Jharkhand'.lower()
api_address = "https://api.covid19india.org/data.json"
json_data = requests.get(api_address).json()
state_indexing = range(0,30)
for n in state_indexing:
    if state == json_data['statewise'][n]['state'].lower():
        Confirmed_Case = json_data['statewise'][n]['confirmed']
    n+= 1
print(f"Active Case in {state} is {Confirmed_Case}")

I hope you like it. Please share it with your friends to try this code.

 
Share this