First Steps into the Quantified Self: Getting to Know the Fitbit API

Today I have been experimenting with the Fitbit API.

fitbit Logo
Fitbit Logo

Here are some rather amateurish steps for getting data from Fitbit’s “cloud”.

Simple Client Setup for Public Data

1. Get and setup a Fitbit product.

I have access to a set of Fitbit scales (the “Aria”). The Fitbit Force looks quite good; I may get one of those when they are launched in the UK to record steps.

2. Register an “App”

Goto Login using your Fitbit password from 1). Click the link to add an “app”.

3. Setup Libraries

Install oauth2:

sudo pip install oauth2

Clone python-fitbit:

mkdir Healthcare
cd Healthcare
git clone

4. Record Consumer Key & Secret

After registering your “app” in 2) above, you have access to a “Consumer Key” and a “Consumer Secret”. You can access these details by going to “Manage My Apps”. I then store these in a “config.ini” file I keep in my working project directory. For example:

[Login Parameters]

5. Access Public Data

With the client variables we can access public data on Fitbit.

import fitbit
import ConfigParser

#Load Settings
parser = ConfigParser.SafeConfigParser()'config.ini')
consumer_key = parser.get('Login Parameters', 'C_KEY')
consumer_secret = parser.get('Login Parameters', 'C_SECRET')

#Setup an unauthorised client (e.g. with no user)
unauth_client = fitbit.Fitbit(consumer_key, consumer_secret)

#Get data for a user
user_params = unauth_client.user_profile_get(user_id='1ABCDE')

You can get your user ID by accessing your Fitbit profile page. It is displayed in a URL just above any photo you may have. The last command returns a JSON string similar to this one:

{u'user': {u'city': u'', u'strideLengthWalking': 0, u'displayName': u'USERNAME', u'weight': 121.5, u'country': u'', u'aboutMe': u'', u'strideLengthRunning': 0, u'height': 0, u'timezone': u'UTC', u'dateOfBirth': u'', u'state': u'', u'encodedId': u'1ABCDE', u'avatar': u'https://pic_url.jpg', u'gender': u'NA', u'offsetFromUTCMillis': 0, u'fullName': u'', u'nickname': u'', u'avatar150': u'https://pic_url.jpg'}}

As I am only looking at getting a current weight reading (as taken from the last Aria scales measurement) I could stop here, as long as I have made my “body” data public (viewable by “Anyone” on the Fitbit profile > privacy page). However, as I wanted to get a BMI and body fat reading I continued with user authentication.

User Authenication

The python-fitbit library has a routine called I followed this routine in iPython to get the user key and secret for my own Fitbit account.

1. Fetch Request Token

client = fitbit.FitbitOauthClient(consumer_key, consumer_secret)
token = client.fetch_request_token()
print 'key: %s' % str(token.key)
print 'secret: %s' % str(token.secret)
print 'callback confirmed? %s' % str(token.callback_confirmed)
print ''

2. Allow Access to User Fitbit Account

I first followed the steps:

print '* Authorize the request token in your browser'
print ''
print 'open: %s' % client.authorize_token_url(token)
print ''

I had registered my callback URL as http://localhost/callback. I had not actually implemented a request handler for this URL on my local machine. However, I learnt this was not a problem – the parameters I needed were included in the callback URL. Even though the page got a ‘404’ I could still see and extract the ‘verifier’ parameter from the URL in the browser.

3. Get User Key and Secret

First I stored the ‘verifier’ parameter from the URL as a string (verifier = “*parameterfromURL*”). Then I ran these steps to get the user key and secret:

print '* Obtain an access token ...'
print ''
print 'REQUEST (via headers)'
print ''
token = client.fetch_access_token(token, verifier)
print 'key: %s' % str(token.key)
print 'secret: %s' % str(token.secret)
print ''

4. Save User Key and Secret and Use in Request

The last step is to save the user key and secret in the “config.ini” file. I saved these as “U_KEY” and “U_SECRET” in a similar manner to the consumer key and secret. Hence, these variables could be retrieved by calling:

user_key = parser.get('Login Parameters', 'U_KEY')
user_secret = parser.get('Login Parameters', 'U_SECRET')

Finally, we can use both sets of keys and secrets to access the more detailed user data:

authd_client = fitbit.Fitbit(consumer_key, consumer_secret, user_key=user_key, user_secret=user_secret)

body_stats = authd_client._COLLECTION_RESOURCE('body')


Some further work includes:

  • Adapting the python-fitbit routines for the iHealth API.
  • Building a script that gets data and imports into a website datastore.
  • Using the body data to alter a SVG likeness.

Charting Energy Use with Flask

I have developed a version of the Energy Monitor from this post that instead uses Python to display the data on an internal network.

The code can be found here:

I use Flask to render a basic template to obtain search parameters:

HTML Form for Data Selection
HTML Form for Data Selection

The code on the back-end then connects to my SQLite3 database and extracts the data according to the entered ranges. [This is slow so I may look into caching the database data.]

I then use pandas for some time-series manipulation. For example, resampling by hour, day, week, month or year is simple with pandas.

Finally I use pygal to render the manipulated data as an SVG:

2014-01-03 16.35.12
A pygal Time Series Chart

Hiding Secrets from GitHub (or using ConfigParser)

As I get into the habit of using git and github for versioning I need to make sure that I hide personal settings and passwords. To do this I use ConfigParser in Python. It’s pretty easy.

Configuration is Fun
Configuration is Fun

Creating the Configuration File

First create a ‘config.ini’ file in your code directory. I generally use a text editor or program such as Geany to do this. Section heading are enclosed in square brackets ([]) and parameters are stored in “key = value” pairs.

[Section Heading]
setting name 1 =
setting name 2 = thisismypassword

To create the file programmatically you can use code such as:

import ConfigParser

parser = ConfigParser.SafeConfigParser()
parser.add_section('Section Heading')
parser.set('Section Heading', 'Setting Name 1' , '')
parser.set('Section Heading', 'Setting Name 2' , 'thisismypassword')

with open('config.ini', 'wb') as configfile:

Reading the Configuration File

I typically read the configuration file in the ‘__init__’ method of the class I am building. Alternatively, you can do this at the start of your function or script. The code is as follows:

import ConfigParser

parser = ConfigParser.SafeConfigParser()'config.ini')
ip = parser.get('Section Heading', 'Setting Name 1')
password = parser.get('Section Heading', 'Setting Name 2')

Ignoring the Configuration File

Finally to prevent the configuration file from being uploaded to github add a ‘.gitignore’ file to the directory with a line saying ‘*.ini’.