Exploring Air Quality Data with OpenAQ API

If you are looking for air quality data from around the world, OpenAQ API provides access to this data with a simple HTTP request. OpenAQ is a community-led open data platform that aims to harmonize disparate air quality data sources and provide access to well-documented, high-quality data.

Getting Started with OpenAQ API

To begin exploring OpenAQ API, you will need to sign up to get your API Key. This API Key is necessary to authenticate your requests.

Once you have obtained your API Key, you can start sending HTTP requests to the OpenAQ API endpoints. The basic structure of an API request is as follows:

https://api.openaq.org/v1/<endpoint>?<parameters>

In the above URL, <endpoint> refers to the API endpoint you would like to access, and <parameters> refer to any parameters you would like to pass to the API endpoint.

Example API Requests in JavaScript

Here are some example API requests in JavaScript:

Get Cities

The following example sends an HTTP GET request to the cities endpoint to get a list of cities with available air quality data:

const endpointURL = 'https://api.openaq.org/v1/cities';
const requestURL = endpointURL + '?limit=10';

fetch(requestURL)
  .then(response => response.json())
  .then(data => {
    console.log(data.results);
  })
  .catch(error => {
    console.warn(error);
  });

In the code above, we are using the fetch function to send an HTTP GET request to the cities endpoint with a limit of 10 results. Once we receive a response, we parse the response data as JSON and log it to the console.

Get Latest Measurements

The following example sends an HTTP GET request to the latest endpoint to get the latest air quality measurements for a specific location:

const endpointURL = 'https://api.openaq.org/v1/latest';
const location = 'Los Angeles';

const requestURL = endpointURL + `?location=${location}`;

fetch(requestURL)
  .then(response => response.json())
  .then(data => {
    console.log(data.results[0]);
  })
  .catch(error => {
    console.warn(error);
  });

In the code above, we are using the fetch function to send an HTTP GET request to the latest endpoint with a query parameter for a specific location. Once we receive a response, we parse the response data as JSON and log the first result to the console.

Conclusion

In this blog post, we have explored OpenAQ API, a community-led open data platform that provides access to air quality data from around the world. We have also demonstrated some example API requests in JavaScript. Now it's time for you to start exploring the OpenAQ API and unleash the power of air quality data!

Related APIs