Skip to content

Quick start

This tutorial provides a tutorial for making your first OpenAQ API query using the OpenAQ Python SDK. As a simple example, we will query a single air quality monitoring location and get the location’s name from the response.

  1. Install the openaq package from PyPI:

    Terminal window
    pip install openaq
  2. Get your OpenAQ API key

    If you do not have an account with us, visit explore.openaq.org/register to register for one. Once you have verified your email and logged in to your account, you can find your OpenAQ API key at the bottom of the settings page.

  3. Replace the replace-me-with-a-valid-key value in the code below with your OpenAQ API key then run the code block in a Python REPL or from a .py file:

    from openaq import OpenAQ
    client = OpenAQ(api_key="replace-with-valid-openaq-api-key")
    location = client.locations.get(2178)
    client.close()
    print(location.results[0].name)

    If you follow the steps correctly, you will see Del Norte printed out in your console. This is the name of the monitoring location with ID 2178.

Now let’s walk through the code step by step:

  1. Import the client class from the openaq package. The OpenAQ client provides all the options for querying resources.

    from openaq import OpenAQ
    client = OpenAQ(api_key="replace-with-valid-openaq-api-key")
    location = client.locations.get(2178)
    client.close()
    print(location.results[0].name)
  2. Set up the client by providing an OpenAQ API key to the client for authentication. Remember to replace the value replace-me-with-a-valid-key with your API key, this placeholder value will not work.

    from openaq import OpenAQ
    client = OpenAQ(api_key="replace-with-valid-openaq-api-key")
    location = client.locations.get(2178)
    client.close()
    print(location.results[0].name)
  3. Access the locations resource to get a location’s information by its ID. We are choosing the location ID 2178 here, you can replace this with another other OpenAQ locations ID.

    from openaq import OpenAQ
    client = OpenAQ(api_key="replace-with-valid-openaq-api-key")
    location = client.locations.get(2178)
    client.close()
    print(location.results[0].name)
  4. Close the connection. The OpenAQ Python SDK utilizes HTTP connection pooling to efficiently reuse connections, explicitly closing the connection ensures that all network resources are properly released.

    from openaq import OpenAQ
    client = OpenAQ(api_key="replace-with-valid-openaq-api-key")
    location = client.locations.get(2178)
    client.close()
    print(location.results[0].name)
  5. Print the results.

    from openaq import OpenAQ
    client = OpenAQ(api_key="replace-with-valid-openaq-api-key")
    location = client.locations.get(2178)
    client.close()
    print(location.results[0].name)
  • Read the other getting started articles to familiarize yourself with the basics of working with the SDK.
  • Check out the common workflows for more real-world practices.
  • Individual resources pages provide guides for terms, access methods, and examples to interact with each resource.