Skip to content

Instantly share code, notes, and snippets.

@MatthewJDavis
Last active August 22, 2022 20:40
Show Gist options
  • Save MatthewJDavis/e26f388b68d27dfede3ba8bbb1d213fa to your computer and use it in GitHub Desktop.
Save MatthewJDavis/e26f388b68d27dfede3ba8bbb1d213fa to your computer and use it in GitHub Desktop.
Airthings API Python
# Authenticate to the Airthings API and get the latest data from a particular device.
# Requires an API client created in the web gui: https://dashboard.airthings.com/integrations/api-integration
# Update script with client id and device id which can be retrieved from the devices page: https://dashboard.airthings.com/devices
# export the api secret to an environment variable called secret (Bash export secret="secret-key"
# Requires the requests package (pip install requests into the virutal environment of container).
# Python version 3.6 or above needed for f strings.
# Matthew Davis July 2022
import os
import logging
import pprint
import requests
from requests import HTTPError
client_id = ""
device_id = ""
client_secret = os.environ["secret"]
authorisation_url = "https://accounts-api.airthings.com/v1/token"
device_url = f"https://ext-api.airthings.com/v1/devices/{device_id}/latest-samples"
token_req_payload = {
"grant_type": "client_credentials",
"scope": "read:device:current_values",
}
# Request Access Token from auth server
try:
token_response = requests.post(
authorisation_url,
data=token_req_payload,
allow_redirects=False,
auth=(client_id, client_secret),
)
except HTTPError as e:
logging.error(e)
token = token_response.json()["access_token"]
# end auth token
# Get the latest data for the device from the Airthings API.
try:
api_headers = {"Authorization": f"Bearer {token}"}
response = requests.get(url=device_url, headers=api_headers)
except HTTPError as e:
logging.error(e)
print(pprint.pprint(response.json()))
print(f"co2: {response.json()['data']['co2']}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment