Skip to content

Strava

Uploading GPS tracks to Strava from the command line using the Strava API.

2 min read

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):
Terminal window
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):
Terminal window
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
{
"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.

Terminal window
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:

Terminal window
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

Terminal window
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

Terminal window
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

Related articles

1 min read

gRPC

What gRPC is, why choose it over REST, and its four API types.

  • APIs & Integration
  • Architecture & Process
1 min read

Google Sheets JSON Access

Reading a public Google Sheet as JSON over HTTP, without an API key.

  • APIs & Integration
2 min read

JSON Web Tokens - jwt

How JSON Web Token authentication works, step by step.

  • Security
  • APIs & Integration

Type to search the whole site.