Strava is one of my most-used apps and my primary form of social media; I take great interest in the the data I can get from Strava's API on my health and fitness. This is a guide on how to make your own API application with Next.js to get your fitness data.
Create a Strava application
After you are logged in to your Strava account, head to https://www.strava.com/settings/api and create an application. Add your application's client ID and client secret to your .env:
Strava API Authentication
Every API request needs an access token, but access tokens are short-lived so you'll need a long-lived refresh token to generate new access tokens without the need to re-authenticate each time.
Authorize your app
Go to http://www.strava.com/oauth/authorize?client_id=[YOUR_CLIENT_ID]&response_type=code&redirect_uri=http://localhost/exchange_token&approval_prompt=force&scope=read and click "Authorize".
Optional: Scope change
The URL above is for only the most basic use case. I prefer to grant all permissions because the data I want requires a scope of read_all or activity:read_all, and I also want the ability to edit activities. The URL I use for authentication is http://www.strava.com/oauth/authorize?client_id=[YOUR_CLIENT_ID]&response_type=code&redirect_uri=http://localhost/exchange_token&approval_prompt=force&scope=read,read_all,profile:read_all,profile:write,activity:read,activity:read_all,activity:write. But if you're building an app for real users, you'll need to be more selective about which permissions to request.
Get the refresh token
You'll be redirected to a broken page. Look at the URL and copy the code parameter. This is your authorization code. To get your refresh token, make a curl request with your authorization code, , and :