Skip to content

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. localhost and 127.0.0.1 are 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):

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

Uploading needs activity:write. See the Strava authentication documentation for what each scope allows.

  1. Open this URL in a browser, with CLIENT_ID replaced 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
  2. Log in to Strava, tick the permissions you want to grant and click Authorize.

  3. The browser is redirected to http://localhost/exchange_token, which shows an error page because nothing runs there. That is expected.

  4. Copy the value of the code parameter from the address bar.

  5. Exchange the code for a token, replacing CLIENT_ID, CLIENT_SECRET and CODE:

    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
  6. Copy access_token from the JSON response. This is your OAuth token.

    {
    "token_type": "Bearer",
    "access_token": "987654321234567898765432123456789",
    "athlete": {},
    "refresh_token": "1234567898765432112345678987654321",
    "expires_at": 1531378346,
    "state": "STRAVA"
    }

    athlete contains 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:

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

Each 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.

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

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"

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:

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

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

References

Related notes

2 min read

gRPC

What gRPC is, how it differs from JSON over REST, and its four kinds of RPC with a Protocol Buffers service example.

  • APIs & Integration
  • Architecture & Process
2 min read

Read Google Sheets as JSON

Read a public Google Sheet as JSON over HTTP, with the keyless gviz endpoint or the Sheets API v4 and an API key.

  • APIs & Integration
3 min read

JSON Web Tokens (JWT)

How access and refresh tokens work in JWT authentication, the endpoints a server needs, and where a client should keep each token.

  • Security
  • APIs & Integration

Type to search the whole site.