Getting historical data with API keys in Python

Hi all,
I am trying to download some historical data from the last 2 months using python but seem to be running into some issues, I was wondering has anyone been using python rather than R that could help me out?

This is where I’m at so far:
import requests

#store API read key

#sensor identifier
sensor_index = XXXXX

#to collect data
def getSensorData(sensor_index):

#assigning URL that request is being sent to
my_url = '' + str(XXXXX)

#assigning context of request 
my_headers = {'X-API-Key':read_key}

r = requests.get(my_url, headers=my_headers)

print (r.content)
return (r)


The API for historical access is not available at the moment ( Downloaded data not appearing in CSV file - #5 by Ethan_Breinholt ). I think you’ll need special access to download historical data. Especially if it is for a big time range. I would suggest reaching out to purple air support for help.

1 Like

This has worked for me:

from purpleair import PurpleAir
p = PurpleAir(read_key)

r = p.get_sensor_history(sensor_index=sensor, start_timestamp, end_timestamp, fields=(‘pm2.5_alt’, ))

Then script in for loop reading each line in variable r which is in json format.

1 Like

Thanks so much Brandyn, much appreciated!

Hey Ashling, you may find this post helpful: Making API Calls in Other Languages