A Python script to export the last 30 days of Fitbit data to a CSV file.
The script exports the following Fitbit data for each day (in this column order):
- Date
- Sleep Start Time
- Sleep Stop Time
- Minutes Asleep
- Steps
- Calories Burned
- Weight
pip install -r requirements.txt- Go to Fitbit Developer
- Create a new app with the following settings:
- OAuth 2.0 Application Type: Personal
- Callback URL: http://localhost:8080/
- Default access type: Read-Only
After creating your app, you'll need to:
- Note your Client ID and Client Secret
- Generate an access token and refresh token (you may need to use a tool like Postman or a authorization code flow to get these tokens)
Edit the .env file with your Fitbit API credentials:
FITBIT_CLIENT_ID=your_client_id_here
FITBIT_CLIENT_SECRET=your_client_secret_here
FITBIT_ACCESS_TOKEN=your_access_token_here
FITBIT_REFRESH_TOKEN=your_refresh_token_here
Run the script with one of the following options:
# Default: get last 8 days of data
python fitbit_export.py # Outputs to fitbit_export-YYYY-MM-DD_to_YYYY-MM-DD.csv
# Get a specified number of days from today
python fitbit_export.py --days 30 # Outputs to fitbit_export-YYYY-MM-DD_to_YYYY-MM-DD.csv
# Get data for a specific date range
python fitbit_export.py --start 2025-04-01 --end 2025-04-30
# Get data for a single specific date
python fitbit_export.py --date 2025-04-15
# Control the order of date processing
python fitbit_export.py --days 30 --desc # Process newest to oldest dates
python fitbit_export.py --start 2025-01-01 --end 2025-04-30 --asc # Process oldest to newest (default)
# Specify a custom filename without date range
python fitbit_export.py --days 30 --output custom.csv
# Use the original filename without date range
python fitbit_export.py --days 30 --no-date-in-filename # Outputs to fitbit_export.csvFor backwards compatibility, you can also use the original positional arguments:
python fitbit_export.py 30 my_data.csv # 30 days → my_data.csvThe script will:
- Authenticate with Fitbit on first run (opens a browser window)
- Fetch the requested data one day at a time
- Write each day's data to the CSV as soon as it's retrieved
- Process dates in the specified order (oldest to newest by default)
- Handle token refresh if your access token has expired
- Include date range in the output filename by default
- The script writes data incrementally, so if it's interrupted, you'll still have partial data
- By default, dates are processed from oldest to newest (--asc), but you can use --desc to process newest to oldest
- CSV filenames now include the date range by default (e.g., fitbit_export-2025-04-26_to_2025-05-03.csv)
- Use --no-date-in-filename to revert to the original filename behavior
- The script uses the Fitbit API which has rate limits. If you encounter rate limiting issues, the script may fail.
- Some data may be missing if you didn't record it on your Fitbit device (e.g., if you didn't wear your device while sleeping).