Client
The OpenAQ SDK provides the OpenAQ class to call OpenAQ API endpoints.
API key
Section titled “API key”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:
from openaq import OpenAQ
client = OpenAQ()OPENAQ_API_KEY=replace-with-a-valid-openaq-api-key python main.pyWithout a key, the client raises an ApiKeyMissingError exception. An invalid
key raises a NotAuthorizedError (HTTP 401) exception.
OpenAQ Client
Section titled “OpenAQ Client”The OpenAQ class provides a synchronous interface to access the API.
from openaq import OpenAQThe 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()Context manager
Section titled “Context manager”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: ...