Skip to content

Repository files navigation

Withings.NET

.NET client for the Withings Health Data API (OAuth 2.0).

NuGet

Requirements

  • .NET 8.0, 9.0, or 10.0

Installation

dotnet add package Withings.NET

Quick Start

1. Configure Credentials

varcredentials=newWithingsCredentials();credentials.SetClientProperties("your_client_id","your_client_secret");credentials.SetCallbackUrl("http://localhost:8585/callback");

2. OAuth 2.0 Authorization

varauthenticator=newAuthenticator(credentials);// Generate the authorization URL and redirect the uservarurl=authenticator.GetAuthCodeUrl("user.info,user.metrics,user.activity",state);// After the user authorizes, exchange the code for tokensvartoken=awaitauthenticator.GetAccessToken(authorizationCode);// Refresh tokens when they expirevarnewToken=awaitauthenticator.RefreshAccessToken(token.RefreshToken);

3. Fetch Health Data

varclient=newWithingsClient(credentials);// Activity measuresvaractivity=awaitclient.GetActivityMeasures(startDate,endDate,userId,accessToken);// Body measuresvarbody=awaitclient.GetBodyMeasures(userId,startDate,endDate,accessToken);// Sleep summaryvarsleep=awaitclient.GetSleepSummary("2024-01-01","2024-01-31",accessToken);// Sleep measuresvarsleepData=awaitclient.GetSleepMeasures(userId,startDate,endDate,accessToken);// Workoutsvarworkouts=awaitclient.GetWorkouts("2024-01-01","2024-01-31",accessToken);// Intraday activityvarintraday=awaitclient.GetIntraDayActivity(userId,startDate,endDate,accessToken);// HeartvarheartList=awaitclient.GetHeartList(startDate,endDate,accessToken);varrecording=awaitclient.GetHeartRecording(signalId,accessToken);// Uservardevices=awaitclient.GetDevices(accessToken);vargoals=awaitclient.GetGoals(accessToken);// Webhook subscriptionsawaitclient.Subscribe(callbackUrl,appli,accessToken);varsubscriptions=awaitclient.ListSubscriptions(appli,accessToken);awaitclient.RevokeSubscription(callbackUrl,appli,accessToken);

Development

Running Unit Tests

dotnet test --filter "TestCategory!=E2E"

Running E2E Tests

E2E tests run against the live Withings API. You need to bootstrap OAuth tokens first.

  1. Create a .env file in the project root:
WITHINGS_CLIENT_ID=your_client_id
WITHINGS_CLIENT_SECRET=your_client_secret
WITHINGS_CALLBACK_URL=http://localhost:8585/callback
WITHINGS_REFRESH_TOKEN=
WITHINGS_USER_ID=
  1. Run the bootstrap script to obtain tokens:
./scripts/bootstrap-token.sh
  1. Run E2E tests:
dotnet test --filter "TestCategory=E2E" -f net10.0

The E2E test suite automatically saves new refresh tokens back to .env after each run (Withings refresh tokens are single-use).

API Reference

ClassDescription
AuthenticatorOAuth 2.0 authorization and token management
WithingsClientAPI client for health data endpoints
OAuthTokenToken response with access token, refresh token, and user ID
WithingsApiExceptionException thrown for non-zero API status codes

License

See LICENSE for details.

About

Wrapper around the Withings Health Data api.

Topics

Resources

Stars

5 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages