Airthings consumer API with Python
July 2022
Overview
Quick guide on how to authenticate and query the Airthings consumer API to pull data from the air quality monitoring devices registered with the account.
Code
Here’s the code to authenticate and pull sensor data to get going.
Script details
Requires Python 3.6 and above due to use of f-strings.
The requests package is a requirement and should be installed in a virtual environment or container with pip install requests
.
The script calls the authentication server to request a bearer token to authenticate the calls to the API, using the client credentials Oauth2 flow.
You’ll need the API token and client id which can be created: https://dashboard.airthings.com/integrations/api-integration with the scopes ‘'’read:device:current_values’’’ checked.
Before running the script, the secret should be exported to an environment variable. That can be done by:
# Linux and Mac
export secret="secret-key"
# Windows
$Env:secret = 'secret-key'
Update the client_id and device_id variable values - these can be viewed in the Airthings website.
python main.py
That should then pull the latest data from the API for that particular device. Individual values can be accessed via the dictionary returned from the API as demonstrated in the script with the co2 value.