Accessing FitBit data in R

I caught a pretty amazing episode of Horizon (the BBC’s in depth science program in the UK) a while back called “The future of medicine is apps“. The programme explored the health benefits of giving people data about their body, health and lifestyle. The more extreme examples included the tracking of the England rugby team during training which allows the coaches to predict injury/flu before the player’s are aware of it, and the professor that monitored the level of every metabolite in his blood every day and was able to diagnose himself with Chron’s disease prior to any symptoms. At the more practical level were the people who simply track their activity levels each day. The theory goes that if you are aware via a direct data feed of what you are doing (or not doing I suppose) then you are able to make changes to your lifestyle for the better. Being a just a bit of a geek I was inspired to get myself an activity tracker and see what it was to collect some data on myself.

I settled on a FitBit Flex, which is essentially a pedometer that you wear on your wrist and which tracks activity (steps) as well as sleep patterns. I have to say it works really well and I am mightily addicted to trying to meet my activity goal each day – currently set to the default of 10,000 steps. FitBit provide a fairly slick website to display all the data you collect but, unfortunately, if you want to download the data and do any kind of analysis yourself you have to pay a pretty exorbitant subscription fee. Luckily, you can get at your data via their API if you have the know-how so I decided have a go in R.

First off you have to register an “app” with FitBit (mine is called StepTrack!) in order to get the credentials needed for authentication. I used the httr package for the OAuth authentication and data retrieval.


token_url = ""
access_url = ""
auth_url = ""
key = "my_key"
secret = "my_secret"

fbr = oauth_app('StepTrack',key,secret)
fitbit = oauth_endpoint(token_url,auth_url,access_url)
token = oauth1.0_token(fitbit,fbr)
sig = sign_oauth1.0(fbr, token=token$oauth_token, token_secret=token$oauth_token_secret)

# get all step data from my first day of use to the current date:
steps = GET("",sig)

The data is returned as json, which can then be plotted to your hearts content. In the plot you can see a five day gap – I went on holiday and forgot the charger!

> steps
Response []
Status: 200
Content-type: application/json;charset=UTF-8


Admittedly I am struggling to come up with ideas of what to do with the data that FitBit doesn’t provide already through their website. But, its the principal of the thing – I should be able to get at my data and now I can. For all of the data shown in the above I was on holiday and in general much more active than when i’m plonked in my desk at work. It will be interesting to see what my daily step count is on a normal working day and whether knowing this will push me on to go for a run at lunch time or take the very long route to the sandwich shop. Being a very competitive person, I suspect it will.

UPDATE: 4th April 2014 – @asrowe has made a nice comparison of two trackers (fitbit and jawbone) here.


12 thoughts on “Accessing FitBit data in R

    • Something like this:


      # convert JSON to a dataframe:
      data = NULL
      for (i in 1:length(content(steps)$`activities-steps`)) {
      x = c(content(steps)$`activities-steps`[i][[1]]$dateTime,content(steps)$`activities-steps`[i][[1]]$value)
      data = cbind(data,x)
      data = t(data)
      colnames(data) = c("date","steps")
      data =,row.names=1)

      # extract step counts and convert to numeric:
      steps = as.numeric(as.character(data$steps))

      # set up and plot the graph:
      brew = brewer.pal(3,"Set1") # red, blue, green
      cols = rep(brew[1],length(steps))
      cols[steps > 10000] = brew[3]

  1. Thanks for the code! I have a problem in the following line:
    token = oauth1.0_token(fitbit,fbr)

    After executing this command, the console prints the following: “Waiting for authentication in browser…” And a new browser window opens to ask me for permission. When I accept it, then I got a PIN code, and I do not know how to proceed. I am not sure if it may be a problem with the configuration of my APP. Because of my missunderstanding of the oauth protocol, I don’t know what things should I check. Any advise would be more than wellcome!

    • Ok, I resolved this issue by myself. The problem is that I configured the application as desktop and not as web app. Now, I get the authentication complete.

  2. Pingback: 2013 with Fitbits | Jean-Etienne's blog

  3. The httr library has recently been updated and one of the methods is now deprecated. I made the following updates to make it work

    The lines:
    token = oauth1.0_token(fitbit,fbr)
    sig = sign_oauth1.0(fbr, token=token$oauth_token, token_secret=token$oauth_token_secret)

    Should be replaced with:
    token = oauth1.0_token(fitbit,fbr)
    sig <-config(token = token)

    • Not sure what I am doing wrong but I can’t seem to get any data. Though, I think the handshake was succesfful after I allowed access to the browser and because the status after printing the GEt command is 200. Anyways…

      My questions:
      1. Is the GET command correct, or should I replace the hypehn in …user/-/…. with the userID ?

      2. Why can’t I see any output as the steps command in the example?

      Thanks in advance,

      HERE IS MY SHORT CODE (with suggestions from @asrowe):

      token_url = “”
      access_url = “”
      auth_url = “”
      key = “xxx”
      secret = “xxx”

      fbr = oauth_app(‘myapp’,key,secret)
      fitbit = oauth_endpoint(token_url,auth_url,access_url)
      token = oauth1.0_token(fitbit,fbr)
      sig = config (token=token)

      # get all step data from my first day of use to the current date:
      steps = GET(“”,sig)

      Here is my output:

      > token_url = “”
      > access_url = “”
      > auth_url = “”
      > key = “xx”
      > secret = “xx”
      > fbr = oauth_app(‘PACEReport’,key,secret)
      > fitbit = oauth_endpoint(token_url,auth_url,access_url)
      > token = oauth1.0_token(fitbit,fbr)
      Waiting for authentication in browser…
      Press Esc/Ctrl + C to abort
      Authentication complete.
      > sig = config (token=token)
      > # get all step data from my first day of use to the current date:
      > steps = GET(“”,sig)
      > print(steps)
      Response []
      Date: 2014-10-25 13:57
      Status: 200
      Content-type: application/json;charset=UTF-8
      Size: 681 B

      • Hi, it all looks like it’s working fine – you get a good response (200) so it was successful. Your problem is that the “steps” object is of class “response” (if you do “summary(steps)” you can see what it contains) and so it’s not as straightforward to get the data as just printing it to screen. In a reply to Chris above on September 25th 2013 I demonstrated how you can extract and convert the data to something more familiar e.g. a dataframe.

        Hope this helps.


  4. Pingback: Analysing squash performance using fitbit data | Bioinpharmatics

  5. Pingback: What We Are Reading | Quantified SelfQuantified Self

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s