Need help getting started with REST API

Hi - I am trying to get started with the REST API using Python and need some help. I can get the current observation using the following Python code:

Get Latest Observation

resp = requests.get(‘https://swd.weatherflow.com/swd/rest/observations/station/’ + stationID + ‘?token=’ + token)
if resp.status_code != 200:
print(‘Error accessing station’)
else:
print(resp.json())

However, I don’t understand how to modify this to get a range of observations by passing things like the “obs_air”, “day_offset”, “time_start”, “time_end”, and “format” parameters. Could someone please advise or provide an example?

Thanks!

You can pass the extra parameters as query string parameters in the URL. That is you pass them after the ? in the URL (token is a query string parameter as well), and you separate them with an ampersand. For example, you can use time_start and time_end like:

https://swd.weatherflow.com/swd/rest/observations/station/1234?token=xxxx&time_start=yyyy&time_end=zzzz

or you can use day_offset like:

https://swd.weatherflow.com/swd/rest/observations/station/1234?token=xxxx&day_offset=0

Hope this helps!

if click on the api in the menu on the top of this page, you can play with it and see how it is formatted

Super helpful - thank you!!!

OK - still having problems. I can get the station status information but when I try to retrieve the observations I am getting an authorization error. The command works the api webpage but not from my python script. Advice appreciated! Code below.

Try getting observations from the station

deviceID = ‘129977’
resp = requests.get(‘https://swd.weatherflow.com/swd/rest/observations/device/129977?day_offset=0?format=csv’ )
if resp.status_code != 200:
# This means something went wrong.
print(‘Error’)
else:
print(resp)

Have you generated a token from your account and use it to authenticate ?

In settings, down the page Data authorizations to generate one or find it back if already generated

1 Like

As Eric said the URL you are using in requests.get is missing the token= parameter. Follow Eric’s instructions to get a token and then add this into the URL. You may also want to change the format=csv parameter as I have no idea how requests will deal with this option. It might work fine, so feel free to ignore

1 Like

Thanks guys! I ended up authenticating using the cURL command provided in the API webpage and things seem to work OK now. Seems like the format-csv parameter works OK. Now to figure out how to get the resulting mess into a pandas dataframe. (The idea of dumping the observations into a series of Google Sheets documents using the IFTTT initially sounded messy, but is sounding better now. This whole process seems more complicated than it should be (at least for me)…