API’s are scary huh? I feel like us data people just use fancy words sometimes for the sake of it. That, and we write documentation that makes zero sense, no shade intended.
Well, welcome to my dummies guide on how to access your Strava data using python and postman. What I will go through today is how to successfully set up your Strava API connection and retrieve ALL of your Strava stats from your account.
A few really useful resources before we start:
- A Strava Account!
- Ability to run (not necessarily understand) python code, as well as access to postman (We will cover access)
Okay let’s crack on.
You can login to Strava, here.
Navigate to your Settings API Page.
This will store all your starting information.
You may need to create the initial API account application.
- After you are logged in, go to https://www.strava.com/settings/api and create an app.
- You should see the “My API Application” page now. Here is what everything means:
- Category: The category you chose for your application (doesn’t matter)
- Club: not necessary
- Client ID: Your application ID (ID for your API)
- Client Secret: Your client secret (confidential!)
- Authorization token: Your authorization token which will change every six hours (confidential)
- Your Refresh token: The token you will use to get a new authorization token (confidential)
- Rate limits: Your current rate limit (lets not spam it)
- Authorization Callback Domain: set to ‘localhost’
Once we’ve done that,
We can then refer back to the Strava documentation where it is asking to make a curl request. What does that mean??? Well its a command line tool that helps make http requests, but we will use Postman to do this, so lets set up our postman account.
You can sign up here, for free.
Once you’ve made an account, click get started with something new.
Postman makes requests alot easier to build. What it’ll help us do is run our commands based on the access token we feed it.
So the documentation tells us we need to then make a GET request based on this link.
For it to work we need to add in our Bearer token.
Go to the header and add ‘authorization’ in the key.
Add into the value ‘Bearer YOURACCESSTOKEN’
Amazing, if it pops up with your account details you’ve done it correctly.
We now know who we are….
If we go back to the documentation now we now notice in part D we have to authorise our account. This is because our account is set to ‘read’ and not ‘read_all’ mode. This will allow us to retrieve all our data.
First we need to get our authorisation code from the authorisation page.
- Go to https://www.strava.com/settings/api and copy your Client ID
- Paste your Client ID into this
** Note, it is really important to have this say read_all, this will allow us to be able to find all our data.
Click Authorise and save down the new url, it stores the code that we will need later.
Don’t worry if the page errors out. This gives us a new code for our access token and refresh token for a read_all. This will give access to the Strava App.
Go back into to Postman.
We want to generate a new token with the scope that we have now requested.
Copy and past the url above into postman. Make sure the setting is on POST. I personally would start a new tab for this.
Replace your client_id, client_secret from your API Application Page. Add the new code that we just retrieved into the value of code_saved_from_previous.
Sorry lots of red pen, but if it works when you hit the send button this should generate your refresh token and access tokens!
** Do note your new refresh token and access token will be different to previous**
We can copy these for our code. Finally we are ready to run some code.
Time to open up Pycharm.
Copy and paste the main.py script from the repo at the top of the page. The code is written in a way that it’ll loop through all your activities until there are no more to find. Some of the code has been pulled together from various other articles.
The payload will include the client_id, client_secret from your main Strava page.
The refresh token is the token we just generated in Postman. This means we can continue to request data.
Once it authorises, it will print out the access token that you will have just seen in our latest postman POST request.
So what does the remainder of the code do?
Well according to the Strava documentation, there is pagination that we need to account for. I.e if we didnt set our parameters we would only get a small view of the true number of activities we’ve done. By being able to flick through the pages we can now store all the activities.
I’ll put some more sanitised example data from my results on the GitHub repo so that you can take a look at what the fields may look like.
Let me know how you get on with setting up and connecting your own API. It can be a little fiddly, especially with the need to set your tokens for read_all access.
A few common errors if you get stuck during the process:
- If you’re using postman for the first time, do not get params and headers mixed up.
- GET & POST are two very different things. Double check you’re trying to do the correct thing.
- When you paste in your codes / tokens make sure not to leave a leading or trailing space!
- Make sure that each part of the postman requests are working before you try running the code, otherwise it will fail if you try jump straight into the code.
- For the code, make sure you have all the packages pip installed that are required.
In future weeks we will look to see how we can visualise some of these elements.
For now, here is a quick calendar I put together in Tableau, nothing too fancy for now.