top of page
Search
  • Writer's pictureIT_Nurse

Ready, Set, Go! Accessing Fitbit data through the API with Python

Updated: Nov 7, 2022


I learned how to do something last weekend, and I think it's pretty neat.


As you may know from this previous blog post, several years ago when I started learning PowerBI I decided to create a dashboard based on my Fitbit data. I've gone through several iterations, but they've all had one thing in common: refreshing the data is a bit of a pain. It's a manual process that requires me to sign in to my Fitbit account, navigate to the Data Export page, choose a timeframe (of not more than 30 days!) and download my records. Those records come out in one of two pre-defined formats that I then need to "clean-up" when bringing the data into PowerBI. I figured there had to be an easier way.


I did some research and found out that Fitbit has an API. Great! However, I'm not a professional developer and honestly had no idea what an API actually IS and how it would help solve my problem. Further research ensued, and with a fair amount of trial-and-error I have FINALLY figured out how to connect directly to my data, so that I can refresh my data with a single click. Here's a quick summary of how it works:


  1. I first needed Fitbit to provide me with a user ID and access token so that I could access my data through the API. To get this, I had to register an app on Fitbit's developer site, and authorize the app to send the data. This was actually really easy, and there's many articles online (such as this one) that explain the steps.

  2. I then wrote some Python code, using my user ID and access token*. Since I'm still a beginner with Python I did this in a Google Colab notebook, but I could have skipped this step and written the code directly in PowerBI. Here's the code I used to bring in my steps and distance for the month of March, and save the data in a single dataframe (aka a table)**.

  3. Once I knew my code would return my actual Fitbit data, I was able to copy and paste the data into PowerBI's Python connector. At first I thought this final step would be the easiest, but there were several other things that needed to happen first (fortunately all of these steps are covered in this video on YouTube):

    1. I needed to install Python on my computer

    2. I needed to install the Pandas and OAuth2 python libraries (because I use them in my code)

    3. I needed to make sure PowerBI could tell that Python was installed on my computer


I am beyond excited that this works. I still need to create a new version of my dashboard that brings in all of the data this way, but now that I have a proof-of-concept it's just a matter of finding the time. Once that's done, I would also like to see if I can apply these same concepts to bring in my Strava data, as they provide an API as well.


Finally, I also need to give a huge Shout Out to Women Who Code. I've recently joined their online community and it is absolutely fantastic. If you are a beginner looking to get started, or if you are looking to expand your existing skills, and you want to be part of an incredibly supportive community, you should definitely check them out.


Thanks for reading!




Extra Notes:

* If you don't have a background in programming this sentence might sound a little scary, but it's really not. Python is a programming language that's actually super easy to learn (check out this series from Women Who Code). You'll need somewhere to run your code, which is where Google Colab comes in, and this tutorial can help you get started.


** I need to give credit to arpanghosh8453 on Github as I used their code as a template for my own.


*** Here is my python code (you will need to substitute your access token and user id):



!pip install oauth2

import requests
import oauth2 as oauth2
from pprint import pprint
import pandas as pd
from datetime import date
import csv


start_date = "2022-11-01"
end_date = str(date.today())

USER_ID = '**********'
ACCESS_TOKEN = '**************'

header = {'Authorization': 'Bearer {}'.format(ACCESS_TOKEN)}


#Get Values
steps = requests.get("https://api.fitbit.com/1//user/"+USER_ID+"/activities/steps/date/"+start_date+"/"+end_date+"/1min.json", headers=header).json()['activities-steps']
elevation = requests.get("https://api.fitbit.com/1//user/"+USER_ID+"/activities/elevation/date/"+start_date+"/"+end_date+"/1min.json", headers=header).json()['activities-elevation']

#Populate dataframes with values
steps_df = pd.DataFrame(steps)
elevation_df = pd.DataFrame(elevation)

#Rename dataframe column headers
steps_df.rename({'dateTime': 'Date', 'value': 'Steps'}, axis=1,inplace=True)
elevation_df.rename({'dateTime': 'Date', 'value': 'Elevation'}, axis=1,inplace=True)

#Cast date columns to date type
steps_df['Date']= pd.to_datetime(steps_df['Date'])
elevation_df['Date']= pd.to_datetime(elevation_df['Date'])

#Merge dataframes into single activity dataframe
activity_df=pd.merge(steps_df,elevation_df, how='left', on='Date')

print(activity_df)



146 views2 comments

Recent Posts

See All
bottom of page