Blog 31.12.2015

Integrating with Fitbit APIs

Good to see you here! We have no doubt this post has good information, but please keep in mind that it is over 9 years old.

Wearable fitness sensors have gained popularity recently. Cheap devices measure general activity level through motion detection. More expensive devices measure more precise physiological data such as heart rate. In addition, these devices save the measurement results either in a phone or in the cloud. Fitbit is one of these manufacturers. They make portable fitness trackers and online scales.
IMG_0918
A Fitbit tracking device
Fitbit provides API access to their measurement data. This allows to fetch and integrate their measurements to other data. For example you might have a Fitbit scale and activity tracker. The API would allow you to fetch your weight and activity levels. A typical use case for this would be to integrate Fitbit data with data from some other source. This other source could be some other service or the user could manually provide some extra information that they want to quantify and compare with the Fitbit data.
One interesting measurement that Fitbit doesn’t provide is waist circumference. If you intend on losing or gaining weight, this is almost as important a measurement as your weight. To demonstrate how Fitbit’s API works, I build a simple application that fetches users weight from Fitbit and allows the user to input their waist circumference. The source code for this can be found at https://github.com/lhahne/bulker. If you have a Fitbit scale and a Fitbit user account, you can access this app at https://bulkest.herokuapp.com/.
bulker
The dataflow of this app is quite simple. Fitbit automatically collects their measurement from user’s devices. My node app then reads this data from Fitbit’s API and displays it to user for validation. Once user enters their waist circumference, both the weight (from Fitbit) and the waist measurement are saved to mongodb.
Authenticating with Fitbit
Fitbit’s oauth2 implementation seems to be somewhat different from what passport-oauth2 expects. Fortunately another package, passport-oauth2-fitbit, provides support for Fitbit’s authentication. This package only requires you to provide your API keys and to configure a callback for the authentication. Here is a short example.
https://gist.github.com/8cda593b43462914b8ae
Getting data from Fitbit
I decided to perform my API calls to Fitbit from client instead of my node server. The reason for this is, that performing the calls from server would require duplicating Fitbit’s API on my own server. To achieve this I need to transmit current user’s API from my node backend to the frontend. This is quite simple with express and passport.
https://gist.github.com/8dd60efe237bcc10cad8
Thereafter I fetch this token and define a function that can be used to make API calls in my frontend.
https://gist.github.com/3a3c8adaa492b560afe5
And finally we can fetch weight data from the api.
https://gist.github.com/26a085a0136e1f547d7f
I implemented my frontend using two react apps. First one reads measurement data and integrates it with Fitbit’s data. Second app fetches data from backend and displays it to the user. This provides a nice separation on concerns and modularity.
The application currently has only a very crude user interface. In addition, some graphs would make displaying the measurements more user friendly.
The application only currently supports Fitbit but integrating with other services would be possible. The app currently uses Fitbit’s user accounts for authentication, so handling other authentication methods and keeping track of different API tokens will require some extra work.

Back to top