Skip to content

Repository files navigation

Strava Ruby Client

Gem Version Test Coverage Status

A complete Ruby client for the Strava API v3.

It supports OAuth with refresh tokens, webhooks, pagination and rate limits, provides rich Strava models with distance, time and elevation helpers, and is tested with actual Strava data.

Table of Contents

Installation

Add to Gemfile.

gem 'strava-ruby-client'

Run bundle install.

Usage

Get a Token

Strava API calls require an OAuth access token. The token shown on My API Application can't read your activities, so use strava-oauth-token to obtain one with more scopes, or implement the OAuth workflow in your application.

STRAVA_CLIENT_ID=... STRAVA_CLIENT_SECRET=... strava-oauth-token

Save the refresh_token in a secure location and use strava-refresh-token to get a new access token when it expires.

Make Requests

client = Strava::Api::Client.new(access_token: ENV['STRAVA_ACCESS_TOKEN'])

athlete = client.athlete # => Strava::Models::DetailedAthlete
athlete.firstname # => 'Daniel'

activity = client.activity(1946417534) # => Strava::Models::DetailedActivity
activity.name # => 'TCS NYC Marathon 2018'
activity.distance_in_miles_s # => '26.26mi'
activity.strava_url # => 'https://www.strava.com/activities/1946417534'

Paginate

List APIs return one page by default. Pass per_page to retrieve all pages, limit to stop after a number of items, or a block to process items as pages arrive.

client.athlete_activities(per_page: 30, limit: 50) do |activity|
  activity # => Strava::Models::SummaryActivity
end

Handle Errors

HTTP errors raise Strava::Errors::Fault, and exceeding rate limits raises Strava::Errors::RatelimitError. Every response exposes rate limits.

begin
  athlete = client.athlete
  athlete.http_response.ratelimit.fifteen_minutes_remaining # => 590
rescue Strava::Errors::Fault => e
  e.message # => 'Authorization Error'
end

More

See docs/USAGE.md for all APIs, including activities, athletes, clubs, gear, routes, segments, streams, uploads, OAuth, webhooks, pagination, rate limits and errors.

Configuration

Web Client Options

You can configure web client options used in the OAuth and API clients, globally.

Strava::Web::Client.configure do |config|
  config.user_agent = 'Strava Ruby Client/3.0'
end

The following settings are supported.

setting description
user_agent User-agent, defaults to Strava Ruby Client/version.
proxy Optional HTTP proxy.
ca_path Optional SSL certificates path.
ca_file Optional SSL certificates file.
logger Optional Logger instance that logs HTTP requests.
timeout Optional open/read timeout in seconds.
open_timeout Optional connection open timeout in seconds.
log_formatter Optional Faraday::Logging::Formatter class, e.g. Strava::Web::LogFormatter.
filter_credentials Filter client secrets, tokens and codes from logs and errors, defaults to true.

API Client Options

The API client inherits web client options and provides additional application configuration. These can be configured globally or for a client instance.

Strava::API.configure do |config|
  config.access_token = "..." # Strava access token
end
client = Strava::API::Client.new(
  access_token: "...",
  user_agent: "..."
)

The following settings are supported.

setting description
access_token Access token to pass in the Authorization header.
endpoint Defaults to https://www.strava.com/api/v3.

OAuth Client Options

The OAuth client inherits web client options and provides additional application configuration. These can be configured globally or for a client instance.

Strava::OAuth.configure do |config|
  config.client_id = "..." # Strava client ID
  config.client_secret = "..." # Strava client secret
end
client = Strava::OAuth::Client.new(
  client_id: "...",
  client_secret: "...",
  user_agent: "..."
)

The following settings are supported.

setting description
client_id Application client ID.
client_secret Application client secret.
endpoint Defaults to https://www.strava.com/oauth.

Webhooks Client Options

The Webhooks client inherits web client options and provides additional application configuration. These can be configured globally or for a client instance.

Strava::Webhooks.configure do |config|
  config.client_id = "..." # Strava client ID
  config.client_secret = "..." # Strava client secret
end
client = Strava::Webhooks::Client.new(
  client_id: "...",
  client_secret: "...",
  user_agent: "..."
)

The following settings are supported.

setting description
client_id Application client ID.
client_secret Application client secret.
endpoint Defaults to https://www.strava.com/api/v3.

Tools

For a complete set of command-line tools, check out strava-ruby-cli built on top of this gem.

Strava OAuth Token

Use strava-oauth-token to obtain a token from the command-line. This will open a new browser window, navigate to Strava, request the appropriate permissions, then handle OAuth in a local redirect. The token type, refresh token, access token and token expiration will be displayed in the browser.

STRAVA_CLIENT_ID=... STRAVA_CLIENT_SECRET=... strava-oauth-token

Strava Refresh Token

Use strava-refresh-token to obtain a token from the command-line using an existing refresh token.

STRAVA_CLIENT_ID=... STRAVA_CLIENT_SECRET=... STRAVA_API_REFRESH_TOKEN=... strava-refresh-token

Users

Resources

Contributing

See CONTRIBUTING, CHANGELOG, UPGRADING and RELEASING.

Copyright and License

Copyright (c) 2018-2025, Daniel Doubrovkine and Contributors.

This project is licensed under the MIT License.

Releases

Sponsor this project

Packages

Used by

Contributors

Languages