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.
Tutorial
Section titled “Tutorial”-
Install the openaq package from PyPI:
Terminal window pip install openaq -
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.
-
Replace the
replace-me-with-a-valid-keyvalue 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 OpenAQclient = 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 Norteprinted out in your console. This is the name of the monitoring location with ID 2178.
Walkthrough
Section titled “Walkthrough”Now let’s walk through the code step by step:
-
Import the client class from the
openaqpackage. TheOpenAQclient provides all the options for querying resources.from openaq import OpenAQclient = OpenAQ(api_key="replace-with-valid-openaq-api-key")location = client.locations.get(2178)client.close()print(location.results[0].name) -
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-keywith your API key, this placeholder value will not work.from openaq import OpenAQclient = OpenAQ(api_key="replace-with-valid-openaq-api-key")location = client.locations.get(2178)client.close()print(location.results[0].name) -
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 OpenAQclient = OpenAQ(api_key="replace-with-valid-openaq-api-key")location = client.locations.get(2178)client.close()print(location.results[0].name) -
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 OpenAQclient = OpenAQ(api_key="replace-with-valid-openaq-api-key")location = client.locations.get(2178)client.close()print(location.results[0].name) -
Print the results.
from openaq import OpenAQclient = OpenAQ(api_key="replace-with-valid-openaq-api-key")location = client.locations.get(2178)client.close()print(location.results[0].name)
Next Steps
Section titled “Next Steps”- 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.