A laravel package to access data from the Strava API. Compatible with Laravel 6.0
and above.
- Strava Access Credentials
- Installation
- Publish Strava Config File
- Auto Discovery
- Usage
- Available Methods
- Parameter Types
- Caching
- Useful Links
In order to use this package you will need to create an app from within your strava account. Create Strava App to access your API credentials. Click here for more information on the Strava API.
To install the package within your laravel project use the following composer command:
composer require codetoad/strava
The vendor:publish
commmand will publish a file named ct_strava.php
within your laravel project config folder config/ct_strava.php
. Edit this file with your Strava API credentials, generated from the Strava app you created.
php artisan vendor:publish --provider="CodeToad\Strava\StravaServiceProvider"
Published Config File Contents
'client_id' => env('CT_STRAVA_CLIENT_ID', '')
'client_secret' => env('CT_STRAVA_SECRET_ID', '')
'redirect_uri' => env('CT_STRAVA_REDIRECT_URI', '')
Alternatively you can ignore the above publish command and add this following variables to your .env
file. Make sure to add your Strava App credentials
CT_STRAVA_CLIENT_ID=ADD-STRAVA-CLIENT-ID-HERE
CT_STRAVA_SECRET_ID=ADD-STRAVA-SECRET-HERE
CT_STRAVA_REDIRECT_URI=ADD-STRAVA-REDIRECT-URI-HERE
If you're using Laravel 5.5+ you don't need to manually add the service provider or facade. This will be Auto-Discovered. For all versions of Laravel below 5.5, you must manually add the ServiceProvider & Facade to the appropriate arrays within your Laravel project config/app.php
CodeToad\Strava\StravaServiceProvider::class,
'Strava' => CodeToad\Strava\StravaFacade::class,
Add the Strava facade to the top of your controller so you can access the Strava class methods.
use Strava;
class MyController extends Controller
{
// Controller functions here...
}
Call the Strava::authenticate()
method to redirect you to Strava. If authentication is successful the user will be redirected to the redirect_uri
that you added to the config
file or your .env
file.
public function stravaAuth()
{
return Strava::authenticate();
}
When returned to the redirected uri, call the Strava::token($code)
method to generate the users Strava access token & refresh token. The tokens are generated using the code
parameter value within the redirected uri. Be sure to store the users access_token
& refresh_token
in your database.
public function getToken(Request $request)
{
$code = $request->code;
$token = Strava::token($code);
// Store $token->access_token & $token->refresh_token in database
}
Example Response
"token_type": "Bearer"
"expires_at": 1555165838
"expires_in": 21600 // 6 Hours
"refresh_token": "671129e56b1ce64d7e0c7e594cb6522b239464e1"
"access_token": "e105062b153da39f81a439b90b23357c741a40a0"
"athlete": ...
At this point you have access to the Athlete
object that can be used to login to your website. Of course you'll need to write the logic for your login system, but the athlete name, email etc.. is contained within the object for you to verify the user against your own database data.
Access tokens will now expire after 6 hours under the new flow that Strava have implemented and will need to be updated using a refresh token. In the example above you can see the response has a refresh_token
and an expires_at
field. When storing the user access tokens you may also want to store the expires_at
field too. This will allow you to check when the current access token has expired.
When calling any of the Strava methods below you may want to compare the current time against the expires_at
field in order to validate the token. If the token is expired you'll need to call the Strava::refreshToken($refreshToken)
method in order to generate a new tokens. All you need to do is pass the users currently stored refresh_token
, the method will then return a new set of tokens (access & refresh), update the current users tokens with the new tokens from the response. Heres an example of how that might work, using the Strava::athlete($token)
method.
public function myControllerFunction(Request $request)
{
// Get the user
$user = User::find($request->id);
// Check if current token has expired
if(Carbon::now() > $user->expires_at)
{
// Token has expired, generate new tokens using the currently stored user refresh token
$refresh = Strava::refreshToken($user->refresh_token);
// Update the users tokens
User::where('id', $request->id)->update([
'access_token' => $refresh->access_token,
'refresh_token' => $refresh->refresh_token
]);
// Call Strava Athlete Method with newly updated access token.
$athlete = Strava::athlete($user->access_token);
// Return $athlete array to view
return view('strava.athlete')->with(compact('athlete'));
}else{
// Token has not yet expired, Call Strava Athlete Method
$athlete = Strava::athlete($user->access_token);
// Return $athlete array to view
return view('strava.athlete')->with(compact('athlete'));
}
}
You can allow users to unauthenticate their Strava account with your Strava app. Simply allow users to call the following method, passing the access token that has been stored for their account.
Strava::unauthenticate($token);
All methods require an access token, some methods accept additional optional parameters listed below.
- Optional Parameters
- $perpage (Int - default 10)
- $perpage (Int - default 10)
- $before (Int/Timestamp - default = most recent)
- $after (Int/Timestamp - default = most recent)
Returns the currently authenticated athlete.
Strava::athlete($token);
Returns the activities of an athlete.
Strava::activities($token, $page, $perPage, $before, $after);
Returns the given activity that is owned by the authenticated athlete.
Strava::activity($token, $activityID);
Returns the comments on the given activity.
Strava::activityComments($token, $activityID, $page, $perPage);
Returns the athletes who kudoed an activity.
Strava::activityKudos($token, $activityID, $page, $perPage);
Returns the laps data of an activity.
Strava::activityLaps($token, $activityID);
Summit Feature Required. Returns the zones of a given activity.
Strava::activityZones($token, $activityID);
Returns the the authenticated athlete's heart rate and power zones.
Strava::athleteZones($token);
Returns the activity stats of an athlete.
Strava::athleteStats($token, $athleteID, $page, $perPage);
Returns a given club using its identifier.
Strava::club($token, $clubID);
Returns a list of the athletes who are members of a given club.
Strava::clubMembers($token, $clubID, $page, $perPage);
Retrieve recent activities from members of a specific club. The authenticated athlete must belong to the requested club in order to hit this endpoint. Pagination is supported. Athlete profile visibility is respected for all activities.
Strava::clubActivities($token, $clubID, $page, $perPage);
Returns a list of the administrators of a given club.
Strava::clubAdmins($token, $clubID, $page, $perPage);
Returns a list of the clubs whose membership includes the authenticated athlete.
Strava::athleteClubs($token, $page, $perPage);
Returns equipment data using gear ID.
Strava::gear($token, $gearID);
Returns a route using its route ID.
Strava::route($token, $routeID);
Returns a list of the routes created by the authenticated athlete using their athlete ID.
Strava::athleteRoutes($token, $athleteID, $page, $perPage);
Returns the specified segment.
Strava::segment($token, $segmentID);
Returns a segment effort from an activity that is owned by the authenticated athlete.
Strava::segmentEffort($token, $segmentID);
List of the authenticated athlete's starred segments.
Strava::starredSegments($token, $page, $perPage);
$token = String
$activityID = integer
$athleteID = integer
$clubID = integer
$gearID = integer
$routeID = integer
$segmentID = integer
$page = integer
$perPage = integer
$before = integer (timestamp)
$after = integer (timestamp)
It's highly recommended that you cache your requests made to Strava for 2 reasons.
Strava have quite a good API Rate Limit, 600 requests every 15 minutes, 30,000 daily. If your website has high traffic you might want to consider caching your Strava response data so you don't exceed these limits.
Caching your Strava data will drastically improve website load times.