Skip to content

Client

The OpenAQ SDK provides the OpenAQ class to call OpenAQ API endpoints.

To use the SDK, pass a valid OpenAQ API key directly during client initialization:

from openaq import OpenAQ
client = OpenAQ(api_key="replace-with-a-valid-openaq-api-key")

You can also set the OPENAQ_API_KEY environment variable. This configures the key without defining it during instantiation:

main.py
from openaq import OpenAQ
client = OpenAQ()
Terminal window
OPENAQ_API_KEY=replace-with-a-valid-openaq-api-key python main.py

Without a key, the client raises an ApiKeyMissingError exception. An invalid key raises a NotAuthorizedError (HTTP 401) exception.

The OpenAQ class provides a synchronous interface to access the API.

from openaq import OpenAQ

The client uses HTTP connection pooling to efficiently reuse connections. Always close the connection after finishing requests to release the pool.

from openaq import OpenAQ
client = OpenAQ()
# Call resources with the open connection pool
...
client.close()

The OpenAQ client can also be used as a context manager. Utilizing a with block ensures the client closes automatically upon exit, eliminating the need to manually call the close() method.

with OpenAQ(api_key="replace-with-a-valid-openaq-api-key") as client:
...