Strava
Upload GPS tracks to Strava from the command line with curl: create an API application, get an OAuth token and call the uploads endpoint.
3 min readUpdated
Strava’s API v3 lets you upload activity files (FIT, TCX, GPX) without the web interface. This note shows how to do it from a shell with nothing but curl: create an API application, authorise it once in the browser to get an OAuth token, then upload single files or a whole directory.
Create an API application
Strava uses OAuth 2 to grant third-party applications access to an account, so you first need an application of your own. Create one on the Strava API settings page:
- Application Name: any name you like.
- Website: any website; it must be a valid URL.
- Authorization Callback Domain: the domain Strava may redirect to after authorisation.
localhostand127.0.0.1are always allowed, which is all this tutorial needs.
After saving the application, Strava asks you to upload an icon for it.
Note two values from the application page for the requests below:
- Client ID: identifies your application.
- Client Secret: a secret generated for the application. It is not an OAuth token.
Get an OAuth token
Strava defines seven scopes (permissions):
readread_allprofile:read_allprofile:writeactivity:readactivity:read_allactivity:write
Uploading needs activity:write. See the Strava authentication documentation for what each scope allows.
-
Open this URL in a browser, with
CLIENT_IDreplaced by your client ID: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 -
Log in to Strava, tick the permissions you want to grant and click Authorize.
-
The browser is redirected to
http://localhost/exchange_token, which shows an error page because nothing runs there. That is expected. -
Copy the value of the
codeparameter from the address bar. -
Exchange the code for a token, replacing
CLIENT_ID,CLIENT_SECRETandCODE: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
access_tokenfrom the JSON response. This is your OAuth token.{"token_type": "Bearer","access_token": "987654321234567898765432123456789","athlete": {},"refresh_token": "1234567898765432112345678987654321","expires_at": 1531378346,"state": "STRAVA"}athletecontains a summary of the authorised athlete, shortened here.
Refresh an expired token
Access tokens expire six hours after they are created (expires_at is the expiry as a Unix timestamp). Get a new one with the refresh token from the last response:
curl -X POST https://www.strava.com/oauth/token \ -d client_id=CLIENT_ID \ -d client_secret=CLIENT_SECRET \ -d grant_type=refresh_token \ -d refresh_token=REFRESH_TOKENEach response contains a new refresh token; use the latest one next time.
Check the token
This request returns the authenticated athlete, which confirms the token works. Tokens with the profile:read_all scope get the detailed athlete representation; others get a summary.
curl -X GET https://www.strava.com/api/v3/athlete \ -H "Authorization: Bearer OAUTH_TOKEN"Upload tracks
Upload one file with a POST to the uploads endpoint:
curl -X POST https://www.strava.com/api/v3/uploads \ -H "Authorization: Bearer OAUTH_TOKEN" \ -F file=@"PATH_TO_FILE" \ -F data_type="tcx"data_type must match the file format: fit, fit.gz, tcx, tcx.gz, gpx or gpx.gz.
Strava processes uploads asynchronously. To check the status, take the id from the upload response and run:
curl https://www.strava.com/api/v3/uploads/ID \ -H "Authorization: Bearer OAUTH_TOKEN"To upload every TCX file in a directory, loop over the files:
for f in /path/to/files/*.tcx; do curl -X POST https://www.strava.com/api/v3/uploads \ -H "Authorization: Bearer OAUTH_TOKEN" \ -F file=@"$f" \ -F data_type="tcx"done