Skip to main content

API examples in python

To run the examples you need API key and installed python3 with library requests.

Monitoring Posts

List of available post objects

import json
import os

import requests

api_url = 'https://mycityair.ru/harvester/v2/Posts'
api_key = os.getenv('CITYAIR_TOKEN') # or set your api key directly

assert api_key, 'API key is empty'

headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}',
}
response = requests.get(api_url, headers=headers)
response.raise_for_status()

formatted_response = json.dumps(response.json(), indent=2, ensure_ascii=False)
print(formatted_response)

Measurement data of individual post object


import datetime
import os
import json

import requests

post_list_url = 'https://mycityair.ru/harvester/v2/Posts'
api_key = os.getenv('CITYAIR_TOKEN') # or set your api key directly

assert api_key, 'API key is empty'

headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}',
}
response = requests.get(post_list_url, headers=headers)
response.raise_for_status()
posts = response.json()
assert len(posts) > 0, 'At least one post is required'
post = posts[0]
print(f'Got post {post["id"]}:{post["name"]}')


interval = '1h'
date_start = (datetime.datetime.utcnow() - datetime.timedelta(days=1)).isoformat()
post_measurements_url = f'https://mycityair.ru/harvester/v2/Posts/{post["id"]}/measurements?' \
f'interval={interval}&' \
f'date__gt={date_start}'
response = requests.get(post_measurements_url, headers=headers)
assert response.status_code == 200, f'Server responded with error: {response.text}'
response.raise_for_status()

formatted_response = json.dumps(response.json(), indent=2, ensure_ascii=False)
print(formatted_response)

Station objects

Working with data sources

List of station objects


import os
import json

import requests

station_list_url = 'https://mycityair.ru/harvester/v2/Stations'
api_key = os.getenv('CITYAIR_TOKEN') # or set your api key directly

assert api_key, 'API key is empty'

headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}',
}
response = requests.get(station_list_url, headers=headers)
response.raise_for_status()

formatted_response = json.dumps(response.json(), indent=2, ensure_ascii=False)
print(formatted_response)

Measurement data

import os
import json

import requests
import datetime

station_list_url = 'https://mycityair.ru/harvester/v2/Stations'
api_key = os.getenv('CITYAIR_TOKEN') # or set your api key directly
assert api_key, 'API key is empty'

headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}',
}
response = requests.get(station_list_url, headers=headers)
response.raise_for_status()
stations = response.json()
assert len(stations) > 0, 'At least one station is required'
station = stations[0]
print(f'Got station {station["id"]}:{station["name"]}')

date__gt = (datetime.datetime.utcnow() - datetime.timedelta(hours=1)).isoformat()
date__lt =datetime.datetime.utcnow().isoformat()
# The default limit is 1000 packets in the data section, so use shorter intervals
post_measurements_url = f'https://mycityair.ru/harvester/v2/Stations/{station["id"]}/measurements?' \
f'date__gt={date__gt}&' \
f'date__lt={date__lt}'

response = requests.get(post_measurements_url, headers=headers)
assert response.status_code == 200, f'Server responded with error: {response.text}'
response.raise_for_status()
formatted_response = json.dumps(response.json(), indent=2, ensure_ascii=False)
print(formatted_response)