百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
W

whoop

> 安全
开源

支持 OAuth2 的 WHOOP API v2 的 Python 客户端。

100 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

支持 OAuth2 的 WHOOP API v2 的 Python 客户端。

WHOOP for Python

Python client for the WHOOP API v2 with OAuth2 support.

WHOOP is a wearable strap for monitoring sleep, activity, and workouts. Learn more about WHOOP at https://www.whoop.com.

WHOOP API documentation can be found at https://developer.whoop.com/api.

Contents

  • Installation
  • Getting Started
  • Migration From Pre-1.0
  • API Requests
    • Get Activity Mapping
    • Get Profile
    • Get Body Measurement
    • Get Cycle By ID
    • Get Cycle Collection
    • Get Recovery For Cycle
    • Get Sleep For Cycle
    • Get Recovery Collection
    • Get Sleep By ID
    • Get Sleep Collection
    • Get Sleep Stream
    • Get Workout By ID
    • Get Workout Collection
  • Usage With DataFrame

Installation

The whoop module can be installed via pip:

pip install whoop

Getting Started

This client uses WHOOP's official OAuth2 authorization-code flow against the WHOOP v2 API.

Register an app

Before you can authenticate, you must register an application in the WHOOP Developer Dashboard. Registration gives you a client_id and client_secret, and lets you register one or more redirect URIs.

The redirect URI must use an https:// or whoop:// scheme — plain http://localhost is not accepted. A value like https://localhost:8080/whoop/callback works for local development.

It is best practice to store these values in a .env file:

bash
# WHOOP app credentials
CLIENT_ID=""
CLIENT_SECRET=""
REDIRECT_URI=""

You can use python-dotenv to load the environment variables for use in code:

python
import os
from dotenv import load_dotenv

load_dotenv()

client_id = os.getenv("CLIENT_ID") or ""
client_secret = os.getenv("CLIENT_SECRET") or ""
redirect_uri = os.getenv("REDIRECT_URI") or ""

Authorize a new client

Constructing a WhoopClient does not make any network requests. To authorize a brand-new client, build the authorization URL, send the user there to grant access, then exchange the authorization code returned on the redirect:

python
from whoop import WhoopClient

client = WhoopClient(client_id, client_secret, redirect_uri)

# Send the user to this URL to grant access. After they consent, WHOOP redirects
# them to your redirect_uri with an authorization code in the query string.
url, state = client.authorization_url()
print(url)

# Exchange the code for a token. Pass either the full redirect URL the user
# landed on, or the bare code extracted from it.
client.fetch_token(authorization_response=input("Redirect URL: "))
# client.fetch_token(code="")

Reuse a saved token (headless)

The constructor also accepts a previously fetched token, which skips the interactive consent flow entirely. The current token is available via the .token property:

python
saved_token = client.token  # persist this somewhere

# Later, in a headless process:
client = WhoopClient(client_id, client_secret, token=saved_token)

Tokens auto-refresh as long as you request the offline scope (included by default — see Scopes). To persist rotated tokens, pass an on_token_refresh callback that is invoked with the new token whenever a refresh occurs:

python
def save_token(token):
    ...  # write token to disk, a database, etc.

client = WhoopClient(
    client_id,
    client_secret,
    token=saved_token,
    on_token_refresh=save_token,
)

You can check whether a client holds a token with client.is_authenticated(). The client also works as a context manager, which closes the underlying session on exit:

python
with WhoopClient(client_id, client_secret, token=saved_token) as client:
    ...

Scopes

By default the client requests every read scope plus offline (required for token refresh). Pass a custom scopes list to narrow the request:

python
client = WhoopClient(
    client_id, client_secret, redirect_uri, scopes=["read:sleep", "offline"]
)

The default scopes are:

  • read:profile
  • read:body_measurement
  • read:cycles
  • read:recovery
  • read:sleep
  • read:workout
  • offline

Note WHOOP's mixed pluralization: read:cycles is plural while read:workout is singular.

Migration From Pre-1.0

Version 1.0 removes the legacy username/password flow and uses WHOOP's official OAuth2 authorization-code flow instead.

  • Replace WhoopClient(username, password) with WhoopClient(client_id, client_secret, redirect_uri).
  • Replace eager constructor authentication and authenticate() calls with authorization_url() plus fetch_token(...).
  • Persist client.token after authorization and pass it back with WhoopClient(client_id, client_secret, token=saved_token) for headless use.
  • Use get_activity_mapping(...) to look up a v2 UUID for a stored v1 activity ID.
  • Request the default offline scope, or include it in custom scopes, if you need refresh-token based auto-refresh.

API Requests

There are thirteen different API requests that WhoopClient can make. Full WHOOP API documentation can be found on WHOOP's website.

Get Activity Mapping

Look up the V2 UUID for a legacy V1 activity ID.

Method: get_activity_mapping(activity_v1_id: int)

Payload:

  • activityV1Id: Legacy V1 activity ID. Passed into the request path.

Example Response:

python
{
    "v2_activity_id": "ecfc6a15-4661-442f-a9a4-f160dd7afae8"
}

Get Profile

Get the user's basic profile.

Method: get_profile()

Payload: None

Example Response:

python
{
    "user_id": 10129,
    "email": "[email protected]",
    "first_name": "John",
    "last_name": "Smith"
}

Get Body Measurement

Get the user's body measurements.

Method: get_body_measurement()

Payload: None

Example Response:

python
{
    "height_meter": 1.8288,
    "weight_kilogram": 90.7185,
    "max_heart_rate": 200
}

Get Cycle By ID

Get the cycle for the specified ID.

Method: get_cycle_by_id(cycle_id: int)

Payload:

  • cycle_id: ID of the cycle to retrieve. Passed into the request path.

Example Response:

python
{
    "id": 93845,
    "user_id": 10129,
    "created_at": "2022-04-24T11:25:44.774Z",
    "updated_at": "2022-04-24T14:25:44.774Z",
    "start": "2022-04-24T02:25:44.774Z",
    "end": "2022-04-24T10:25:44.774Z",
    "timezone_offset": "-05:00",
    "score_state": "SCORED",
    "score": {
        "strain": 5.2951527,
        "kilojoule": 8288.297,
        "average_heart_rate": 68,
        "max_heart_rate": 141
    }
}

Get Cycle Collection

Get all physiological cycles for a user. Results are sorted by start time in descending order.

Method: get_cycle_collection(start_date: str | None = None, end_date: str | None = None)

Payload:

  • start: The earliest time for which to get data, derived from the start_date parameter. Returns cycles that occurred after or during (inclusive) this time. Accepts YYYY-MM-DD date strings or ISO datetimes. Date-only values cover whole UTC days. Defaults to six days before today (a trailing seven-day window).
  • end: The latest time for which to get data, derived from the end_date parameter. Returns cycles that intersect this time or ended before (exclusive) this time. Accepts YYYY-MM-DD date strings or ISO datetimes. Date-only values cover whole UTC days. Defaults to today's date.

Example Response:

python
[
    {
        "id": 93845,
        "user_id": 10129,
        "created_at": "2022-04-24T11:25:44.774Z",
        "updated_at": "2022-04-24T14:25:44.774Z",
        "start": "2022-04-24T02:25:44.774Z",
        "end": "2022-04-24T10:25:44.774Z",
        "timezone_offset": "-05:00",
        "score_state": "SCORED",
        "score": {
            "strain": 5.2951527,
            "kilojoule": 8288.297,
            "average_heart_rate": 68,
            "max_heart_rate": 141
        }
    },
    ...
]

Get Recovery For Cycle

Get the recovery for a cycle

Method: get_recovery_for_cycle(cycle_id: int)

Payload:

  • cycle_id: ID of the cycle to retrieve. Passed into the request path.

Example Response:

python
 {
    "cycle_id": 93845,
    "sleep_id": "ee9e6759-2cd8-4317-bc44-0a0bc59a6f1f",
    "user_id": 10129,
    "created_at": "2022-04-24T11:25:44.774Z",
    "updated_at": "2022-04-24T14:25:44.774Z",
    "score_state": "SCORED",
    "score": {
        "user_calibrating": False,
        "recovery_score": 44,
        "resting_heart_rate": 64,
        "hrv_rmssd_milli": 31.813562,
        "spo2_percentage": 95.6875,
        "skin_temp_celsius": 33.7
    }
}

Get Sleep For Cycle

Get the sleep associated with the specified cycle ID.

Method: get_sleep_for_cycle(cycle_id: int)

Payload:

  • cycle_id: ID of the cycle to retrieve sleep for. Passed into the request path.

Example Response:

…

Get Recovery Collection

Get all recoveries for a user. Results are sorted by start time of the related sleep in descending order.

Method: get_recovery_collection(start_date: str | None = None, end_date: str | None = None)

Payload:

  • start: The earliest time for which to get data, derived from the start_date parameter. Returns recoveries that occurred after or during (inclusive) this time. Accepts YYYY-MM-DD date strings or ISO datetimes. Date-only values cover whole UTC days. Defaults to six days before today (a trailing seven-day window).
  • end: The latest time for which to get data, derived from the end_date parameter. Returns recoveries that intersect this time or ended before (exclusive) this time. Accepts YYYY-MM-DD date strings or ISO datetimes. Date-only values cover whole UTC days. Defaults to today's date.

Example Response:

python
[
    {
        "cycle_id": 93845,
        "sleep_id": "ee9e6759-2cd8-4317-bc44-0a0bc59a6f1f",
        "user_id": 10129,
        "created_at": "2022-04-24T11:25:44.774Z",
        "updated_at": "2022-04-24T14:25:44.774Z",
        "score_state": "SCORED",
        "score": {
            "user_calibrating": False,
            "recovery_score": 44,
            "resting_heart_rate": 64,
            "hrv_rmssd_milli": 31.813562,
            "spo2_percentage": 95.6875,
            "skin_temp_celsius": 33.7
        }
    },
    ...
]

Get Sleep By ID

Get the sleep for the specified ID.

Method: get_sleep_by_id(sleep_id: str)

Payload:

  • sleep: ID of the sleep to retrieve. Passed into the request path.

Example Response:

python
{
    "id": "5c060dd1-975d-4544-880c-3def81bdfb0d",
    "user_id": 10129,
    "created_at": "2022-04-24T11:25:44.774Z",
    "updated_at": "2022-04-24T14:25:44.774Z",
    "start": "2022-04-24T02:25:44.774Z",
    "end": "2022-04-24T10:25:44.774Z",
    "timezone_offset": "-05:00",
    "nap": False,
    "score_state": "SCORED",
    "score": {
        "stage_summary": {},
        "sleep_needed": {},
        "respiratory_rate": 16.11328125,
        "sleep_performance_percentage": 98,
        "sleep_consistency_percentage": 90,
        "sleep_efficiency_percentage": 91.69533848
    }
}

Get Sleep Collection

Get all sleeps for a user. Results are sorted by start time in descending order.

Method: get_sleep_collection(start_date: str | None = None, end_date: str | None = None)

Payload:

  • start: The earliest time for which to get data, derived from the start_date parameter. Returns sleeps that occurred after or during (inclusive) this time. Accepts YYYY-MM-DD date strings or ISO datetimes. Date-only values cover who

Issues· 2 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Pythonapi-clienthealth-dataoauth2python

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类安全
定价开源

> 相关工具

O
OWASP ZAP
开源 Web 应用安全扫描器
O
owasp-wstg-tracker
Simple web app to track OWASP WSTG security testing progress
H
homebridge-mi-gateway-security
XiaoMi Gateway Security plugin for HomeBridge.