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 Namechoose whatever name you likeWebsitechoose whatever website you want to use (needs to be a valid url]Callback Domainany 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 AppSecret- 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):
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
Authorizeand tick the required permissions if needed. - Browser should go to 404 as
http://localhost/exchange_tokendoesn’t exist. - Copy the authorization code from URL that is given as the URL
codeparameter - Run the following CURL request, to get the final OAuth token (replace
CLIENT_ID,CLIENT_SECRETandCODEaccordingly):
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_tokenfrom 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.
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:
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
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
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