Hendrik Bulens All about .NET development

C# and the Strava Web API

For those eager to share their extraordinary bike rides (or other activities) from Strava, there’s an easy way to use Web API which I will explain in this post.

You can find the source code here so you can see for yourself.

The Strava Web API is rather easy to use for the most of it because there is little security involved in the process. First, you need to create an app on the Strava Developer site. Once you’ve registered your application, you’ll receive your Client ID and Client Secret for when you want to access your private data. For public data such as your (public) rides, you won’t need this as the data is already public. There’s one piece of data that you will need to identify your user account to the API: the public access key. Once you’ve got that, you’re ready to use the Web API. For instance, the following URL retrieves my last 200 activities:


Let’s break down this URL:

  • https://www.strava.com/api/v3: this is the entry URL for the web API. This always remains the same
  • athlete: indicates I want to get data from an athlete
  • activities: indicates I want to get the activities from an athlete
  • accesstoken= [PUBLIC ACCESS KEY] indicates this is the athlete’s ID for which retrieve the activities
  • per_page= 200 ensures paging

The API’s documentation is fairly good, so if there are other elements you are looking for (like segments, gear, clubs, segment efforts, etc), chances are that the documentation will help you out. Now here comes the part that’ll probably be the reason why you have come to this place: the implementation in C#. Much like the REST API, there is little coding envolved in C#. For my website, I didn’t require private data so I won’t cover this section here. In my post about the Spotify Web API, I have covered this into greater detail so I suggest you look there for a generic approach regarding Web API authorization. My library exposes two relevant public methods, both of which calling the same base method that does all of the work. Here’s what I have done, I’ll explain all of it in a minute.

The generic GetStravaData method is actually a simple method that takes a REST URI, does the call, parses the incoming Json data and deserializes it to the specified type. Note that I use the Json.NET Nuget package to use the Deserialize method. Besides this small remark, the rest of the work – mostly parameterization – is done in the other two methods: GetStravaRides and GetAthlete. These two methods define the REST URI and retrieve the public access key from the database (you can also put a hard-coded string in there but I don’t consider that to be a good coding practice). These methods also define the type that the base method must deserialize. Using the special paste feature in Visual Studio, I have converted the raw Json that you see in your browser when you directly call the API into a CLR object. For instance, the special paste of an Athlete JSON object resulted in this:

The code to retrieve and deserialize data is quite easy, but don’t forget to define the correct REST URI and the correct data types, as these probably will cause the issues that you might have when implementing this nice features.

1 comment

Leave a Reply

Hendrik Bulens All about .NET development