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.
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/.
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.
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.
Thereafter I fetch this token and define a function that can be used to make API calls in my frontend.
And finally we can fetch weight data from the api.
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.
Integrating with Fitbit APIs
Kiva kun löysit tämän artikkelin! Se sisältää varmasti hyvää tietoa, mutta pidäthän mielessä, että se on kirjoitettu 8 vuotta sitten.