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

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.

Smoke from west? No, dust from east

Did the huge forest fires on the US West Coast in September impact air quality in Europe? Not really, the measurements show. What did have an impact, was dust transported by winds from south east.

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.

Air pollution shortens more lives than smoking, drugs and malaria

Although we are in the midst of a global pandemic, air pollution remains the single major contributor to loss in life expectancy across the globe.

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.

10 days hazel pollen forecast for Europe

Hazel pollen will probably increase and the front will move north-east during the next ten days