Skip to content

API access limit tracking #134

Description

@frejonb

I was trying to get my heart rate intraday series for a whole year but the API only supports pulling data for 1 day at a time, which hits the call limit (only 150 requests per hour).

I added the rate limit attributes to the Fitbit class in order to track these in #133.

With these I can pull data of 150 days at a time...

Activity

  1. jacobthejones commented on Dec 6, 2025

    @jacobthejones

    If anyone else runs into this and doesn't want to fork the repo, you can add the attributes using request hooks like so:

    client = fitbit.Fitbit(
        client_id,
        client_secret,
        oauth2=True,
        access_token=access_token,
        refresh_token=refresh_token,
        refresh_cb=on_token_refresh,
    )
        
    # Initialize rate limit attributes to None
    client.ratelimit_limit = None
    client.ratelimit_remaining = None
    client.ratelimit_reset = None
    # Define a hook function to grab headers from every response
    def save_rate_limit(response, *args, **kwargs):
        client.ratelimit_limit = response.headers.get('Fitbit-Rate-Limit-Limit')
        
        raw_remaining = response.headers.get('Fitbit-Rate-Limit-Remaining')
        if raw_remaining is not None:
            client.ratelimit_remaining = int(raw_remaining)
        else:
            client.ratelimit_remaining = 0
        
        raw_reset = response.headers.get('Fitbit-Rate-Limit-Reset')
        if raw_reset is not None:
            client.ratelimit_reset = int(raw_reset)
        else:
            client.ratelimit_reset = 0
    # Attach the hook to the client's internal session
    client.client.session.hooks['response'].append(save_rate_limit)
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions