strava-client

Strava V3 API client

strava-client

Node module for accessing Strava's V3 API http://strava.github.io/api/

This is a simple REST API client wrapper for Strava's V3 API http://strava.github.io/api/. It is incomplete. The only methods supported so far are the ones I need for a simple web app I am developing. I will add more methods as and when I need them. Please do not send issue reports that method X is not implemented. Please add what you need. Forking and contributing is easy.

  • Athlete

    • Retrieve Current Athlete
    • Retrieve Another Athlete
    • List Athlete's K/QOMs/CRs
    • List Current Athlete's Friends
    • List Another Athlete's Friends
    • List Current Athlete's Followers
    • List Another Athlete's Followers
  • Activities

    • List Athlete Activities
    • Retrieve An Activity
    • Retrieve Activity Streams

Strava lets you track your rides and runs via your iPhone, Android or dedicated GPS device and helps you analyze and quantify your performance.

This project is not affiliated with Strava in any way.

 
/**
 * load dependencies
 */
var StravaClient = require('strava-client');
var _ = require('underscore');
 
/**
 * provide a JSON configuration for the Strava API client instance
 *
 *  {
 *     "clientID"    : your-app-id,
 *     "clientSecret": "your secret key",
 *     "accessToken" : "your access token"
 *     "testActivityId": id-of-one-of-your-activities
 *   }
 *
 */
var config = require('../strava.secrets');
 
/**
 * create an instance of the Strava API client
 */
var stravaClient = new StravaClient(config);
 
/**
 * Define our request options
 *
 * id = the activity id
 *
 * types = a comma separated list of stream types.
 *
 * allowed values are:
 * - time:  integer seconds
 * - latlng:  floats [latitude, longitude]
 * - distance:  float meters
 * - altitude:  float meters
 * - velocity_smooth:  float meters per second
 * - heartrate:  integer BPM
 * - cadence:  integer RPM
 * - watts:  integer watts
 * - temp:  integer degrees Celsius
 * - moving:  boolean
 * - grade_smooth:  float percent
 *
 * query - hash of query string keys/value pairs
 *
 * allowed keys/value pairs are:
 *
 * resolution:
 *  values low, medium or high
 *
 * series_type: (relevant only if using resolution)
 * either 'time' or 'distance', default is 'distance'
 *
 */
var options = {
  id   : 108908089,
  types: 'latlng',
  query: {
    resolution : 'low',
    series_type: 'time'
  }
};
 
/**
 * Define a callback function to execute when the REST request completes
 * errors - null if the request is successful
 * data - the response object (already parsed JSON)
 */
var callback = function (errorsdata) {
 
  /* if errors, dump them to console and return early */
  if (errors) {
    console.log(errors);
    return;
  }
 
  /* use underscore's 'find' method to get the 'time' stream from the data
   * (because the response is an array of objects each with a type property) */
  var timeStream = _.find(data, function (stream) {
    return stream.type === 'time';
  });
 
  /* use underscore's sample method to dump 5 random entries from the stream */
  console.log(_.sample(timeStream.data, 5));
 
  var latlngStream = _.find(data, function (stream) {
    return stream.type === 'latlng';
  });
 
  console.log(_.sample(latlngStream.data, 5));
};
 
/**
 * execute the method 'retrieveActivityStreams' - a thin
 * wrapper to the REST endpoint /activities/:id/streams/:types
 * passing in our options and callback method
 */
stravaClient.retrieveActivityStreams(options, callback);
 

Clone the repo and create a strava.secrets.json file containing your API credentials in the project root like this:

    {
      "clientID"    : 123,
      "clientSecret": "your secret here",
      "accessToken" : "your access token here",
      "testActivityId": "a real activity id for testing",
      "testAthleteId": "a real athlete id for testing"
    }

Run npm install to get the dependencies

    npm install

Run the tests from npm

    npm test

Alternatively, if you want to use grunt, run the Mocha tests from grunt (it will run the source through JSHint first)

    grunt mochacli

If you are new to grunt, you can install it globally like this

    npm install grunt-cli -g

If you are on Windows, you might need to add this to your PATH environment variables

    C:\Users[username]\AppData\Roaming\npm