Getting access to the Strava API

This post describes how you can get access to the Strava API so that you can build an application.

For this you will need some basic level of coding with HTML, CSS, Javascript and PHP as well as a web host that runs PHP.  If you would like my recommendation for a good free web host I use x10hosting.com.  You will also need a Strava Account.

Step 1. Create the application in Strava

Head over to the strava developers page at https://developers.strava.com/ and click the button that looks like this: 

From here you will log in to Strava (if not already logged in) and then to your settings page where you can enter the details required to set up your app.

I should do the right thing here and mention that you should read the Strava API Agreement before you go too much further with this, just to make sure that what you are creating doesn't go against any of the Terms of Use


At this point you are just creating a webpage for your own use for building and testing, so you don't need to enter any actual details, besides, you don't want some doofus to accidentally come across your website at this stage.  So you can enter your details like this, these can all be changed later:






When your app is ready to go public you will need to change the 'Website' field to the home page of your website and the 'Authorisation Callback Domain' to your website's domain.  Everything else is just for the Strava apps catalogue.



After this step you are required to upload an image to be used as the app's logo.  Once again, you can upload any old photo at this stage and change it later when you go public.



Once your app is created on Strava, your Strava settings page will have a new section called 'My API Application', (congratulations, you have a Strava API app).  On it you will have some codes required to make your app do stuff, specifically the Client ID, Client Secret and Your Access Token.  Make sure when you use these in your App that they are never visible to anyone that might use your credentials to build something illegal. 








Step 2. Time to start building your app


In your favourite text editor create a PHP file and create these variables (replace the asterisks with the actual values provided on your strava API settings page):

<?php

// My credentials 

   $clientId = "*****";        //**== Enter your strava client ID ==**
   $clientSecret = "*****";    //**== Enter your strava client Secret ==**
   $clientToken = "*****";     //**== Enter your strava Access Token ==**
?>



These variables will be your keys to accessing strava data.
Use this code to make an API request and decode the returned data:

<?php

// My details

$data = file_get_contents("https://www.strava.com/api/v3/athlete/?access_token=$clientToken");
   $summary = json_decode($data, true);
   
// Required details Name, photo, location 
   $userId = $summary["id"];
   $fname = $summary["firstname"];
   $lname = $summary["lastname"];
   $photo = $summary["profile_medium"];
   $city = $summary["city"];
   $state = $summary["state"];
   $country = $summary["country"];

?>



You can then 'echo' this onto your screen or into a Javascript variable or anything else that you need to do with it.

The above describes how to make a request to get the user details of the Strava athlete that is making the request, there is a full list of the different types of request here: https://developers.strava.com/, along with information about the data that is returned from the different requests.  I have sometimes found this documentation to be inaccurate so I tend to find out what details result from different types of request and how to decode them by typing the request into the address bar of a new browser tab like so:


The result of this looks like a long list of garbage, so copy and paste it all into a text editor then manually sort through it by placing the text on a new line after every comma so that you end up with something that looks like this:

{
   "id":614155,
   "username":"mark_stubbs",
   "resource_state":3,
   "firstname":"mark",
   "lastname":"stubbs",
   "city":"Hobart",
   "state":"Tasmania",
   "country":"Australia",
etc...

You can then save this as your reference for an API athlete request.  Some of the data won't make much sense, for example in the above snippet there is something called a resource state - I still don't know what that is! - so you might need to search for some info in the Strava API documentation.

The above is an example of an athlete request that returns the details of the athlete making the request - i.e. the user token that you created in Strava and that is used in your request is connected to you and so will only return your own data - at the moment for building and testing this is sufficient, I'll show you how to make your website public so that other people can have their own data on the screen in a later post. 

Here's an example of how a whole PHP file that requests, returns and displays the Strava athlete's details might look:
<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="utf-8">
   <title>My Strava API</title>
   <meta name="Description" content="Strava API.">
</head>

<?php


// My credentials 

   $clientId = "*****";        //**== Enter your strava client ID ==**
   $clientSecret = "*****";    //**== Enter your strava client Secret ==**
   $clientToken = "*****";     //**== Enter your strava Access Token ==**

   $data = file_get_contents("https://www.strava.com/api/v3/athlete/?access_token=$clientToken");

   $summary = json_decode($data, true);
   
// User details Name, photo, location, etc
   $userId = $summary["id"];
   $userFirstname = $summary["firstname"];
   $userLastname = $summary["lastname"];
   $userPhoto = $summary["profile_medium"];
   $userCity = $summary["city"];
   $userState = $summary["state"];
   $userCountry =  $summary["country"]; 
   $userJoinedDate = $summary["created_at"];
// The date that the user started using strava is returned in this format - YYYY-MM-DDTHH:MM:SSZ
// which you can format into something more friendly:
   $JoinedDateFormatted = date("D - d M Y" ,strtotime($userJoinedDate));
   $userDistPref = $summary["measurement_preference"];
   $userActivityPref = $summary["athlete_type"]; 
// The user's preffered activity is either 1 or 0, which translates as 1 = run, 0 = ride   
   if($activityPref == 1){
     $prefType = "Run";
    } else {
     $prefType = "Ride";
    }
?>
<body>

<header>

   
   <div>
     <h1>My Strava API Application</h1>
     <p>This is my file for playing with strava data</p>
   </div>
   
   <div>
     <h4>Logged in as:</h4>
     <p><?php echo $userFirstname . " " .  $userLastname;?></p>
     <img src = "<?php echo $userPhoto;?>" alt "Strava profile image" />
     <p>
       <?php if ($userCity != "") {echo $userCity ." | ";}?>
       <?php if ($userState != "") {echo $userState ." | ";}?>
       <?php if ($userCountry != "") {echo $userCountry;}?>
     </p>
     <p>
       Strava member since: <?php echo $JoinedDateFormatted;?><br />
       Preferred Measurements: <?php echo $userDistPref;?><br />
       Preferred Activity: <?php echo $prefType;?>
   </div>
   
</header>


</body>

</html>


So far I have explained how to set up your Strava API, get the required token, how to use it to make a basic request for your user information and how to display it on your web page.  I will explore other types of request in my next post.

Comments

Popular posts from this blog

Working with the Strava API's Activity data

Introduction