SDKs overview

OpenAQ offers programmatic access to its global air quality data through a RESTful API. To streamline interaction with this API, various software development kits (SDKs) and libraries have been developed. These tools encapsulate the complexities of HTTP requests, authentication, and data parsing, allowing developers to focus on integrating air quality information into their applications. The primary focus for official support is on languages commonly used in data science and web development, such as Python, R, and JavaScript, reflecting OpenAQ's utility in data journalism and environmental research.

The OpenAQ API provides access to real-time and historical air quality data, including measurements for pollutants like PM2.5, PM10, ozone, and NO2, from a network of sensors worldwide. SDKs simplify operations such as querying locations, fetching latest measurements, or retrieving historical data series for specific parameters. This enables developers to build dashboards, research tools, and public health applications with reduced development overhead.

While OpenAQ maintains official client libraries for key languages, the open-source nature of the platform also encourages community contributions, leading to a broader ecosystem of tools. These community-driven projects can offer specialized functionalities or support for additional programming environments.

Official SDKs by language

OpenAQ provides officially supported client libraries to facilitate integration with its API. These libraries are designed to be robust and maintained in alignment with API updates, offering a reliable way to access air quality data. The primary languages with official SDKs are Python, R, and JavaScript, chosen due to their prevalence in data analysis, scientific computing, and web development respectively.

Language Package Name Installation Command (Example) Maturity
Python openaq pip install openaq Stable
R ropenaq install.packages("ropenaq") Stable
JavaScript openaq-api npm install openaq-api Stable

These official SDKs abstract the underlying HTTP requests, allowing developers to interact with the API using idiomatic language constructs. For instance, instead of constructing a URL and parsing JSON manually, a developer can call a function like openaq.cities() to retrieve a list of cities with available data. The OpenAQ API documentation provides detailed reference material for each endpoint and parameter.

Installation

Installing OpenAQ SDKs typically involves using the standard package manager for the respective programming language. Each package is distributed through common repositories, ensuring ease of access and dependency management.

Python

The Python SDK for OpenAQ is available on PyPI. To install it, use pip:

pip install openaq

For development or to install specific versions, virtual environments are recommended, as described in the Python Packaging User Guide.

R

The R library for OpenAQ is available on CRAN. To install it within an R environment:

install.packages("ropenaq")

This command downloads and installs the package and its dependencies. Users can then load the library using library(ropenaq).

JavaScript

The JavaScript SDK is available via npm. To install it in a Node.js project:

npm install openaq-api

This command adds the package to your project's node_modules directory and updates your package.json file. For client-side browser usage, bundlers like Webpack or Rollup can be used to include the package.

Quickstart example

The following quickstart examples demonstrate basic usage of the OpenAQ SDKs to retrieve the latest air quality measurements. These snippets illustrate how to initialize the client and make a simple API call.

Python Quickstart

This Python example fetches the latest measurements from the OpenAQ API:

import openaq

# Initialize the OpenAQ API client
api = openaq.OpenAQ()

# Fetch the latest 5 measurements
status, resp = api.measurements(limit=5)

# Print the measurements
if status == 200:
    for measurement in resp['results']:
        print(f"Location: {measurement['location']}, Parameter: {measurement['parameter']}, Value: {measurement['value']} {measurement['unit']}")
else:
    print(f"Error fetching data: {status}")

R Quickstart

This R example retrieves the latest 5 measurements using the ropenaq package:

# Load the ropenaq library
library(ropenaq)

# Fetch the latest 5 measurements
latest_measurements <- aq_measurements(limit = 5)

# Print the measurements
if (length(latest_measurements) > 0) {
  for (i in 1:nrow(latest_measurements)) {
    cat(paste0("Location: ", latest_measurements$location[i], ", Parameter: ", latest_measurements$parameter[i], ", Value: ", latest_measurements$value[i], " ", latest_measurements$unit[i], "\n"))
  }
} else {
  cat("No data found or error fetching data.\n")
}

JavaScript Quickstart (Node.js)

This Node.js example uses the openaq-api package to get the latest 5 measurements:

const OpenAQ = require('openaq-api');

// Initialize the OpenAQ API client
const api = new OpenAQ();

// Fetch the latest 5 measurements
api.measurements({ limit: 5 })
  .then(response => {
    if (response.status === 200) {
      response.data.results.forEach(measurement => {
        console.log(`Location: ${measurement.location}, Parameter: ${measurement.parameter}, Value: ${measurement.value} ${measurement.unit}`);
      });
    } else {
      console.error(`Error fetching data: ${response.status}`);
    }
  })
  .catch(error => {
    console.error('An error occurred:', error);
  });

These examples provide a starting point for integrating OpenAQ data. Developers can expand upon these by adding parameters for filtering by city, country, or specific pollutants, as detailed in the OpenAQ API reference.

Community libraries

Beyond the officially supported SDKs, the OpenAQ community has developed additional libraries and tools that extend functionality or offer support for other programming languages and platforms. These community contributions often arise from specific project needs or academic initiatives, reflecting the diverse applications of air quality data in fields like environmental monitoring and public health.

While OpenAQ does not officially endorse or maintain these community-driven projects, they can be valuable resources for developers working in specific environments or requiring specialized features not covered by official SDKs. Users should review the documentation and support status of any community library before integrating it into production systems.

Examples of potential community contributions might include:

  • Libraries for languages such as Java, Go, or Ruby.
  • Data visualization tools or Jupyter Notebook examples for specific analytical tasks.
  • Connectors for data warehousing solutions or IoT platforms.

Developers interested in contributing to the OpenAQ ecosystem or finding community-maintained resources are encouraged to explore the OpenAQ GitHub repositories and community forums, which often highlight such projects. Information about contributing to open-source software is available from organizations like the Open Source Initiative.