fbpx

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.

Prerequisites

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)

Conclusion

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

Latest

Has air quality improved during lockdowns?

Delhi has had much cleaner air during the Covid-19 lockdowns, whereas we do not find the same improvement in PM2.5 levels in Oslo, Norway.

Birch pollen – how did it develop?

The birch season is over for this year - we have dived into our data and created a visualisation of how it developed in Europe.

Grass pollen levels still high in Central Europe

High levels in Denmark, Germany and Poland, France may have seen the peak. Grass pollen in UK and Scandinavia is on the rise as northern Europe really warms up.

Monitoring Air Pollution With Airmine’s Air Quality Index API

Learn how to use our Air Quality API in this short tutorial. The API is designed for web, IoT, and mobile app developers. It is a free API that gives you access to the PM2.5 concentration at any given location, both current concentration and a 8-days forecast.

Grass blooming in Central Europe

The grass season is long - and Central Europe is in the midst of the blooming. Northern Europe is starting to feel the season.

Birch pollen – the north gets the rest

The birch pollen season is almost finished in most of Central and South Europe. The inhabitants of the northern regions can still expect moderate to high levels, as the weather (finally) warms up.

A first look at the grass pollen season

Grass pollen is the most common pollen allergy in Europe and the season is well under way. Central Europe can expect high levels, while the cold weather further north let us wait for the spread.

The birch pollen front continues north – temporary slowdown due to colder weather

Central Europe past the peak, Nordic countries still on the rise The...

Birch pollen season in full swing in Europe

The birch pollen season is at its height in Central Europe, with high levels recorded across the continent. Southern Scandinavia is starting to feel the birch as well, and will see increasing pollen levels the next ten days.

High birch pollen levels expected across Central Europe

Temperatures are rising in Europe and birch pollen levels will be high across Central Europe and southern UK. The front is moving northwards, and Southern Scandinavia and Finland will also see birch pollen levels on the rise.

Cycling in cities: Darth Vader on two wheels?

Sell your bike and buy a bus card? We say don't.

Birch pollen season in full swing in Europe

The birch pollen season is at its height in Central Europe, with high levels recorded across the continent. Southern Scandinavia is starting to feel the birch as well, and will see increasing pollen levels the next ten days.