datagovindia is a wrapper for around >130,000 APIS of the Government of India’s open data platform data.gov.in. Here is a small guide to take you through the package. Primarily,the functionality is centered around three aspects :

  • API discovery - Finding the right API from all the available APIs
  • API information - Getting information about a particular API
  • Querying the API - Getting a tidy data set from the chosen API

API Discovery

The APIs from the portal are scraped every week to update a list of all APIs and the information attached to them like sector, source, field names etc. The website data.gov.in provides a search functionality through string searches and drop down menus but these are very limited. The functions in this package allows one to have more robust string based searches.
A user can search by API title, description, organization type, organization (ministry), sector and sources. Briefly there are two types of functions here, the first lets the user get a list of all available and unique organization type, organization (ministry), sector and sources and the other lets one “search” by these criteria and more.

Here is a demonstration of the former (getting only the first few values)

###List of organizations (or ministries)
get_list_of_organizations() %>% 
  head
#> [1] "Ministry of Environment, Forest and Climate Change"
#> [2] "Central Pollution Control Board"                   
#> [3] "Ministry of Power"                                 
#> [4] "Ministry of Petroleum and Natural Gas"             
#> [5] "Ministry of Health and Family Welfare"             
#> [6] "Department of Health and Family Welfare"
###List of sectors 
get_list_of_sectors() %>% 
  head
#> [1] "Environment and Forest"    "Vehicular Air Pollution"  
#> [3] "Residential Air Pollution" "Industrial Air Pollution" 
#> [5] "Power and Energy"          "Petroleum and Natural Gas"

Searching for the right API

Once you have an idea about what you want to look for in the API, search queries can be constructed using titles, descriptions as well as the categories explored earlier. A data.frame with information of APIs matching the search keywords is returned. Multiple search functions can be applied over each other utilizing the data.frame structure of the result.

##Single Criteria
search_api_by_title(title_contains = "pollution") %>% head(2)
#> Warning in as.POSIXlt.POSIXct(x, tz): unknown timezone 'Asia/Caclutta'
#> Warning in as.POSIXlt.POSIXct(x, tz): unknown timezone 'Asia/Caclutta'
index_name title description org_type org sector source created_date updated_date
583f10fa-a19e-4a08-85f1-69dcf64438f4 Details of Number of industries inspected and Directions issued under Section 5 of Environment (Protection) Act, 1986 by Central Pollution Control Board (CPCB) since 2016-17 till 14.06.2019 (From: Ministry of Environment, Forest and Climate Change) Details of Number of industries inspected and Directions issued under Section 5 of Environment (Protection) Act, 1986 by Central Pollution Control Board (CPCB) since 2016-17 till 14.06.2019 (From: Ministry of Environment, Forest and Climate Change) Central Rajya Sabha All data.gov.in 2021-03-04 12:22:31 2021-03-23 09:24:05
b8e4ff80-ec3c-439c-aebb-f27eabe410b3 State/UT-wise Number of Complying and Non-Complying Locations w.r.t. Heavy Metals According Central Pollution Control Board (CPCB) during 2017 (From : Ministry of Environment, Forest and Climate Change) State/UT-wise Number of Complying and Non-Complying Locations w.r.t. Heavy Metals According Central Pollution Control Board (CPCB) during 2017 (From : Ministry of Environment, Forest and Climate Change) Central Rajya Sabha All data.gov.in 2021-03-04 12:07:26 2021-03-23 09:11:45
##Multiple Criteria
dplyr::intersect(search_api_by_title(title_contains = "pollution"),
                 search_api_by_organization(organization_name_contains = "pollution"))
#> Warning in as.POSIXlt.POSIXct(x, tz): unknown timezone 'Asia/Caclutta'
#> Warning in as.POSIXlt.POSIXct(x, tz): unknown timezone 'Asia/Caclutta'
index_name title description org_type org sector source created_date updated_date
0579cf1f-7e3b-4b15-b29a-87cf7b7c7a08 Details of Comprehensive Environmental Pollution Index (CEPI) Scores and Status of Moratorium in Critically Polluted Areas (CPAs) in India NA Central Ministry of Environment, Forest and Climate Change|Central Pollution Control Board Industrial Air Pollution|Water Quality|Natural Resources|Environment and Forest data.gov.in 2017-06-08 16:36:24 2018-11-29 21:05:16

Once you have found the right API for your use, take a note of the “index_name” of that API, for example, “0579cf1f-7e3b-4b15-b29a-87cf7b7c7a08” corresponds to the API for “Details of Comprehensive Environmental Pollution Index (CEPI) Scores and Status of Moratorium in Critically Polluted Areas (CPAs) in India”. index_name will be essential for both getting to know more about the API or to even get data from it.

Getting more information about a chosen API

There are two functions in this section, one to get API information, the other to get a available “field” names and types of the chosen API (using it’s index_name obtained above).

API information

get_api_info(api_index = "0579cf1f-7e3b-4b15-b29a-87cf7b7c7a08")
#> Warning in as.POSIXlt.POSIXct(x, tz): unknown timezone 'Asia/Caclutta'
#> Warning in as.POSIXlt.POSIXct(x, tz): unknown timezone 'Asia/Caclutta'
index_name title description org_type org sector source created_date updated_date
0579cf1f-7e3b-4b15-b29a-87cf7b7c7a08 Details of Comprehensive Environmental Pollution Index (CEPI) Scores and Status of Moratorium in Critically Polluted Areas (CPAs) in India NA Central Ministry of Environment, Forest and Climate Change|Central Pollution Control Board Industrial Air Pollution|Water Quality|Natural Resources|Environment and Forest data.gov.in 2017-06-08 16:36:24 2018-11-29 21:05:16

API Fields

Fields are essentially the variables in the dataset obtained from the API. Knowing the fields before querying for the data will be essential to preform tasks such as filtering, sorting and subsetting the data obtained from the API’s server.

get_api_fields(api_index = "0579cf1f-7e3b-4b15-b29a-87cf7b7c7a08")
id name type
document_id document_id double
status_of_moratorium Status of Moratorium keyword
industrial_cluster_area Industrial Cluster / Area keyword
state State keyword
cepi_score_2009 CEPI SCORE-2009 double
cepi_score_2011 CEPI SCORE-2011 double
cepi_score_2013 CEPI SCORE-2013 double
resource_uuid resource_uuid keyword

The id of these fields is going to be useful while querying the data.

Querying the chosen API

The function get_api_data is really the powerhouse in this package which allows one to do things over and above a manually constructed API query can do by utilizing the data.frame structure of the underlying data. It allows the user to filter, sort, select variables and to decide how much of the data to extract. The website can itself filter on only one field with one value at a time but one command through the wrapper can make multiple requests and append the results from these requests at the same time.

But before we dive into data extraction, we first need to validate our API key relieved from data.gov.in. To get the key, you need to register first register and then get the key from your “My Account” page after logging in. More instruction can be found on this official guide. Once you get your API key, you can validate it as follows (only need to do this once per session) :

##Using a sample key
register_api_key("579b464db66ec23bdd000001cdd3946e44ce4aad7209ff7b23ac571b")
#> Connected to the internet
#> The server is online
#> The API key is valid and you won't have to set it again

Once you have your key registered, you are ready to extract data from a chosen API. Here is what each argument means :

  • api_index : index_name of the chosen API (found by using search functions)
  • results_per_req : Results per request sent to the server ; can take integer values or the string “all” to get all of the available data
  • filter_by : A named character vector of field id (not the name) - value(s) pairs ; can take multiple fields as well as multiple comma separated values
  • field_select : A character vector of fields to select only a subset of variables in the final data.frame
  • sort_by : Sort by one or multiple fields

To recap, first find the API you want using the search functions, get the index_name of the API from the results, optionally take a look at the fields present in the data of the API and then use the get_api_data function to extract the data. Suppose we choose the API “Real time Air Quality Index from various location” with index_ name 3b01bcb8-0b14-4abf-b6f2-c1bfd384ba69. First we will look at which fields are available to construct the right query.
Suppose We want to get the data from only 2 cities Chandigarh and Gurugram and pollutants PM10 and NO2. We will let all fields to be returned (dataset columns).

We will use a sample key from the website for this demonstration.

register_api_key("579b464db66ec23bdd0000019fc84f43ca52437351b43702f5998234")
#> Connected to the internet
#> The server is online
#> The API key is valid and you won't have to set it again

We now look at the fields available to play with.

get_api_fields("3b01bcb8-0b14-4abf-b6f2-c1bfd384ba69")
id name type
document_id document_id double
id id double
country country keyword
state state keyword
city city keyword
station station keyword
last_update last_update date
pollutant_id pollutant_id keyword
pollutant_min pollutant_min double
pollutant_max pollutant_max double
pollutant_avg pollutant_avg double
pollutant_unit pollutant_unit keyword
resource_uuid resource_uuid keyword

We accordingly select the city and pollution_id fields for constructing our query. Note that we use only field id to finally query the data.


get_api_data(api_index="3b01bcb8-0b14-4abf-b6f2-c1bfd384ba69",
             results_per_req=10,filter_by=c(city="Gurugram,Chandigarh",
                                            polutant_id="PM10,NO2"),
             field_select=c(),
             sort_by=c('state','city'))
#> Connected to the internet
#> The server is online
#> url-https://api.data.gov.in/resource/3b01bcb8-0b14-4abf-b6f2-c1bfd384ba69?api-key=579b464db66ec23bdd0000019fc84f43ca52437351b43702f5998234&format=json&offset=0&limit=10&filters[city]=Gurugram&filters[polutant_id]=PM10
#> gave the API a rest
#> url-https://api.data.gov.in/resource/3b01bcb8-0b14-4abf-b6f2-c1bfd384ba69?api-key=579b464db66ec23bdd0000019fc84f43ca52437351b43702f5998234&format=json&offset=0&limit=10&filters[city]=Chandigarh&filters[polutant_id]=PM10
#> gave the API a rest
#> url-https://api.data.gov.in/resource/3b01bcb8-0b14-4abf-b6f2-c1bfd384ba69?api-key=579b464db66ec23bdd0000019fc84f43ca52437351b43702f5998234&format=json&offset=0&limit=10&filters[city]=Gurugram&filters[polutant_id]=NO2
#> gave the API a rest
#> url-https://api.data.gov.in/resource/3b01bcb8-0b14-4abf-b6f2-c1bfd384ba69?api-key=579b464db66ec23bdd0000019fc84f43ca52437351b43702f5998234&format=json&offset=0&limit=10&filters[city]=Chandigarh&filters[polutant_id]=NO2
#> gave the API a rest
#> No results returned - check your api_index
id country state city station last_update pollutant_id pollutant_min pollutant_max pollutant_avg pollutant_unit
649 India Haryana Gurugram NISE Gwal Pahari, Gurugram - IMD 08-05-2022 01:00:00 PM10 NA NA NA NA
654 India Haryana Gurugram Sector-51, Gurugram - HSPCB 08-05-2022 01:00:00 PM10 130 372 198 NA
661 India Haryana Gurugram Teri Gram, Gurugram - HSPCB 08-05-2022 01:00:00 PM10 140 255 186 NA
236 India Chandigarh Chandigarh Sector 22, Chandigarh - CPCC 08-05-2022 01:00:00 PM10 69 166 116 NA
243 India Chandigarh Chandigarh Sector-25, Chandigarh - CPCC 08-05-2022 01:00:00 PM10 81 150 114 NA
250 India Chandigarh Chandigarh Sector-53, Chandigarh - CPCC 08-05-2022 01:00:00 PM10 88 245 148 NA
650 India Haryana Gurugram NISE Gwal Pahari, Gurugram - IMD 08-05-2022 01:00:00 NO2 NA NA NA NA
655 India Haryana Gurugram Sector-51, Gurugram - HSPCB 08-05-2022 01:00:00 NO2 17 148 48 NA
662 India Haryana Gurugram Teri Gram, Gurugram - HSPCB 08-05-2022 01:00:00 NO2 23 23 23 NA
668 India Haryana Gurugram Vikas Sadan, Gurugram - HSPCB 08-05-2022 01:00:00 NO2 7 15 11 NA
237 India Chandigarh Chandigarh Sector 22, Chandigarh - CPCC 08-05-2022 01:00:00 NO2 13 190 57 NA
244 India Chandigarh Chandigarh Sector-25, Chandigarh - CPCC 08-05-2022 01:00:00 NO2 5 155 49 NA
251 India Chandigarh Chandigarh Sector-53, Chandigarh - CPCC 08-05-2022 01:00:00 NO2 21 230 78 NA