# Node SDK

Source: https://www.flexpa.com/docs/records/node-sdk
Description: The Flexpa Node SDK: a TypeScript library for OAuth 2.0, token management and FHIR resource operations with the Flexpa API.

The Node SDK is a TypeScript library for interacting with Flexpa API. It handles OAuth 2.0 authentication, token management, FHIR resource operations, and SMART Health Links.

## Installation

Requires Node.js 19+ (uses the global Web Crypto API).

*Install*

```bash
npm install @flexpa/node-sdk
```

```bash
yarn add @flexpa/node-sdk
```

```bash
pnpm add @flexpa/node-sdk
```

```bash
bun add @flexpa/node-sdk
```

```typescript
import FlexpaClient from '@flexpa/node-sdk';
```

## Initialize

Choose an initialization method based on your use case:

- **[fromBearerToken](#frombearertoken)** — You already have an access token
- **[fromAuthorizationCode](#fromauthorizationcode)** — Public clients (mobile apps, SPAs) using OAuth PKCE
- **[fromClientCredentials](#fromclientcredentials)** — Server-to-server without patient context

### fromBearerToken

Initialize from an existing [access token](https://www.flexpa.com/docs/records#access-token).

**Parameters**

- `bearerToken` (string):
Access token

```typescript
const flexpaClient = FlexpaClient.fromBearerToken('your-access-token');
```

---

### fromAuthorizationCode

Exchange an OAuth 2.0 authorization code from the [PKCE flow](#oauth-pkce-utilities).

**Parameters**

- `code` (string):
Authorization code received from the OAuth redirect

- `codeVerifier` (string):
The original code verifier used to generate the code challenge (43-128 characters)

- `redirectUri` (string):
Must match the redirect URI used in the authorization request

- `publishableKey` (string):
Publishable Key. See [API Keys](https://www.flexpa.com/docs/records#api-keys).

```typescript
// In your callback handler, after user authorizes
const flexpaClient = await FlexpaClient.fromAuthorizationCode(
  code,               // From URL query params
  codeVerifier,       // Retrieved from secure storage
  'https://your-app.com/callback',
  'pk_live_...'
);

// Store the token for future API calls
const accessToken = flexpaClient.getAccessToken();
```

---

### fromClientCredentials

Obtain an [application access token](https://www.flexpa.com/docs/records#application-access-token) for server-to-server calls without patient context. Tokens are valid for 30 minutes.

Test and live mode keys can mint application tokens for SMART Health Links and REST consent management. Each token is restricted to its application's mode. Application-token access to FHIR resources requires live mode.

**Parameters**

- `publishableKey` (string):
Publishable Key. See [API Keys](https://www.flexpa.com/docs/records#api-keys).

- `secretKey` (string):
Secret Key. See [API Keys](https://www.flexpa.com/docs/records#api-keys).

```typescript
const flexpaClient = await FlexpaClient.fromClientCredentials(
  'pk_live_...',
  'sk_live_...'
);
```

## OAuth PKCE Utilities

The SDK provides helper functions to implement the OAuth 2.0 PKCE flow (RFC 7636).

### generateCodeVerifier

Generate a cryptographically random code verifier for PKCE.
This is an async function that resolves to a 43-character URL-safe random string, so it must be awaited.

**Note:**
Store the code verifier securely on the client. You'll need it to exchange the authorization code for tokens.

```typescript
const codeVerifier = await FlexpaClient.generateCodeVerifier();
// Example: "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk"
```

---

### generateCodeChallenge

Generate a code challenge from a code verifier using the S256 method. This is an async function and returns a `Promise<string>`, so it must be awaited.

**Parameters**

- `codeVerifier` (string):
The code verifier to hash

```typescript
const codeVerifier = await FlexpaClient.generateCodeVerifier();
const codeChallenge = await FlexpaClient.generateCodeChallenge(codeVerifier);
// Example: "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM"
```

---

### buildAuthorizationUrl

Build the authorization URL for initiating the OAuth 2.0 PKCE flow.
Open this URL in a browser to start patient authorization.

**Parameters**

- `publishableKey` (string):
Your publishable key (client_id)

- `redirectUri` (string):
Must match a registered redirect URI for your application

- `codeChallenge` (string):
The S256 code challenge generated from your code verifier

- `externalId` (string):
Your application's unique identifier for this user. Enables tracking authorizations and correlating patient data across sessions.

- `scope` (string[]):
OAuth scopes (default: `['launch/patient']`). Include `'offline_access'` to request [MULTIPLE usage](https://www.flexpa.com/docs/guides/usage).

- `state` (string):
Optional state parameter for CSRF protection

- `endpointId` (string):
Optional endpoint ID to pre-select a directory endpoint. Cannot be combined with `flow: { type: 'ial2' }`.

- `lineOfBusiness` (LineOfBusiness):
The patient's [line of business](https://www.flexpa.com/docs/consent#build-authorization-url), to skip the line of business selection screen: `'EMPLOYER'`, `'OFF_EXCHANGE'`, `'MEDICARE_ADV'`, `'ON_EXCHANGE'`, or `'MEDICAID'`. Requires `endpointId`. Emits `flexpa_line_of_business`.

- `resume` (boolean):
Set to `true` to resume an in-progress consent workflow. Emits `flexpa_resume=true`.

- `flow` (AuthorizationFlow):
Opt into a non-default flow. Discriminated union — either `{ type: 'ial2' }` for TEFCA / IAL2 identity-verified flow, or `{ type: 'search', hints?: { firstName?, lastName?, dob? } }` for the search-mode patient match flow. Omit for the standard payer/provider flow.

```typescript
// 1. Generate PKCE credentials (both helpers are async)
const codeVerifier = await FlexpaClient.generateCodeVerifier();
const codeChallenge = await FlexpaClient.generateCodeChallenge(codeVerifier);

// 2. Build the authorization URL
const authUrl = FlexpaClient.buildAuthorizationUrl({
  publishableKey: 'pk_live_...',
  redirectUri: 'https://your-app.com/callback',
  codeChallenge,
  externalId: 'user-123',
});

// 3. Store codeVerifier securely, then redirect user to authUrl
```

```typescript
const authUrl = FlexpaClient.buildAuthorizationUrl({
  publishableKey: 'pk_live_...',
  redirectUri: 'https://your-app.com/callback',
  codeChallenge,
  externalId: 'user-123',
  flow: { type: 'ial2' },
});
```

## Token API

### introspect

Retrieve token metadata including sync status, patient ID, and available resources. See the [Introspect API reference](https://www.flexpa.com/docs/records#introspect) for the full response schema.

```typescript
const flexpaClient = FlexpaClient.fromBearerToken('your-access-token');
const tokenDetails = await flexpaClient.introspect();
```

---

### tokenRefresh

[Refresh](https://www.flexpa.com/docs/records#token-refresh) the access token to extend access to patient data.
Only available for `MULTIPLE` [usage](https://www.flexpa.com/docs/guides/usage) authorizations.

Returns a new access token. You should update your stored token with the returned value.

**Parameters**

- `publishableKey` (string):
Publishable Key

- `secretKey` (string):
Secret Key

```typescript
const flexpaClient = FlexpaClient.fromBearerToken('your-access-token');
const { access_token } = await flexpaClient.tokenRefresh(
  'pk_live_...',
  'sk_live_...'
);
// Store the new access_token for future requests
```

---

### revokeToken

[Revoke](https://www.flexpa.com/docs/consent#revoke) the consent with an OAuth 2.0 token revocation request ([RFC 7009](https://www.rfc-editor.org/rfc/rfc7009)), authenticating with your publishable key and secret key in a Basic header. It sends the refresh token, or the access token when the client has no refresh token. Available in `@flexpa/node-sdk` 2.4.0.

**Parameters**

- `publishableKey` (string):
Publishable Key

- `secretKey` (string):
Secret Key

- `patientAuthorizationIds` (string[]):
Revoke only these patient authorizations of the consent. The consent is not revoked, so its tokens keep working and return data from its other patient authorizations. Omit to revoke the whole consent. An empty array throws an error without sending a request.

```typescript
const flexpaClient = FlexpaClient.fromBearerToken('your-access-token', undefined, 'your-refresh-token');
const { count } = await flexpaClient.revokeToken('pk_live_...', 'sk_live_...');
```

```typescript
await flexpaClient.revokeToken('pk_live_...', 'sk_live_...', ['3c9a5f0e-6b1d-4b8e-9f2a-1d7c5e8a4b20']);
```

## SMART Health Links

These methods are available in `@flexpa/node-sdk` 2.1.0. Use an application-token client for server-to-server [SHL management](https://www.flexpa.com/docs/smart-health-links#authenticated-api):

```typescript
const client = await FlexpaClient.fromClientCredentials('pk_test_...', 'sk_test_...');
```

Patient tokens and legacy secret keys also work through `FlexpaClient.fromBearerToken(credential)`. All requests are scoped to the credential's application and mode; patient tokens can manage only their own user's links. New server integrations should use application tokens.

### createSmartHealthLink

Create an encrypted link and receive its `id`, `shl`, `viewerUrl`, `manifestUrl`, and `expiresAt`. The exported `CreateSmartHealthLinkOptions` type describes the [creation options](https://www.flexpa.com/docs/smart-health-links#create-shl).

```typescript
const link = await client.createSmartHealthLink({
  files: [{
    contentType: 'application/fhir+json',
    content: { resourceType: 'Bundle', type: 'collection', entry: [] },
  }],
  user: { externalId: 'user-123' },
  ttl: 3600,
});
```

`ttl` is in seconds and required unless `longTerm: true`. Direct transfer requires exactly one file and excludes a passcode. Application tokens and secret keys require `user.externalId`; patient tokens derive the user from their authorization.

### listSmartHealthLinks

Returns `ListSmartHealthLinksResponse`: a `data` array of link metadata and successful-access summaries (`accessCount`, `lastAccessedAt`), plus `meta.hasMore` and `meta.nextCursor`. The optional `limit` defaults to 20 and accepts 1–1000.

```typescript
const page = await client.listSmartHealthLinks({ limit: 20 });
if (page.meta.nextCursor) {
  const nextPage = await client.listSmartHealthLinks({
    limit: 20,
    cursor: page.meta.nextCursor,
  });
}
```

### listSmartHealthLinkAccesses

Returns a typed FHIR Bundle of AuditEvents, including successful and denied access attempts. Entries retain their `fullUrl` identifiers. The optional `limit` defaults to 20 and accepts 1–100.

Filter `status` by `SUCCEEDED`, `PASSCODE_REQUIRED`, `INVALID_PASSCODE`, `LOCKED`, `EXPIRED`, or `REVOKED`, and `type` by `MANIFEST` or `DIRECT`.

```typescript
const filters = { status: 'SUCCEEDED', type: 'MANIFEST', limit: 20 } as const;
const audit = await client.listSmartHealthLinkAccesses(link.id, filters);

const nextUrl = audit.link?.find((entry) => entry.relation === 'next')?.url;
const cursor = nextUrl ? new URL(nextUrl).searchParams.get('cursor') : null;
if (cursor) {
  const nextAudit = await client.listSmartHealthLinkAccesses(link.id, {
    ...filters,
    cursor,
  });
}
```

Pass the cursor back with the same filters. The SDK sends the request to its configured API URL; it does not fetch Bundle links automatically. Malformed cursors return `400` through `HttpError`.

### revokeSmartHealthLink

Revokes the link by ID. Resolves without a value after the API's `204` response.

```typescript
await client.revokeSmartHealthLink(link.id);
```

SHL management methods use the SDK's existing retry behavior: up to ten retries on `429`, respecting `Retry-After`. Other failures are returned immediately.

## FHIR API

FHIR methods include built-in retry logic for [429 responses](https://www.flexpa.com/docs/records#errors) — both the `transient` 429 returned while a patient's initial [sync](https://www.flexpa.com/docs/records#sync-job-states) is running and `throttled` [rate limits](https://www.flexpa.com/docs/records#rate-limits). Every method retries; only `read` and `search` accept retry options. The SDK does not implement [ViewDefinition $run](https://www.flexpa.com/docs/records#viewdefinition-run) — call that operation directly and handle its `429` responses yourself.

### getCapabilityStatement

Retrieve the FHIR [capability statement](https://www.flexpa.com/docs/records#capability-statement)

```typescript
const flexpaClient = FlexpaClient.fromBearerToken('your-access-token');
const capabilityStatement = await flexpaClient.getCapabilityStatement();
```

---

### read

[Read](https://www.flexpa.com/docs/records#read) a FHIR resource

**Parameters**

- `resourceType` (string):
Supported FHIR Resource

- `id` (string):
Resource ID

- `options` ({ numRetries?: number; delayMs?: number }):
Retry options object passed as the third argument. `numRetries` is the maximum number of retries (default: `10`); `delayMs` is the delay in milliseconds before the first retry (default: `1000`), doubling after each attempt. A `Retry-After` header on the response takes precedence and becomes the base for that doubling. With the defaults and no `Retry-After` — as with the `transient` sync 429 — the SDK waits about 17 minutes across 10 retries before throwing an [`HttpError`](#error-handling).

```typescript
const flexpaClient = FlexpaClient.fromBearerToken('your-access-token');
const patient = await flexpaClient.read('Patient', 'patient-id', { numRetries: 5, delayMs: 2000 });
```

---

### search

[Search](https://www.flexpa.com/docs/records#search) a FHIR resource

**Parameters**

- `resourceType` (string):
Supported FHIR Resource

- `searchParams` (Record):
FHIR search parameters as key-value pairs

- `options` ({ numRetries?: number; delayMs?: number }):
Optional retry options. `numRetries` — maximum number of retries (default: `10`); `delayMs` — delay in milliseconds before the first retry (default: `1000`), doubling after each attempt unless the response carries a `Retry-After` header, which takes precedence

```typescript
const flexpaClient = FlexpaClient.fromBearerToken('your-access-token');
const patients = await flexpaClient.search('Patient',
  { given: 'John' },
  { numRetries: 10, delayMs: 1000 }
);
const observations = await flexpaClient.search('Observation',
  { patient: 'patient-id' }
);
```

---

### Patient $everything

Retrieve all the resources for the [patient](https://www.flexpa.com/docs/records#patient-everything)

```typescript
const flexpaClient = FlexpaClient.fromBearerToken('your-access-token');
const patientData = await flexpaClient.$everything();
```

## Error handling

The Node SDK throws `HttpError` instances for API failures. These errors include the full HTTP response for detailed inspection.

**Common Error Codes:**
- `429` - Data still processing (see [sync jobs](https://www.flexpa.com/docs/records#sync-job-states))
- `422` - Processing error during sync
- `401` - Invalid credentials
- `404` - Resource not found

```typescript
import { HttpError } from '@flexpa/node-sdk';

try {
  const patient = await flexpaClient.read('Patient', 'invalid-id');
} catch (error) {
  if (error instanceof HttpError) {
    console.log('Status:', error.response.status);

    // Parse error details
    const errorData = await error.json();
    console.log('Error details:', errorData);
  }
}
```

## Utilities

### getAccessToken

Retrieve the access token from the client for storage or use with other HTTP clients.

```typescript
const flexpaClient = await FlexpaClient.fromAuthorizationCode(...);

// Store for later use with fromBearerToken
const accessToken = flexpaClient.getAccessToken();
```
