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


WHO tightens air quality guidelines

WHO releases stricter air quality guidelines to protect human health. The new standard for fine particles (PM2.5) is halved, stressing that exposure to PM2.5 is a major health risk.

The accidental badger detector

PM2.5 levels peaked late at night and early mornings at our sensor test site and we struggled to find out why. Turns out the answer had fur and four legs.

We’re measuring pollen!

We are running our airminer 2.0 sensors to calculate local pollen levels. As expected, pollen levels vary with temperature and precipitation, but we also see significant variations through the day.

When to expect birch pollen in Norway?

Hazel and alder is flowering in Southern Norway, but the key allergen to many of us is birch. When can we expect the birch pollen season to start?

Spring in Europe – alder, hazel and birch pollen

We are heading into warmer and lighter days in Europe, lockdowns or not. And no surprise, the pollen is faithfully here.

PM2.5 levels drop in Wuhan, China

PM2.5 levels drop by almost 40% after the quarantine in Wuhan, China. We expect reductions in air pollution for the rest of the world as the pandemic develops.

Using satellites to predict the start of the pollen season

Is it possible to use satellites to predict and monitor the development of pollen levels in the atmosphere? We use the vegetation index in our forecasting - using satellite data to measure chlorophyll levels in leaves.