Strava
May 12, 2023

This short tutorial describes how to upload GPS tracks to Strava using your command line interface / shell. It requires no special tools or any 3rd party code.

References

Generate an API Key

Strava uses OAuth2 to authenticate against 3rd party applications. In order to authenticate to your Strava account from your command line, you first have to generate an API key. To create a new API, go to https://www.strava.com/settings/api. The settings are as follows:

  • Application Name choose whatever name you like
  • Website choose whatever website you want to use (needs to be a valid url]
  • Callback Domain any domain name that should be used as a callback

After you saved your API, you need to upload a image for it.

Then, copy the following values to use for HTTP requests.

  • Client ID - an ID for your application, used later on to identify your App
  • Secret - a secret token generated for you (not your OAuth Token!)

Generate an OAuth Token

Strava has defined seven different types of permissions to access the API:

  • read
  • read_all
  • profile:read_all
  • profile:write
  • activity:read
  • activity:read_all
  • activity:write

Please refer to the official documentation for what each scope represent.

  • Open the following URL (replace CLIENT_ID with the ID from above):
1
https://www.strava.com/oauth/authorize?client_id=CLIENT_ID&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%2Fexchange_token&approval_prompt=force&scope=read_all,profile:read_all,activity:write&state=mystate
  • Login to Strava then click Authorize and tick the required permissions if needed.
  • Browser should go to 404 as http://localhost/exchange_token doesn’t exist.
  • Copy the authorization code from URL that is given as the URL code parameter
  • Run the following CURL request, to get the final OAuth token (replace CLIENT_ID, CLIENT_SECRET and CODE accordingly):
1
2
3
4
5
curl -X POST https://www.strava.com/oauth/token \
  -F client_id=CLIENT_ID \
  -F client_secret=CLIENT_SECRET \
  -F code=CODE \
  -F grant_type=authorization_code
  • Copy the access_token from the JSON response - this is your OAuth token
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
{
"token_type": "Bearer",
"access_token": "987654321234567898765432123456789",
"athlete": {
#{summary athlete representation}
},
"refresh_token": "1234567898765432112345678987654321",
"expires_at": 1531378346,
"state": "STRAVA"
}

Get Authenticated Athlete

Returns the currently authenticated athlete. Tokens with profile:read_all scope will receive a detailed athlete representation; all others will receive a summary representation.

1
2
curl -X GET https://www.strava.com/api/v3/athlete \
    -H "Authorization: Bearer OAUTH_TOKEN"

Upload the tracks

Use the following command to upload a track to Strava:

1
2
3
curl -X POST https://www.strava.com/api/v3/uploads \
    -H "Authorization: Bearer OAUTH_TOKEN" 
    -F file=@"PATH_TO_FILE" -F data_type="tcx"

The other data_types can be fit, fit.gz, tcx, tcx.gz, gpx, gpx.gz.

To check the status of your update, use the id from the JSON response and run

1
curl https://www.strava.com/api/v3/uploads/ID -H "Authorization: Bearer OAUTH_TOKEN"

If you want to upload a directory with files, use the following command

1
2
3
for i in `ls /path/to/files/*.tcx`
  do curl -X POST https://www.strava.com/api/v3/uploads -H "Authorization: Bearer OAUTH_TOKEN" -F file=@"$i" -F data_type="tcx"
done