Monitoring Air Pollution With Airmine’s Air Quality Index API


Learn how in this tutorial

Air pollution is a health risk one can no longer ignore. It is one of the major factors responsible for lowering the quality of life in both developed and developing countries. Consequently, a lot of people today are looking for tools that can help them learn more about the contents of the air they are exposed to.

Airmine’s Air Quality Index API is designed for web, IoT, and mobile app developers who are interested in creating such tools. It is a free API that gives you access to the atmospheric fine particulate matter concentration, commonly known as PM2.5, at any given location. In addition to the current concentration, it offers forecasts up to 8 days in the future.

In this short tutorial, we help you get started with the Airmine AQI API.


To follow this tutorial, you’ll need:

  • a Rapid API account
  • a basic understanding of JSON and Python

1. Subscribe to the API

To be able to use the Airmine AQI API, you must subscribe to it. So log into your RapidAPI account and search for Airmine AQI.

Once you find it, click on the Subscribe to Test button. In the next screen, choose the Basic plan, which allows you to make 100 free requests per day.

At this point, you’ll be able to make requests to the following REST endpoint: https://pm251.p.rapidapi.com/aqi

2. Fetch Data From Airmine AQI API

You must make HTTP GET requests to fetch data from the Airmine AQI API. It expects two parameters in the query string: lat and lon. As you may have guessed, lat is the latitude of a location, and lon its longitude.

Additionally, you must include two custom HTTP headers, named x-rapidapi-key and x-rapidapi-host while making the request. The value of the x-rapidapi-key header should be equal to your secret RapidAPI key. And the value of the x-rapidapi-host header should be “pm251.p.rapidapi.com/aqi”. If you’ve used other APIs available on RapidAPI, you may be familiar with these headers already.

The following sample code shows you how to create and execute a valid request in Python3:

import requests

# Airmine AQI endpoint
endpoint = "https://pm251.p.rapidapi.com/aqi"

# Coordinates of Berlin
coordinates = {
    "lat": "52.520008",
    "lon": "13.404954"

# Custom headers
headers = {
    'x-rapidapi-host': "pm251.p.rapidapi.com/aqi",
    'x-rapidapi-key': "YOUR-KEY-HERE"

# Execute a GET request
response = requests.request(
      "GET", endpoint,
      headers = headers, params = coordinates

3. Parse the API’s Response

The response of the API is a JSON document that’s very easy to parse. It has just three outer keys: location, meta, and forecasts. Usually, you’d only be working with the forecasts key, whose value is an array containing hourly PM2.5 forecasts. Here’s what the array looks like:

    "forecasts": [
            "date": "2020-06-13T20:00:00",
            "values": [
                    "parameter": "pm25",
                    "value": 3
            "date": "2020-06-13T21:00:00",
            "values": [
                    "parameter": "pm25",
                    "value": 4

For most locations, the array will have 87 items. The first 60 items give you hourly values. The rest give you six-hourly values.

The following code shows you how to parse the response as a JSON document and determine the number of items in the forecasts array:

data = response.json()
n = len(data['forecasts'])

Each item of the array contains a timestamp and a PM2.5 value. What you actually do with them, of course, depends on the application you’re building. For starters, you could try simply printing them.

for item in data['forecasts']:
    date = item['date']
    pm25 = item['values'][0]['value']
    print("%s | %d" % (date, pm25))

4. Checking Limits

Airmine AQI API’s basic plan gives you 100 free requests per day. Your application will fail if you exceed this limit. Here’s how you can determine the number of requests left using the response’s x-ratelimit-requests-remaining header:

remaining = int(response.headers['x-ratelimit-requests-remaining'])
print("You have %d requests left" % remaining)


You now know how to use the Airmine AQI API. Although we used Python in this tutorial, using the API with any other programming language is just as easy. Whether you’re a professional app developer, a home automation enthusiast, or a hobby programmer, if you’re interested in monitoring air quality, you should consider giving this API a try.

Stay updated!

We keep your data private.

Airmine app

Track your symptoms to air pollution and pollen & get local air quality forecasts

Get it on Google Play


Green in India means red in Norway

We've attempted to compare the different PM2.5 breakpoints in selected air quality indices. It turns out a green level does not mean the same around the world.

EU’s clean air outlook – saving lives and reducing costs

EU has launched its Second Clean Air Outlook report. There are both premature deaths and money to be saved by improving the air quality.

Pilot testing our air quality monitor – where is the pollution coming from?

We are pilot testing our air quality monitors and have started to dig into the first results. We had expected local variations in PM-levels, but it turns out the source is probably found far away from the neighbour's wood burning stove.

Air quality in Europe – it’s going the right way

EEA's recent report gives hope for the air quality in Europe. Most pollutants have been reduced the last decade, but Eastern Europe is still heavily affected.

Winter is coming – with polluted air

As we enter the colder months on the northern hemisphere, air pollution levels rise. Much of Europe is affected, but can hardly match the soaring levels in Northern India.

Better air in Beijing than San Francisco

Last week, you'd rather breathe Delhi air than take a stroll in San Francisco. Smoke and ashes from the huge forest make air quality on the US West Coast the worst in the world.

Green in India means red in Norway

We've attempted to compare the different PM2.5 breakpoints in selected air quality indices. It turns out a green level does not mean the same around the world.