npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@torqlab/strava-api

v1.1.1

Published

Strava API client with automatic rate limiting, retry handling, and comprehensive type safety.

Downloads

486

Readme

Publish

@torqlab/strava-api

TypeScript client for the Strava API with automatic rate limiting, retry handling, and comprehensive type safety.

This package provides a robust interface to the Strava API, handling OAuth authentication, automatic token refresh, intelligent rate limiting, and retry logic. Built with TypeScript for complete type safety and designed for production use.

Install

Published to NPM.

npm i @torqlab/strava-api

Or with Bun:

bun add @torqlab/strava-api

Quick start

import { fetchActivity, fetchActivities, getAuthUrl, exchangeToken } from '@torqlab/strava-api';
import type { StravaApiConfig, StravaActivity } from '@torqlab/strava-api';

// OAuth flow - get authorization URL
const authUrl = getAuthUrl({
  clientId: 'your-client-id',
  redirectUri: 'http://localhost:3000/callback',
  scope: 'read,activity:read_all',
});

console.log(`Visit: ${authUrl}`);

// Exchange authorization code for tokens
const tokens = await exchangeToken({
  clientId: 'your-client-id',
  clientSecret: 'your-client-secret',
  code: 'authorization-code-from-callback',
});

// Configure API client
const config: StravaApiConfig = {
  accessToken: tokens.access_token,
  refreshToken: tokens.refresh_token,
  clientId: 'your-client-id',
  clientSecret: 'your-client-secret',
};

// Fetch a single activity
const activity: StravaActivity = await fetchActivity(config, 12345678);

console.log(`Activity: ${activity.name}`);
console.log(`Distance: ${activity.distance}m`);
console.log(`Moving time: ${activity.moving_time}s`);

// Fetch multiple activities
const activities: StravaActivity[] = await fetchActivities(config, {
  per_page: 10,
  page: 1,
});

console.log(`Found ${activities.length} activities`);

Features

  • Type Safety: Full TypeScript support with comprehensive type definitions
  • OAuth 2.0: Complete OAuth flow with authorization URL generation and token exchange
  • Automatic Token Refresh: Handles expired access tokens automatically
  • Rate Limiting: Intelligent rate limit handling with exponential backoff
  • Retry Logic: Automatic retry for transient failures
  • Error Handling: Comprehensive error types and messages
  • Validation: Input validation for activity IDs and configuration
  • Zero Dependencies: No external runtime dependencies

API Reference

Authentication

getAuthUrl(params)

Generate OAuth authorization URL.

const authUrl = getAuthUrl({
  clientId: 'your-client-id',
  redirectUri: 'http://localhost:3000/callback',
  scope: 'read,activity:read_all', // Optional, defaults to 'read'
  state: 'random-state-string', // Optional
});

exchangeToken(params)

Exchange authorization code for access tokens.

const tokens = await exchangeToken({
  clientId: 'your-client-id',
  clientSecret: 'your-client-secret',
  code: 'authorization-code',
});

Activities

fetchActivity(config, activityId)

Fetch a single activity by ID.

const activity = await fetchActivity(config, 12345678);

fetchActivities(config, params?)

Fetch multiple activities with optional pagination.

const activities = await fetchActivities(config, {
  per_page: 30,
  page: 1,
  before: 1640995200, // Unix timestamp
  after: 1609459200, // Unix timestamp
});

Configuration

The StravaApiConfig interface supports:

  • accessToken: OAuth2 access token (required)
  • refreshToken: OAuth2 refresh token for automatic renewal
  • clientId: OAuth2 client ID for token refresh
  • clientSecret: OAuth2 client secret for token refresh
  • guardrails: Optional validation callback for activities

Error Handling

The package provides comprehensive error handling with typed error codes:

import { fetchActivity } from '@torqlab/strava-api';
import type { StravaApiError } from '@torqlab/strava-api';

try {
  const activity = await fetchActivity(config, activityId);
} catch (error) {
  if (error.code === 'RATE_LIMITED') {
    console.log('Rate limited, retry after delay');
  } else if (error.code === 'UNAUTHORIZED') {
    console.log('Token expired, refresh needed');
  } else if (error.code === 'NOT_FOUND') {
    console.log('Activity not found');
  }
}

License

MIT License - see LICENSE file for details.

Contributing

Issues and pull requests are welcome on GitHub.