支持 OAuth2 的 WHOOP API v2 的 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.
The whoop module can be installed via pip:
pip install whoop
This client uses WHOOP's official OAuth2 authorization-code flow against the WHOOP v2 API.
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:
# WHOOP app credentials
CLIENT_ID=""
CLIENT_SECRET=""
REDIRECT_URI=""You can use python-dotenv to load the environment variables for use in code:
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 ""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:
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="")The constructor also accepts a previously fetched token, which skips the interactive consent flow entirely. The current token is available via the .token property:
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:
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:
with WhoopClient(client_id, client_secret, token=saved_token) as client:
...By default the client requests every read scope plus offline (required for token refresh). Pass a custom scopes list to narrow the request:
client = WhoopClient(
client_id, client_secret, redirect_uri, scopes=["read:sleep", "offline"]
)The default scopes are:
read:profileread:body_measurementread:cyclesread:recoveryread:sleepread:workoutofflineNote WHOOP's mixed pluralization: read:cycles is plural while read:workout is singular.
Version 1.0 removes the legacy username/password flow and uses WHOOP's official OAuth2 authorization-code flow instead.
WhoopClient(username, password) with WhoopClient(client_id, client_secret, redirect_uri).authenticate() calls with authorization_url() plus fetch_token(...).client.token after authorization and pass it back with WhoopClient(client_id, client_secret, token=saved_token) for headless use.get_activity_mapping(...) to look up a v2 UUID for a stored v1 activity ID.offline scope, or include it in custom scopes, if you need refresh-token based auto-refresh.There are thirteen different API requests that WhoopClient can make. Full WHOOP API documentation can be found on WHOOP's website.
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:
{
"v2_activity_id": "ecfc6a15-4661-442f-a9a4-f160dd7afae8"
}Get the user's basic profile.
Method: get_profile()
Payload: None
Example Response:
{
"user_id": 10129,
"email": "[email protected]",
"first_name": "John",
"last_name": "Smith"
}Get the user's body measurements.
Method: get_body_measurement()
Payload: None
Example Response:
{
"height_meter": 1.8288,
"weight_kilogram": 90.7185,
"max_heart_rate": 200
}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:
{
"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 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:
[
{
"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 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:
{
"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 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 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:
[
{
"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 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:
{
"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 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,或尚未同步最近议题。