Strava ExplorerStrava, the software used by athletes to track their activities, has a new API available for accessing their data. I looked through it to see if I could do anything quick and interesting with it.

Earlier this year they angered many developers by unplugging their previous API, leaving many third-party apps stranded. My own interest is in seeing how cyclists use the city: which routes are preferred. Unfortunately, the data available in Strava’s v3 API is extremely limited. Most of the API is designed to reveal a selected user’s data, assuming they have specifically granted the program access. But there doesn’t seem to be a way to look at aggregate data.

In fact there seems to be only a single API function which accepts a geographic bounding box as an input. That API, the “segment explorer” returns up to 10 “popular” segments.

I wanted to see how easy it would be to use the API in a little test program. The result, my Strava Explorer, doesn’t really do anything interesting other than prove I can connect to the Strava API.

To start, you have to get a (free) Strava account, and then sign up for the Strava Developer API, which is quick and painless. Registration will give you your unique access token, which you’ll need as a parameter for the API calls. The Strava API Reference is kept on their Github site.

The API calls are handled with HTTP, so to call you just need to configure a URL, and then read the response. Parameters are plopped into the query string. You can easily test this by entering the API URL in the browser and seeing the JSON results displayed.

I actually got this running entirely in JavaScript using the XMLHttpRequest object to asynchronously get data from another web site, but the one weakness with JavaScript is you can’t hide your access token. To get around that I wrote a super-short proxy script in PHP. All it does it append my access token to each URL. This is the PHP code you need:

<?php
echo file_get_contents("https://www.strava.com/api/v3/segments/explore" .
"?bounds=" . $_GET["bounds"] . "&access_token=YOUR-TOKEN-HERE");
?>

My map doesn’t use all of the information that I get back. I use the “id” field to track which segments have been drawn, since the user can pan and zoom the map and make more requests to show segments. I use the “points” field to draw the segment. This field uses compressed lat/lng coordinates that look like “}g|eFnm@n@Op@VJr@” – you can feed this to the Google Maps Geometry Library‘s decodePath function. decodePath() takes a string and returns an array you can use as the path of a Polyline object, which is then easily drawn on a Google map.

I use the StyledMarker library to draw markers that have the name of the segment inside. I use the “avg_grade” field to affect the color of the marker, so that steeper segments appear more purplish.

While this isn’t a very useful program, it does prove that the new Strava v3 API is usable in a JavaScript-based web program. It seems I don’t have the tools available to build something as cool as the Saturday Heatmap, which Strava produced in-house. I’ll wait to see if they expand the API’s library so third-party developers can make more interesting applications.

Update: I’ve expanded the program’s capabilities – see details at Mashing Up the Strava API. New URL is mvjantzen.com/strava.

Testing the New Version of the Strava API

Leave a Reply