Skip to content

TypeScript client

@rightful/client wraps the /v1 API with types. It has no runtime dependencies: it uses the global fetch (Node 18 or later), and ships as ESM with type definitions.

It is not on the npm registry yet. Ask us for a copy.

import { RightfulClient, RightfulError } from '@rightful/client';
const rightful = new RightfulClient({
apiKey: process.env.RIGHTFUL_KEY!, // rk_test_… or rk_live_…
baseUrl: 'https://api.rightful.id',
});
const session = await rightful.createSession(
{
workflow: 'onboarding-id',
namespace: 'customers',
subject_ref: 'user_42',
return_url: 'https://app.example.com/verified',
},
{ idempotencyKey: 'onboarding:user_42:1' },
);
// Store session.id, then send the person to session.url (returned only here).
// Later, when they come back, or from a background job:
const s = await rightful.getSession(session.id);
if (s.status === 'completed') {
// s.decision is 'approved' | 'declined' | 'review'. Act on it, not on s.declared_decision.
}
// Or block until it finishes (in a job, not a request handler):
const done = await rightful.waitForCompletion(session.id, { intervalMs: 3000, timeoutMs: 20 * 60_000 });

Results are polled, or a webhook says when to fetch them (see below).

Method Endpoint
createSession(params, { idempotencyKey }) POST /v1/sessions
getSession(id) GET /v1/sessions/{id}
listSessions({ namespace, subject_ref, status, limit, created_after }) GET /v1/sessions
cancelSession(id) POST /v1/sessions/{id}/cancel
deleteSession(id) DELETE /v1/sessions/{id}
deleteSubject(namespace, subjectRef) DELETE /v1/subjects/{namespace}/{subject_ref}
waitForCompletion(id, { intervalMs, timeoutMs, signal }) Polls getSession until completed, expired or cancelled
putWorkflow(key, definition, { activate }) PUT /v1/workflows/{key}
validateWorkflow(definition) POST /v1/workflows/validate (the definition must include key)
getWorkflow(key, { version }), listWorkflows() GET /v1/workflows[/{key}]
activateWorkflow(key, version) POST /v1/workflows/{key}/activate
getTheme(), putTheme(theme) GET / PUT /v1/theme
compareFaces({ reference, live }) POST /v1/face/compare
verifyVoice({ reference_session, track_id?, segment_label? }, wav, { idempotencyKey? }) POST /v1/voice/verify
usage({ from, to, group_by }) GET /v1/usage

Every method also takes an optional { signal } (AbortSignal).

The client does not wrap the review decision or evidence endpoints yet. Call POST /v1/sessions/{id}/review and GET /v1/sessions/{id}/evidence[/{evidence_id}] directly; see the API reference.

verifyWebhook({ secret, header, body }) checks a Rightful-Signature header against the raw body (5-minute tolerance by default; toleranceSeconds to change it). constructWebhookEvent(...) does the same and returns the parsed event, or throws WebhookVerificationError. Both use Web Crypto, so they work in Node 18+ and edge runtimes. See Webhooks.

Every failure is a RightfulError with:

  • code: the API’s code, such as SESSION_NOT_FOUND, IDEMPOTENCY_KEY_REUSED or WORKFLOW_INVALID;
  • status: the HTTP status;
  • detail: from the response body;
  • issues: the field errors, for REQUEST_INVALID, WORKFLOW_INVALID and THEME_INVALID.

The client adds its own codes: HTTP_<status> for a response without a code, NETWORK_ERROR (status 0, original error in cause), INVALID_RESPONSE for a success response that is not JSON, and WAIT_TIMEOUT from waitForCompletion. An aborted signal rejects with the signal’s own reason.

try {
await rightful.putWorkflow('onboarding-id', definition);
} catch (e) {
if (e instanceof RightfulError && e.code === 'WORKFLOW_INVALID') console.error(e.issues);
else throw e;
}

The package is ESM-only. On Node 22.12+ or 20.19+, require() of ES modules works without a flag. On older Node, use a dynamic await import('@rightful/client'), compiled with module: node16 or nodenext.