SDK Recipes

Task-based playbooks built on top of @humanity-org/sdk.

This tab collects prescriptive walkthroughs that tie the Humanity SDK to everyday integration tasks. Each recipe is copy/pasteable, highlights the minimum required inputs, and points to the underlying API surface when you need to drop down a level.

Prerequisites

  • Approved Humanity app with client_id, optional client_secret, and a registered redirect_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.

import { HumanitySDK } from '@humanity-org/connect-sdk';

const sdk = new HumanitySDK({
  clientId: process.env.HUMANITY_CLIENT_ID!,
  redirectUri: process.env.HUMANITY_REDIRECT_URI!,
  environment: process.env.HUMANITY_ENVIRONMENT ?? 'sandbox',
  clientSecret: process.env.HUMANITY_CLIENT_SECRET, // omit for public clients
});

Example 1 — Kick off OAuth with PKCE

Use buildAuthUrl 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.

Behind the scenes: the helper generates a PKCE pair, resolves the correct /oauth/authorize URL for the environment, and flattens friendly preset keys (e.g. is_human) into the literal scopes (presets:is_human.read).

Example 2 — Exchange the code and persist the session

Once Humanity redirects back to your redirect_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 the X-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.

Notes:

  • Maximum of 10 presets per batch request; the helper enforces this before hitting the API.

  • verification.results includes evidence metadata, timestamps, and rate-limit context for audit logging, while verification.errors captures API-side failures.

Example 4 — Poll credential and authorization feeds

Feeds expose incremental changes so you can keep downstream systems synchronized. Store the cursor (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 rateLimit data; honor remaining and reset to avoid throttling.

Example 5 — Proactively revoke access

Revoke a single token, a batch of refresh tokens, or every authorization tied to a user. Set cascade: true to clear refresh tokens that belong to the same authorization automatically.

You can also pass 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. Use healthcheck/readiness for alerting.

  • getConfiguration(true) forces a refresh if you suspect the cache is stale.

  • healthcheck is a lightweight liveness probe; readiness asserts 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.

To verify your JWT later:

Example 8 — Decode JWT payloads

Extract claims from any JWT without verification (useful for reading Humanity tokens after they've been validated):

<Warning> Always verify tokens before trusting their contents. Use decodeJwtPayload only to read claims from tokens already validated by the Humanity API. </Warning>

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 the humanity_user preset:

Dropping down to the generated client

All helpers ultimately call the fully generated REST client exposed as sdk.client. Reach for it when you need a controller method that does not yet have a convenience wrapper.

This keeps the SDK future-proof: new API routes appear in the client immediately after pulling the latest release, while higher-level adapters can be layered on as needed.

Last updated