Skip to content

Code Examples

Practical examples for using the AirChain API.

Python

import requests
from datetime import datetime, timedelta

API_KEY = "your_api_key_here"
BASE_URL = "https://api.airchain.ng/v1"
headers = {"Authorization": f"Bearer {API_KEY}"}

# Get all sensors in Yaba
response = requests.get(
    f"{BASE_URL}/sensors?area=Yaba",
    headers=headers
)
sensors = response.json()["sensors"]

# Get latest reading from first sensor
sensor_id = sensors[0]["id"]
response = requests.get(
    f"{BASE_URL}/readings?sensor_id={sensor_id}&limit=1",
    headers=headers
)
reading = response.json()["readings"][0]

print(f"Current AQI in {sensor_id}: {reading['aqi']}")

JavaScript

const API_KEY = 'your_api_key_here';
const BASE_URL = 'https://api.airchain.ng/v1';

async function getCurrentAQI(sensorId) {
  const response = await fetch(
    `${BASE_URL}/readings?sensor_id=${sensorId}&limit=1`,
    {
      headers: {
        'Authorization': `Bearer ${API_KEY}`
      }
    }
  );

  const data = await response.json();
  return data.readings[0].aqi;
}

getCurrentAQI('NGR-LOS-YAB-001')
  .then(aqi => console.log(`Current AQI: ${aqi}`));

More examples →