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 🙏

© 2024 – Pkg Stats / Ryan Hefner

fitbit-lib

v0.2.0

Published

Fitbit API for NodeJS

Downloads

43

Readme

fitbit-lib

NPM

Build Status Dependency Status

Fitbit API library for node.js

Install

$ npm install --save fitbit-lib

Usage

var express = require('express');
var app = express();
var Fitbit = require('../dist/Fitbit').Fitbit;
var cookieParser = require('cookie-parser');
var session = require('express-session');

app.use(cookieParser());
app.use(session({secret: 'bigSecret'}));
app.listen(3000);

var options = {
    creds: {
        clientID: "YOUR_CLIENT_ID",
        clientSecret: "YOUR_CLIENT_SECRET"
    },
    uris: {
        "authorizationUri": "https://www.fitbit.com",
        "authorizationPath": "/oauth2/authorize",
        "tokenUri": "https://api.fitbit.com",
        "tokenPath": "/oauth2/token"
    },
    authorization_uri: {
        "redirect_uri": "http://localhost:3000/oauth_callback",
        "response_type": "code",
        "scope": "activity",
        "state": "3(#0/!~"
    }
};

// OAuth flow
app.get('/', function (req, res) {
    // Create an API client and start authentication via OAuth

    var client = new Fitbit(options);


    res.redirect(client.authorizeURL());

});

// On return from the authorization
app.get('/oauth_callback', function (req, res) {
    var code = req.query.code;

    var client = new Fitbit(options);


    //With Promise
    client.fetchTokenAsync(code).then(function(token){
        req.session.oauth = token;

        res.redirect('/activities/steps');
    }, function(err){
                return res.send(err);
    });

    //OR with callback
    // client.fetchToken(code, function (err, token) {
    //
    //     if (err) {
    //         return res.send(err);
    //     }
    //
    //     req.session.oauth = token;
    //
    //     res.redirect('/activities/steps');
    // });

});

// Display today's steps for a user
app.get('/activity/steps', function (req, res) {
    var fitbit = new Fitbit(options);

    var date = "2016-05-27";

    fitbit.setToken(req.session.oauth);

    fitbit.getDailySteps(date, function (err, resuklt) {
        if (err) {
            res.status(400).send(err);
        } else {
        //newToken = result.token;
            res.send({value: resuklt});
        }
    });
});

app.get('/activities/steps', function (req, res) {
    var fitbit = new Fitbit(options);

    var startdate = "2016-05-27";
    var enddate = "2016-05-30";

    fitbit.setToken(req.session.oauth);

    fitbit.getTimeSeriesStepsActivity(startdate, enddate, function (err, result) {
        if (err) {
            res.status(400).send(err);
        } else {
        //newToken = result.token;
            res.send({value: result.body});
        }
    });
});


// Display activity for a user
app.get('/activity/', function (req, res) {
    var fitbit = new Fitbit(options);

    var date = "2016-05-27";

    var fibitUrl = "https://api.fitbit.com/1/user/" + req.session.oauth.user_id + "/activities/date/" + date + ".json";


    fitbit.setToken(req.session.oauth);


    fitbit.request({
        uri: fibitUrl,
        method: 'GET',
    }, function (err, body, token) {
        if (err) {
            console.log(err);
            res.send(err);
        } else {
            res.send(body);
        }
    });
});

Client API

Activity Measures

All method below have they equivalent but return PROMISE instead of used callback All method have extension Async like example below

 client.getDailyActivityAsync(date).then(function(result){
        //res.send(result);
 }, function(error){
        //throw
 });

client.getDailyActivity(date, callback)

The date is a Date object, and the callback is of the form function(err, {body : activity, token: newToken). The activity is directly format from FITBIT API.

{
    "activities":[
        {
            "activityId":51007,
            "activityParentId":90019,
            "calories":230,
            "description":"7mph",
            "distance":2.04,
            "duration":1097053,
            "hasStartTime":true,
            "isFavorite":true,
            "logId":1154701,
            "name":"Treadmill, 0% Incline",
            "startTime":"00:25",
            "steps":3783
        }
    ],
    "goals":{
        "caloriesOut":2826,
        "distance":8.05,
        "floors":150,
        "steps":10000
     },
    "summary":{
        "activityCalories":230,
        "caloriesBMR":1913,
        "caloriesOut":2143,
        "distances":[
            {"activity":"tracker", "distance":1.32},
            {"activity":"loggedActivities", "distance":0},
            {"activity":"total","distance":1.32},
            {"activity":"veryActive", "distance":0.51},
            {"activity":"moderatelyActive", "distance":0.51},
            {"activity":"lightlyActive", "distance":0.51},
            {"activity":"sedentaryActive", "distance":0.51},
            {"activity":"Treadmill, 0% Incline", "distance":3.28}
        ],
        "elevation":48.77,
        "fairlyActiveMinutes":0,
        "floors":16,
        "lightlyActiveMinutes":0,
        "marginalCalories":200,
        "sedentaryMinutes":1166,
        "steps":0,
        "veryActiveMinutes":0
    }
}

client.getDailySteps(date, callback)

The date is a Date object, and the callback is of the form function(err, {body : data, token: newToken}). The data is the integer number of steps the user has taken today.

client.getDailyFloors(date, callback)

The date is a Date object, and the callback is of the form function(err, {body : data, token: newToken}). The data is the integer number of floors the user has done today.

client.getDailyCalories(date, callback)

The date is a Date object, and the callback is of the form function(err, {body : data, token: newToken}). The data is the integer number of calories the user has consumed today.

client.getDailyElevation(date, callback)

The date is a Date object, and the callback is of the form function(err, {body : data, token: newToken}). The data is the float number of elevation the user has done today.

TimeSeries Measures

client.getTimeSeriesStepsActivity(startdate, enddate, callback)

The date is a Date object, and the callback is of the form function(err,{body : activities, token: newToken} ). The activities is an collection of steps between the two dates.

    [
        {"dateTime":"2016-05-27","value":5491},
        {"dateTime":"2016-05-28","value":12354},
        {"dateTime":"2016-05-29","value":8777},
        {"dateTime":"2016-05-30","value":8196}
    ]

Release Notes

See release notes here.