Prerequisites
- Approved Humanity app with
client_id, optionalclient_secret, and a registeredredirect_uri. - Access to the SDK package published as
@humanity-org/connect-sdk. - Somewhere secure (session storage, KV store, Redis, etc.) to persist the PKCE
code_verifier, OAuth tokens, and any cursors returned by feed helpers.
Example 1 — Kick off OAuth with PKCE
UsebuildAuthUrl anywhere you can redirect the user (Next.js Route Handler shown here). Humanity issues the scopes that correspond to the presets you plan to evaluate. Persist the codeVerifier server-side for five minutes max.
/oauth/authorize URL for the environment, and uses the OAuth scopes (e.g. identity:read, identity:date_of_birth) that determine which presets your app can access.
Example 2 — Exchange the code and persist the session
Once Humanity redirects back to yourredirect_uri, grab the authorization code and the stored code_verifier and trade them for tokens. The helper returns a typed payload that already includes granted preset keys.
- Refresh tokens are only returned for confidential clients—guard the storage location accordingly.
token.rateLimit(if present) mirrors theX-RateLimit-*headers for your own observability.
Example 3 — Gate features by presets
verifyPreset and verifyPresets hydrate evidence payloads and map rate-limit metadata for you. Use them immediately after sign-in or every time the user attempts to unlock a sensitive action.
- Maximum of 10 presets per batch request; the helper enforces this before hitting the API.
verification.resultsincludes evidence metadata, timestamps, and rate-limit context for audit logging, whileverification.errorscaptures API-side failures.
Example 4 — Poll credential and authorization feeds
Feeds expose incremental changes so you can keep downstream systems synchronized. Store thecursor (or updatedSince) and pass it back to pick up where you left off.
- The SDK validates
limit(1–100) so you fail fast instead of receiving a 4xx from the API. - Both helpers surface
rateLimitdata; honorremainingandresetto avoid throttling.
Example 5 — Proactively revoke access
Revoke a single token, a batch of refresh tokens, or every authorization tied to a user. Setcascade: true to clear refresh tokens that belong to the same authorization automatically.
tokens: string[] to revoke multiple credentials in one request or supply an authorizationId to wipe an entire consent grant.
Example 6 — Leverage discovery + health endpoints
When you boot your app, call discovery once to warm up the preset registry and know which scopes are currently available. Usehealthcheck/readiness for alerting.
getConfiguration(true)forces a refresh if you suspect the cache is stale.healthcheckis a lightweight liveness probe;readinessasserts dependencies such as storage and preset registries.
Example 7 — Generate JWTs with your secret key
After validating a Humanity access token, issue your own application JWT. This decouples your session management from Humanity tokens.Example 8 — Decode JWT payloads
Extract claims from any JWT without verification (useful for reading Humanity tokens after they’ve been validated):Example 9 — Refresh Humanity tokens
Refresh tokens before they expire:Example 10 — PKCE state and nonce verification
The SDK provides static helpers for verifying OAuth security parameters:Example 11 — Server-to-server token acquisition
Get tokens for users who have already authorized your app without a browser:This is useful for background jobs, webhooks, and server-to-server scenarios where you need to verify presets without a browser session.
Example 12 — Fetch user profile via preset
Get user profile information using thehumanity_user preset:
Example 13 — Query Engine for declarative checks
Use the Query Engine for complex eligibility checks without hardcoding preset names:Dropping down to the generated client
All helpers ultimately call the fully generated REST client exposed assdk.client. Reach for it when you need a controller method that does not yet have a convenience wrapper.