1 unstable release

Uses new Rust 2024

new 0.1.0 Mar 30, 2025

#365 in Web programming

MIT/Apache

50KB
945 lines

fitbit-rs

Crates.io Documentation Codecov Dependency status MIT/Apache-2.0 licensed

A work in progress Rust client library for the Fitbit API. Currently implemented are fetching sleep data and activity summaries.

Features

  • Authentication using access tokens
  • Fetch sleep data with detailed sleep stages and levels
  • Fetch activity summaries including steps, calories, heart rate zones, etc.
  • Response caching to minimize API calls

Installation

Add the following to your Cargo.toml:

[dependencies]
fitbit-rs = "0.1.0"

Usage

Basic Usage

use fitbit_rs::{FitbitClient, FitbitClientTrait};
use chrono::NaiveDate;

fn main() -> Result<(), fitbit_rs::FitbitError> {
    // Get access token (typically from environment or config file)
    let access_token = "your_access_token".to_string();
    
    // Create a client
    let client = FitbitClient::new(access_token);
    
    // Fetch today's sleep data
    let today = chrono::Local::now().date_naive();
    let sleep_data = client.fetch_sleep_data(today)?;
    
    println!("Sleep duration: {} minutes", sleep_data.summary.total_minutes_asleep);
    
    // Fetch today's activity summary
    let activity = client.fetch_activity_summary(today)?;
    println!("Steps today: {}", activity.summary.steps);
    
    Ok(())
}

Storing Access Tokens

The library provides utilities for storing and retrieving access tokens:

use fitbit_rs::access_token::{get_access_token, store_access_token};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Store an access token
    store_access_token("your_new_access_token")?;
    
    // Retrieve the stored access token
    let token = get_access_token()?;
    println!("Retrieved token: {}", token);
    
    Ok(())
}

Getting an Access Token

To use this library, you need a Fitbit API access token. You can get one by:

  1. Register an application at dev.fitbit.com
  2. Use the OAuth 2.0 flow to get an access token
  3. Store the access token using the store_access_token function or pass it directly to the client

Documentation

For more detailed documentation, see the API Documentation.

License

This project is licensed under either of

at your option.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Dependencies

~15–25MB
~446K SLoC