How to Use COVID-19 API in Python

Rahul Sinha
Python Pandemonium
Published in
3 min readJun 7, 2020
Coronavirus API

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 other API to show the stats for your country just changed the API and do the small modification in the code.

Lets’s Start

  1. 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 requests module allows us to send HTTP requests using Python.

2. 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
statewise API

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

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

Let's start writing the code!!

import requests

we have to import 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 a 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 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 which uses keys/values pairs. It start 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.

Indexing the state

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 match the whole code here!

import requestsstate = '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. Clap it and share it with your friends to try this code.

--

--

Rahul Sinha
Python Pandemonium

Technology enthusiast. Keen interest is in Python, Voice technology, Azure, and IoT. Keen to learn new things.