# OAuth reference

Source: https://www.flexpa.com/docs/consent
Description: Flexpa Consent enables secure patient authorization for Records retrieval using SMART on FHIR with OAuth 2.0 PKCE.

## Consent

Flexpa Consent enables secure patient authorization for [Records](https://www.flexpa.com/docs/records) retrieval using [SMART on FHIR](http://hl7.org/fhir/smart-app-launch/) with OAuth 2.0 PKCE.

Consent can be used in web and mobile apps.

### How it works

1. Your app redirects users to Flexpa's authorization endpoint
2. Users connect their healthcare records from our [3-in-1 network](https://www.flexpa.com/docs/network)
3. Flexpa redirects back to your app with an authorization code
4. You exchange the authorization code for an access token and start making [Records](https://www.flexpa.com/docs/records) requests

### What you need

- A pair of [API Keys](https://www.flexpa.com/docs/guides/quickstart#api-keys) from the [Flexpa Portal](https://portal.flexpa.com)
- A **redirect URI** registered in the [Flexpa Portal](https://portal.flexpa.com). This is where users will be redirected after authorization.
- Optional: The [@flexpa/node-sdk](https://www.flexpa.com/docs/records/node-sdk) package installed in your project

*Image: Flexpa Consent*

#### Looking for a demo?

## Getting started

The SMART on FHIR authorization flow involves these main steps:

1. [Generate PKCE](#generate-pkce) - Create a code verifier and challenge
2. [Build authorization URL](#build-authorization-url) - Construct the URL with your parameters
3. [Redirect](#redirect) - Send the user to Flexpa for authorization
4. [Handle callback](#handle-callback) - Process the authorization code on redirect
5. [Exchange](#exchange) - Exchange the code for access tokens
6. [Fetch records](#fetch-records) - Use the access token to retrieve patient data

Let's detail each of these steps below.

**Note:**
The examples below show raw HTTP/JavaScript implementations. For convenience helpers, install the Node SDK:

*Install*

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

---

### Generate PKCE

PKCE (Proof Key for Code Exchange, pronounced "pixie") is an extension to OAuth 2.0 ([RFC 7636](https://www.rfc-editor.org/rfc/rfc7636)) that prevents authorization code interception attacks. It ensures the application that starts the authorization flow is the same one that finishes it.

You'll generate two values:

- **Code Verifier**: A cryptographically random string (43-128 characters) that you keep secret and store until the callback
- **Code Challenge**: A SHA-256 hash of the verifier, base64url-encoded, sent with the authorization request

The security works because hashing is one-way: even if an attacker intercepts the authorization code and challenge, they cannot reverse the hash to obtain the verifier needed to exchange the code for tokens.

For web apps, store the verifier in `sessionStorage`—it survives redirects within the same tab and is automatically cleared when the tab closes.

*Generate PKCE*

```bash
# Generate code verifier (43-128 character base64url string)
CODE_VERIFIER=$(openssl rand -base64 32 | tr -d '=' | tr '/+' '_-')

# Generate code challenge: BASE64URL(SHA256(verifier))
CODE_CHALLENGE=$(echo -n "$CODE_VERIFIER" | \
  openssl sha256 -binary | base64 | tr -d '=' | tr '/+' '_-')

echo "Code Verifier: $CODE_VERIFIER"
echo "Code Challenge: $CODE_CHALLENGE"
```

```ts
// Generate a cryptographically random code verifier
function generateCodeVerifier(): string {
  const array = new Uint8Array(32);
  crypto.getRandomValues(array);
  return btoa(String.fromCharCode(...array))
    .replace(/\+/g, '-')
    .replace(/\//g, '_')
    .replace(/=+$/, '');
}

// Generate code challenge: BASE64URL(SHA256(verifier))
async function generateCodeChallenge(verifier: string): Promise<string> {
  const encoder = new TextEncoder();
  const data = encoder.encode(verifier);
  const hash = await crypto.subtle.digest('SHA-256', data);
  return btoa(String.fromCharCode(...new Uint8Array(hash)))
    .replace(/\+/g, '-')
    .replace(/\//g, '_')
    .replace(/=+$/, '');
}

// Generate and store PKCE parameters
const codeVerifier = generateCodeVerifier();
const codeChallenge = await generateCodeChallenge(codeVerifier);
sessionStorage.setItem('flexpa_code_verifier', codeVerifier);
```

```python
import secrets
import hashlib
import base64

# Generate code verifier (43-128 character base64url string)
code_verifier = secrets.token_urlsafe(32)

# Generate code challenge: BASE64URL(SHA256(verifier))
digest = hashlib.sha256(code_verifier.encode()).digest()
code_challenge = base64.urlsafe_b64encode(digest).rstrip(b'=').decode()
```

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

// Generate PKCE parameters
const codeVerifier = await FlexpaClient.generateCodeVerifier();
const codeChallenge = await FlexpaClient.generateCodeChallenge(codeVerifier);

// Store the verifier in your user session store for the callback
```

### Build authorization URL

Construct the authorization URL with your OAuth parameters.

**Parameters**

- `client_id` (string):
Your publishable key from the [Flexpa Portal](https://portal.flexpa.com).

- `redirect_uri` (string):
The URL where users will be redirected after authorization. Must be registered in the [Flexpa Portal](https://portal.flexpa.com).

- `response_type` (string):
Must be `code` for the authorization code flow.

- `code_challenge` (string):
The PKCE code challenge generated from the code verifier (43 base64url characters).

- `code_challenge_method` (string):
Must be `S256` (SHA-256).

- `scope` (string):
Space-separated OAuth scopes. Must include `launch/patient`. Add `offline_access` for refresh tokens enabling [multiple usage](https://www.flexpa.com/docs/guides/usage).

- `flexpa_external_id` (string):
Your application's user identifier for this patient. Used for tracking and correlating authorizations across your system. If omitted, the authorization request is rejected with `error=invalid_request` and `error_description` of `flexpa_external_id is required`.

- `state` (string):
An opaque value used to maintain state between the request and callback. This value will be returned in the redirect. Recommended for CSRF protection.

- `ui_locales` (string):
The standard [OpenID Connect Core 1.0](https://openid.net/specs/openid-connect-core-1_0.html#AuthRequest) parameter: the patient's preferred languages for the interface, as a space-separated list of BCP 47 tags ordered by preference.

Flexpa Consent currently ships `en` (English) and `es` (Spanish). Flexpa takes the first tag in your list that it ships and skips the rest, so `ui_locales=fr-CA es en` opens in Spanish. A region subtag matches on its primary subtag, so `es-419` and `es-MX` both resolve to `es`.

When omitted, Flexpa reads the patient's browser `Accept-Language` header, and falls back to English when that names no supported language.

- `flexpa_endpoint_id` (string):
Pre-select a specific endpoint (health plan) for the user. Skips the endpoint selection screen. Get endpoint IDs from the [Directory](https://www.flexpa.com/docs/network/directory). Cannot be set at the same time as `flexpa_ial2_mode`.

- `flexpa_line_of_business` (string):
The patient's line of business (insurance plan type), to skip the line of business selection screen. Requires `flexpa_endpoint_id`, because only a health plan has a line of business. Flexpa validates that the endpoint supports the plan type. If it does not, including when the endpoint is a provider, the authorization request is rejected with `error=invalid_request` and `error_description` of `flexpa_line_of_business is not supported by this endpoint`. Patients whose plan has state-specific routing still see the state selection screen.

  - `EMPLOYER` (string):
For employer-sponsored health plans.

  - `OFF_EXCHANGE` (string):
For off-marketplace individual plans.

  - `MEDICARE_ADV` (string):
For Medicare Advantage plans.

  - `ON_EXCHANGE` (string):
For marketplace/ACA plans.

  - `MEDICAID` (string):
For Medicaid plans.

- `flexpa_ial2_mode` (string):
The only accepted value is `"true"`. When set, starts the IAL2 identity verification flow: patients verify their identity through CLEAR, ID.me, or Persona, and Flexpa searches [TEFCA](https://www.flexpa.com/docs/network#nationwide-exchanges) networks automatically. That search returns provider facilities, not health plans. Once it completes, the patient sees the facilities that were found and can add more endpoints (a health plan included) manually via search. Cannot be set at the same time as `flexpa_endpoint_id` or the demographic hints.

- `flexpa_search_mode` (string):
**Deprecated.** Accepted with any value and ignored. Every request that does not set `flexpa_ial2_mode` starts the search flow, where patients search by provider, facility, or health plan and can connect multiple sources in a single session. You can stop sending it.

- `flexpa_resume` (string):
Set to `true` when starting an authorization for a user who previously exited the consent flow mid-way (for example, after closing an identity-verification tab) so they can pick up where they left off instead of restarting. Omit the parameter to always start a new authorization.

**Note:**
A `ui_locales` naming only languages Flexpa does not ship does not fail the authorization — OpenID Connect asks that an unsupported locale not be an error. Flexpa falls back to where it looks when the parameter is absent: the patient's `Accept-Language`, then English. So `ui_locales=fr` from a browser asking for Spanish opens in Spanish.

A patient can change the language themselves from the picker at the bottom of any screen. That choice is remembered on their device and outranks this parameter on a later visit, so `ui_locales=en` will not pull a patient who chose Spanish back to English.

**Note:**
Resume is backed by a session cookie set when the user enters the consent flow. If that cookie is no longer present, or any request parameter differs from the original (`scope`, `redirect_uri`, `flexpa_endpoint_id`, etc.), Flexpa silently starts a new authorization — there is no error path to handle. `ui_locales` is the one exception: it changes nothing about what is being authorized, so changing it still resumes.

**Demographic hints**

- `flexpa_hint_first_name` (string):
First name hint for the patient. Cannot be used with `flexpa_ial2_mode`.

- `flexpa_hint_last_name` (string):
Last name hint for the patient. Cannot be used with `flexpa_ial2_mode`.

- `flexpa_hint_dob` (string):
Date of birth hint for the patient (YYYY-MM-DD format). Cannot be used with `flexpa_ial2_mode`.

- `flexpa_default_filter` (string):
Opens the search screen already filtered to one kind of organization. Accepted values are `PAYER` (health plans) and `PROVIDER` (hospitals, clinics, and other providers). Patients can still clear the filter or switch to the other one. When omitted, the search screen shows popular providers and popular health plans together.

**Note:**
Send all three hints, with the date of birth in `YYYY-MM-DD` format, and the patient skips the demographic collection screen. Anything less pre-fills the screen and still shows it — Flexpa needs a name and birth date either way, to anchor the [master Patient](https://www.flexpa.com/docs/guides/patient-linking) that links each connection under one consent.

**Note:**
On the first screen of Flexpa Consent, every patient agrees to Flexpa's Terms and Privacy policies. No authorization parameter skips it.

**Note:**
A request that does not set `flexpa_ial2_mode` starts the Flexpa Consent search flow. It never opens Flexpa Link.

*Build URL*

```bash
https://api.flexpa.com/oauth/authorize
  ?client_id=pk_test_...
  &redirect_uri=https://example.com/callback
  &response_type=code
  &code_challenge=$CODE_CHALLENGE
  &code_challenge_method=S256
  &scope=launch/patient
  &flexpa_external_id=usr_1234
```

```ts
const params = new URLSearchParams({
  client_id: 'pk_test_...',
  redirect_uri: 'https://example.com/callback',
  response_type: 'code',
  code_challenge: codeChallenge,
  code_challenge_method: 'S256',
  scope: 'launch/patient',
  flexpa_external_id: 'usr_1234',
});

const authUrl = `https://api.flexpa.com/oauth/authorize?${params}`;
```

```python
from urllib.parse import urlencode

params = {
    'client_id': 'pk_test_...',
    'redirect_uri': 'https://example.com/callback',
    'response_type': 'code',
    'code_challenge': code_challenge,
    'code_challenge_method': 'S256',
    'scope': 'launch/patient',
    'flexpa_external_id': 'usr_1234',
}

auth_url = f'https://api.flexpa.com/oauth/authorize?{urlencode(params)}'
```

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

const authUrl = FlexpaClient.buildAuthorizationUrl({
  publishableKey: 'pk_test_...',
  redirectUri: 'https://example.com/callback',
  codeChallenge,
  externalId: 'usr_1234',
  scope: ['launch/patient', 'offline_access'],
});
```

### Scopes

OAuth scopes control what data your application can access and for how long.

#### Supported scopes

- `launch/patient` (string):
Required scope that indicates patient context will be provided. This is necessary for all Flexpa integrations.

- `offline_access` (string):
Optional scope that requests [MULTIPLE usage](https://www.flexpa.com/docs/guides/usage) — Flexpa maintains the authorization with the endpoint and syncs fresh patient data on your application's configured [sync frequency](https://www.flexpa.com/docs/guides/usage#sync-frequency), without requiring the user to re-authorize.

A `refresh_token` is returned in the token response regardless of this scope. What the scope changes is whether Flexpa continues to sync fresh patient data from the endpoint.

**Note:**
Not all health plans support `offline_access`. Where an endpoint can't maintain the authorization, the authorization still succeeds and you still receive a `refresh_token`, but Flexpa won't sync fresh patient data from that endpoint. The data from the initial sync is available for 24 hours; after that, the user re-authorizes for new data. Refreshability is listed per endpoint in the [directory](https://www.flexpa.com/docs/network/directory).

### Redirect

Once you have the authorization URL, redirect the user to begin the consent flow. Users will:

1. Connect their health records
2. Consent to share their data with your application
3. Be redirected back to your `redirect_uri` with an authorization code

### Handle callback

After authorization, Flexpa redirects back to your `redirect_uri` with query parameters.

Parse the `code` parameter from the URL, retrieve the stored code verifier, and exchange them for tokens.

**Success parameters**

- `code` (string):
The authorization code to exchange for an access token. Valid for 30 minutes.

- `state` (string):
The state parameter you provided (if any). Verify this matches your stored state for CSRF protection.

**Error parameters**

- `error` (string):
One of the [error codes](#error-codes): `access_denied` or `server_error`

- `error_description` (string):
Human-readable error description providing additional detail

- `error_uri` (string):
Link to error code documentation. Included only on terminal/server error redirects (e.g. `server_error`, or an abandoned/expired session). It is **not** present when the patient cancels the flow or no matching records are found.

- `state` (string):
The state parameter you provided (if any). Always returned when present in the original request.

**Warning:**
Always validate the state parameter matches what you stored to prevent CSRF attacks.

```bash
https://example.com/callback?code=abc123&state=xyz789
```

```bash
https://example.com/callback
  ?error=access_denied
  &error_description=User+abandoned+consent
  &error_uri=https://flexpa.com/docs/consent%23error-codes
  &state=xyz789
```

### Error codes

When consent fails, Flexpa redirects back to your `redirect_uri` with standardized error parameters aligned with [RFC 6749](https://www.rfc-editor.org/rfc/rfc6749#section-4.1.2.1) where applicable.

**Error codes**

- `access_denied` (string):
The user did not complete authorization. This includes abandoning the flow, closing the modal, navigating away, or the session timing out. The `error_description` parameter explains the nature of the error:

  - `User cancelled the consent flow`
  - `No records found for this patient` — identity was verified but no matching records were found

- `server_error` (string):
An internal error prevented authorization. This includes initialization failures, no facilities authorized, or authorization code creation failures.

All error redirects include `error` and, when available, `error_description` (human-readable detail) and `state` (echoed back when provided in the original request). The `error_uri` parameter (a link to this section) is included only on server-driven terminal redirects (when the consent workflow reaches an `abandoned` or `expired` state, e.g. `server_error` or a session timeout). Redirects triggered by the patient explicitly leaving the flow — including `access_denied` with `User cancelled the consent flow` and the `No records found for this patient` case — include `error` and `error_description` but may omit `error_uri`.

**Note:**
The `state` parameter is always returned on error redirects when it was provided in the original request. Use this to correlate the error with the user's session and for CSRF validation.

```bash
https://example.com/callback
  ?error=access_denied
  &error_description=User+cancelled+the+consent+flow
  &state=xyz789
```

```bash
https://example.com/callback
  ?error=access_denied
  &error_description=No+records+found+for+this+patient
  &state=xyz789
```

```bash
https://example.com/callback
  ?error=server_error
  &error_description=No+facilities+...
  &error_uri=...
  &state=xyz789
```

### Exchange

Exchange the authorization code for an access token using the token endpoint.

This request:
- Validates the authorization code with Flexpa
- Verifies the PKCE code verifier
- Returns access and refresh tokens

**Note:**
See the [Token endpoint](#token) for full details on request and response fields.

*Exchange*

```bash
curl -X POST https://api.flexpa.com/oauth/token \
  -H "Content-Type: application/json" \
  -d '{
    "grant_type": "authorization_code",
    "client_id": "pk_test_...",
    "code": "the-authorization-code",
    "redirect_uri": "https://example.com/callback",
    "code_verifier": "the-code-verifier"
  }'
```

```ts
const response = await fetch('https://api.flexpa.com/oauth/token', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    grant_type: 'authorization_code',
    client_id: 'pk_test_...',
    code: authorizationCode,
    redirect_uri: 'https://example.com/callback',
    code_verifier: codeVerifier,
  }),
});

const { access_token, refresh_token, expires_in } = await response.json();
```

```python
import requests

response = requests.post(
    'https://api.flexpa.com/oauth/token',
    json={
        'grant_type': 'authorization_code',
        'client_id': 'pk_test_...',
        'code': authorization_code,
        'redirect_uri': 'https://example.com/callback',
        'code_verifier': code_verifier,
    },
)

data = response.json()
access_token = data['access_token']
refresh_token = data.get('refresh_token')
```

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

const client = await FlexpaClient.fromAuthorizationCode(
  authorizationCode,
  codeVerifier,
  redirectUri,
  process.env.FLEXPA_PUBLISHABLE_KEY!
);

const accessToken = client.getAccessToken();
```

### Fetch records

Use your access token to fetch patient data from the [FHIR API](https://www.flexpa.com/docs/records#fhir-api).

Use the Patient Access Token returned from the exchange step.

The `$everything` operation returns a Bundle containing all available resources for the patient—claims, coverage, conditions, medications, and more.

**Note:**
See the [Records documentation](https://www.flexpa.com/docs/records) for the full list of supported FHIR resources and operations.

*Request*

```bash
curl 'https://api.flexpa.com/fhir/Patient/$everything' \
  -H "Authorization: Bearer $ACCESS_TOKEN"
```

```js
const response = await fetch(
  'https://api.flexpa.com/fhir/Patient/$everything',
  {
    headers: {
      'Authorization': `Bearer ${accessToken}`,
    },
  }
);

const bundle = await response.json();
```

```python
import requests

response = requests.get(
    'https://api.flexpa.com/fhir/Patient/$everything',
    headers={'Authorization': f'Bearer {access_token}'},
)

bundle = response.json()
```

```ts
const bundle = await client.$everything();
```

## OAuth API

The OAuth API provides endpoints for authorization, token management, and introspection.

---

### Authorization
The authorization endpoint initiates the OAuth 2.0 PKCE flow. See [Build authorization URL](#build-authorization-url) for implementation details.

---

### Token

The token endpoint supports three grant types:

1. `authorization_code` - For exchanging an authorization code for access tokens (OAuth PKCE flow)
2. `refresh_token` - For refreshing Patient Access Tokens
3. `client_credentials` - For obtaining [Application Access Tokens](https://www.flexpa.com/docs/records#application-access-token)

The request body can be sent as JSON (`Content-Type: application/json`) or form-encoded (`Content-Type: application/x-www-form-urlencoded`, the OAuth 2.0 default). Both take the same fields, which is why the examples on this page use JSON and the [quickstart](https://www.flexpa.com/docs/guides/quickstart) uses form encoding.

**Request headers**

- `Authorization` (string):
Client identity can be provided in one of two ways:

  - **`client_id` in the request body** — for public clients (e.g. mobile apps) that cannot store a secret key. Supported for `authorization_code` and `refresh_token` grants.
  - **`Authorization: Basic` header** — for confidential clients with a secret key. Required for `client_credentials` grant, also supported for `refresh_token` grant. Concatenate your publishable key and secret key (from the [Flexpa Portal](https://portal.flexpa.com)) with a colon (`:`), then base64-encode the result.

You cannot provide both an `Authorization` header and `client_id` in the same request.

**Request body**

- `grant_type` (string):
The type of grant used to obtain the access token.

Valid values:
  - `authorization_code` - For exchanging an authorization code (PKCE)
  - `client_credentials` - For Application Access Tokens
  - `refresh_token` - For refreshing Patient Access Tokens

- `code` (string):
Required when `grant_type` is `authorization_code`.

The authorization code from the [callback](#handle-callback).

- `redirect_uri` (string):
Required when `grant_type` is `authorization_code`.

Must match the redirect URI used in the authorization request.

- `code_verifier` (string):
Required when `grant_type` is `authorization_code`.

The PKCE code verifier used to generate the code challenge.

- `client_id` (string):
Required when `grant_type` is `authorization_code`. Optional for `refresh_token` as an alternative to Basic Auth for public clients that cannot store a secret key.

Your publishable key.

- `refresh_token` (string):
Required when `grant_type` is `refresh_token`.

The `refresh_token` obtained from the [exchange step](#exchange).
Once you have used this token to refresh the `access_token`, it is no longer valid.

A new `refresh_token` will be issued upon successful token refresh.

**Note:**
When using `grant_type=refresh_token`, this endpoint accepts any patient authorization whose `refresh_token` is still valid (within `refresh_expires_in`, default 90 days) and whose consent has not been revoked. This applies to both `ONE_TIME` and `MULTIPLE` usage authorizations — refreshability is determined by the validity of the `refresh_token`, not by the usage value. Request the `offline_access` scope to create a `MULTIPLE` usage authorization.
See [patient authorization usage](https://www.flexpa.com/docs/guides/usage) for more information.

**Note:**
When using `grant_type=client_credentials`, the resulting application access token is **bound to the mode of
the API keys that minted it**: live mode keys mint tokens that see live data, test mode keys mint tokens that
see test data. REST endpoints scope their results to the token's mode; the [FHIR API](https://www.flexpa.com/docs/records) requires
live mode application tokens.

**Response fields**

- `access_token` (string):
The `access_token` to be used to make Flexpa API requests.

For Patient Access Tokens, this is associated with a specific patient.
For Application Access Tokens, this is associated with your application only.

- `expires_in` (number):
`expires_in` is the time (in seconds) for which the `access_token` is valid.

For Patient Access Tokens: 86400 seconds (24 hours).
For Application Access Tokens: 1800 seconds (30 minutes).

- `token_type` (string):
The type of token, always `Bearer`.

- `refresh_token` (string):
The `refresh_token` to be used to refresh the Patient Access Token.

Returned on every successful `authorization_code` exchange and on every successful `grant_type=refresh_token` refresh, regardless of the requested scopes or the [usage](https://www.flexpa.com/docs/guides/usage) of the authorization. Refreshing returns a new `access_token` valid for another 24 hours; it does not itself sync fresh patient data from the endpoint — that requires [`MULTIPLE` usage](https://www.flexpa.com/docs/guides/usage) via the `offline_access` scope.

- `refresh_expires_in` (number):
Returned alongside `refresh_token` for the `authorization_code` and `refresh_token` grants.

The time period for which the `refresh_token` is valid.
You must call this route to refresh the Patient Access Token before this time period elapses otherwise, the patient will need to re-authorize.

Defaults to 7776000 seconds (90 days).

*Refresh Token Grant Request (Confidential Client)*

```bash
PUBLIC_KEY=pk_test...
SECRET_KEY=sk_test...
REFRESH_TOKEN=flexpa-refresh-token

# Base64 encode the credentials
CREDENTIALS=$(echo -n "${PUBLIC_KEY}:${SECRET_KEY}" | base64)

curl -X POST https://api.flexpa.com/oauth/token \
  -H "Authorization: Basic ${CREDENTIALS}" \
  -H "Content-Type: application/json" \
  -d '{
    "grant_type": "refresh_token",
    "refresh_token": "'"${REFRESH_TOKEN}"'"
  }'
```

```js
const PUBLIC_KEY = 'pk_test...';
const SECRET_KEY = 'sk_test...';
const REFRESH_TOKEN = 'flexpa-refresh-token';

const base64Credentials = btoa(`${PUBLIC_KEY}:${SECRET_KEY}`);

const response = await fetch("https://api.flexpa.com/oauth/token", {
  method: "POST",
  headers: {
    "Authorization": `Basic ${base64Credentials}`,
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    grant_type: 'refresh_token',
    refresh_token: REFRESH_TOKEN
  })
});

const { access_token, expires_in, refresh_token } = await response.json();
```

*Refresh Token Grant Request (Public Client)*

```bash
PUBLIC_KEY=pk_test...
REFRESH_TOKEN=flexpa-refresh-token

curl -X POST https://api.flexpa.com/oauth/token \
  -H "Content-Type: application/json" \
  -d '{
    "grant_type": "refresh_token",
    "refresh_token": "'"${REFRESH_TOKEN}"'",
    "client_id": "'"${PUBLIC_KEY}"'"
  }'
```

```js
const PUBLIC_KEY = 'pk_test...';
const REFRESH_TOKEN = 'flexpa-refresh-token';

const response = await fetch("https://api.flexpa.com/oauth/token", {
  method: "POST",
  headers: {
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    grant_type: 'refresh_token',
    refresh_token: REFRESH_TOKEN,
    client_id: PUBLIC_KEY
  })
});

const { access_token, expires_in, refresh_token } = await response.json();
```

*Client Credentials Grant Request*

```bash
PUBLIC_KEY=pk_live...
SECRET_KEY=sk_live...

# Base64 encode the credentials
CREDENTIALS=$(echo -n "${PUBLIC_KEY}:${SECRET_KEY}" | base64)

curl -X POST https://api.flexpa.com/oauth/token \
  -H "Authorization: Basic ${CREDENTIALS}" \
  -H "Content-Type: application/json" \
  -d '{
    "grant_type": "client_credentials"
  }'
```

```js
const PUBLIC_KEY = 'pk_live...';
const SECRET_KEY = 'sk_live...';

const base64Credentials = btoa(`${PUBLIC_KEY}:${SECRET_KEY}`);

const response = await fetch("https://api.flexpa.com/oauth/token", {
  method: "POST",
  headers: {
    "Authorization": `Basic ${base64Credentials}`,
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    grant_type: 'client_credentials'
  })
});

const { access_token, expires_in } = await response.json();
```

---

### Introspect

Returns information about an access token, including the consent and endpoint details.

The `state`, `usage`, `endpoint`, `sync`, and `timeUntilMaxAuth` fields are deprecated and will be removed on February 1, 2027. They describe only the first patient authorization on the consent, so they can be misleading when a consent has more than one, such as a TEFCA consent. Read these fields per authorization from `patientAuthorizations` in the [Consent API](#consent-1) instead.

**Request headers**

- `Authorization` (string):
An `Authorization: Bearer` header value must be presented with a [Patient Access Token](https://www.flexpa.com/docs/records#patient-access-token)

**Response fields**

- `jti` (string):
A random nonce value that uniquely identifies the access token.

- `iat` (number):
When the access token was issued in Unix time (in seconds).

- `exp` (number):
When the access token expires in Unix time (in seconds).

- `active` (boolean):
A boolean that states whether the access token can be used.

- `sub` (string):
A unique identifier for the consent backing this access token.

- `patient` (string):
The Patient ID connected to the authorization (formatted as `Patient/<patient_id>`).

- `endpoint` (object):
**Deprecated.** Use `patientAuthorizations[].endpoint` from the [Consent API](#consent-1). The Endpoint to which the patient authorized access, can be used to determine which health plan the patient connected. For some applications this object also includes `resources`, the FHIR resource types the endpoint supports. The Consent API's `endpoint` does not include `resources`; look up the endpoint by its `id` in the [Endpoints API](https://www.flexpa.com/docs/network#endpoints) instead.

- `usage` (enum):
**Deprecated.** Use `patientAuthorizations[].usage` from the [Consent API](#consent-1). Either `ONE_TIME` or `MULTIPLE`, indicating the data access pattern.

- `state` (string):
**Deprecated.** Use `patientAuthorizations[].state` from the [Consent API](#consent-1). The lifecycle state of the first patient authorization on the consent. One of `CREATED`, `AUTHORIZING`, `AUTHORIZED`, `EXCHANGED`, `ERRORED`, `ABANDONED`, `REVOKED`, `BOUNCED`, or `EXPIRED`.

- `client_id` (string):
The identifier of the application (client) that the access token was issued to.

- `user` (object):
An object describing the end user, containing `externalId` — the external user identifier you supplied when launching the flow (if any).

- `sync` (object):
**Deprecated.** Use `patientAuthorizations[].sync` from the [Consent API](#consent-1). Data synchronization status, containing `resourceTypes` (a map of FHIR resource type to the number of records loaded), `lastSyncedAt` (when the most recent sync completed, in Unix seconds, or `null`), and `state` (the state of the most recent sync job).

- `timeUntilMaxAuth` (number | string):
**Deprecated.** Use `patientAuthorizations[].timeUntilMaxAuth` from the [Consent API](#consent-1). Seconds remaining until the max authorization period expires, for `OAUTH` authorizations on refreshable endpoints (may be negative once past the window). Otherwise one of: `"unsupported"` (e.g., `IAL1`/`IAL2`/`CREDENTIALS` types or non-refreshable endpoints), `"unknown"` (endpoint max auth period not known), or `"indefinite"` (no expiry). Flexpa calculates this value from the endpoint's [`maxAuthPeriod`](https://www.flexpa.com/docs/network#endpoints) and the time the patient authorized. It does not reflect a shorter access duration that the patient selects while authorizing (for example, in Epic MyChart).

*Request*

```bash
ACCESS_TOKEN=your-access-token

curl https://api.flexpa.com/oauth/introspect \
  -H "Authorization: Bearer $ACCESS_TOKEN"
```

```js
const response = await fetch("https://api.flexpa.com/oauth/introspect", {
  headers: {
    "authorization": `Bearer ${accessToken}`,
  },
});

const { jti, iat, patient, exp, active } = await response.json();
```

```json
{
  "jti": "4e99f5ae-eb40-4161-9506-23b119e7136f",
  "iat": 1671116375,
  "exp": 1671202775,
  "active": true,
  "iss": "https://api.flexpa.com",
  "aud": "https://api.flexpa.com/",
  "sub": "329034ef-5fa8-4a08-99d5-c389e22a2533",
  "client_id": "a1b2c3d4-5e6f-7a8b-9c0d-1e2f3a4b5c6d",
  "patient": "Patient/33512af4-5ab2-4be2-90f5-3945cff01c1a",
  "user": {
    "externalId": "your-external-user-id"
  }
}
```

---

### Revoke

Revokes a consent and expunges the patient's data from Flexpa's cache. Both the `access_token` and the `refresh_token` stop working.

To revoke only some of the consent's patient authorizations, pass their IDs in `patient_authorization_ids`. Flexpa revokes those and expunges their data. The consent is not revoked, so its `access_token` and `refresh_token` keep working and return data from its other patient authorizations.

Identify your client in one of three ways. Use only one per request:

- **Publishable key and secret key in a Basic header** (`client_secret_basic`), the same header as the [Token Endpoint](#token). Send no `client_id` or `client_secret` in the body.
- **Publishable key and secret key in the body** (`client_secret_post`): `client_id` and `client_secret`.
- **`client_id` alone** (`none`): your publishable key, or the `client_id` that dynamic client registration returned. Clients registered through dynamic client registration are public, so they always use this method.

**Request headers**

- `Authorization` (string):
For `client_secret_basic`: `Basic` followed by the base64 encoding of `publishable_key:secret_key`

**Request body**

- `token` (string):
The `access_token` or `refresh_token` to revoke

- `client_id` (string):
Your publishable key, or the `client_id` that dynamic client registration returned. Required unless you send a Basic header.

- `client_secret` (string):
For `client_secret_post`: your secret key for the same mode as `client_id`

- `token_type_hint` (string):
`access_token` or `refresh_token`. Optional, because Flexpa can tell the two apart. Flexpa ignores a value it does not recognize.

- `patient_authorization_ids` (string[]):
The IDs of the patient authorizations to revoke, from `patientAuthorizations[].id` in the [Consent API](#consent-1). List more than one by repeating the parameter, or as an array in a JSON body. You can send it in the request body or the query string, but not both. Omit it to revoke the whole consent.

**Response fields**

- `count` (number):
The number of patient authorizations revoked by this request

**Note:**
Every ID in `patient_authorization_ids` must belong to the token's consent. If any does not, the request returns `400` with `invalid_request`, names those IDs in `error_description`, and revokes nothing. Only patient authorizations whose `state` is `AUTHORIZED` or `EXCHANGED` are revoked and included in `count`. A listed patient authorization in any other state is left unchanged.

**Note:**
A token that is unknown, expired, already revoked, or issued to another client, application, or mode returns `200` with a `count` of `0` and revokes nothing. A key pair revokes only tokens of consents your application created through the [Token Endpoint](#token) in the key pair's mode, never a token of a client registered through dynamic client registration. Access tokens expire after 24 hours, so send the `refresh_token` to revoke a consent reliably. A `503` response means the consent was not revoked; retry the request.

**Note:**
Revoke accepts Patient Access Tokens and Personal Access Tokens, not Application Access Tokens. Your own Application Access Token returns `400` with `unsupported_token_type`. See [Access Tokens](https://www.flexpa.com/docs/records#access-tokens) for how the three differ.

**Note:**
A client that fails to authenticate gets `401` with `invalid_client`, and with a `WWW-Authenticate: Basic` header when it sent a Basic header. A Basic header together with a body `client_id` or `client_secret` returns `400` with `invalid_request`.

**Note:**
This is the OAuth 2.0 token revocation endpoint ([RFC 7009](https://www.rfc-editor.org/rfc/rfc7009)). Authorization server metadata at `/.well-known/oauth-authorization-server` advertises it as `revocation_endpoint`, with `revocation_endpoint_auth_methods_supported` of `["none", "client_secret_basic", "client_secret_post"]`.

*Request*

```bash
REFRESH_TOKEN=your-refresh-token
PUBLIC_KEY=pk_test...

curl -X POST https://api.flexpa.com/oauth/revoke \
  -d "token=${REFRESH_TOKEN}" \
  -d "client_id=${PUBLIC_KEY}"
```

```bash
REFRESH_TOKEN=your-refresh-token
PUBLIC_KEY=pk_test...
SECRET_KEY=sk_test...

curl -X POST https://api.flexpa.com/oauth/revoke \
  -u "${PUBLIC_KEY}:${SECRET_KEY}" \
  -d "token=${REFRESH_TOKEN}"
```

```bash
REFRESH_TOKEN=your-refresh-token
PUBLIC_KEY=pk_test...
SECRET_KEY=sk_test...

curl -X POST https://api.flexpa.com/oauth/revoke \
  -d "token=${REFRESH_TOKEN}" \
  -d "client_id=${PUBLIC_KEY}" \
  -d "client_secret=${SECRET_KEY}"
```

```bash
REFRESH_TOKEN=your-refresh-token
PUBLIC_KEY=pk_test...

curl -X POST https://api.flexpa.com/oauth/revoke \
  -d "token=${REFRESH_TOKEN}" \
  -d "client_id=${PUBLIC_KEY}" \
  -d "patient_authorization_ids=3c9a5f0e-6b1d-4b8e-9f2a-1d7c5e8a4b20"
```

```js
const response = await fetch("https://api.flexpa.com/oauth/revoke", {
  method: "POST",
  headers: { "Content-Type": "application/x-www-form-urlencoded" },
  body: new URLSearchParams({
    token: refreshToken,
    client_id: "pk_test...",
  }).toString(),
});

const { count } = await response.json();
```

```json
{
  "count": 1
}
```

## REST API

---

### Consent

Returns the consent for the authenticated patient. The consent ID is inferred from the Patient Access Token — no URL parameter needed.

**Request headers**

- `Authorization` (string):
An `Authorization: Bearer` header containing a [Patient Access Token](https://www.flexpa.com/docs/records#patient-access-token). Presenting an [Application Access Token](https://www.flexpa.com/docs/records#application-access-token) returns `401 Unauthorized`.

**Query parameters**

- `skipResourceCounts` (boolean):
When set to `true`, omits `sync.resourceTypes` from each patient authorization and skips the FHIR resource count lookup. Useful when you only need consent state, sync status, or scope information and want a faster response. Defaults to `false`.

**Response fields**

- `id` (string):
The unique identifier for the consent record.

- `state` (string):
The consent state: `CREATED`, `AUTHORIZED`, `EXCHANGED`, `ABANDONED`, or `REVOKED`.

- `mode` (string):
The operational mode: `TEST` or `LIVE`.

- `createdAt` (string):
When the consent was created, in ISO 8601 format.

- `updatedAt` (string | null):
When the consent was last updated, in ISO 8601 format.

- `consentedAt` (string | null):
When the member gave express, opt-in consent, in ISO 8601 format: when they accepted Flexpa's terms screen, or the opt-in time your application sent when it collected consent in its own interface. `null` when no opt-in time was recorded, for example on consents created before this field existed.

- `user` (object | null):
The application user associated with this consent, set via `flexpa_external_id` during authorization.

  - `externalId` (string):
Your application's user identifier, provided as `flexpa_external_id` during authorization.

- `patientAuthorizations` (array):
Patient authorizations associated with this consent.

  - `id` (string):
Unique identifier for the patient authorization.

  - `type` (string):
How the patient connected. `OAUTH` for SMART on FHIR authorization (health insurers and medical record systems), `CREDENTIALS` for patient portal authorization, `IAL2` for identity verification via nationwide exchanges (TEFCA IAS), or `IAL1` for TEFCA search-flow authorizations using self-attested demographics. See [network types](https://www.flexpa.com/docs/network#network-types).

  - `state` (string):
Authorization state: `CREATED`, `AUTHORIZED`, `EXCHANGED`, `ERRORED`, `ABANDONED`, `REVOKED`, `AUTHORIZING`, `BOUNCED`, or `EXPIRED`.

  - `usage` (string):
Data access pattern: `ONE_TIME` or `MULTIPLE`.

  - `createdAt` (string):
When the authorization was created (ISO 8601).

  - `authorizedAt` (string | null):
When the patient completed authorization (ISO 8601).

  - `exchangedAt` (string | null):
When the authorization code was exchanged for tokens (ISO 8601).

  - `revokedAt` (string | null):
When the authorization was revoked (ISO 8601).

  - `expiredAt` (string | null):
When the authorization expired (ISO 8601).

  - `refreshExpiredAt` (string | null):
When the endpoint stopped accepting refreshes for this authorization (ISO 8601), such as after Flexpa's refresh retries run out and it sends a [`refresh_expired`](https://www.flexpa.com/docs/records/webhooks#refresh_expired) webhook. The authorization's `state` stays `EXCHANGED` and `active` stays `true`. See [When an endpoint stops honoring the authorization](https://www.flexpa.com/docs/guides/usage#when-an-endpoint-stops-honoring-the-authorization).

  - `patient` (string | null):
FHIR Patient reference (formatted as `Patient/<id>`).

  - `identityProvider` (string | null):
Identity verification provider used for `IAL2` authorizations: `CLEAR`, `IDME`, or `PERSONA`. Only present for IAL2 types.

  - `endpoint` (object | null):
The health plan endpoint the patient connected to.

    - `id` (string):
Unique identifier for the endpoint.

    - `name` (string):
Machine-readable endpoint name.

    - `label` (string[]):
Human-readable display labels for the endpoint.

    - `refreshable` (boolean):
Whether this endpoint supports token refresh for `MULTIPLE` usage.

  - `active` (boolean):
Whether the authorization is currently usable (exchanged, not revoked or expired).

  - `timeUntilMaxAuth` (number | string):
Seconds remaining until the max authorization period expires, for `OAUTH` authorizations on refreshable endpoints (may be negative once past the window). Otherwise one of: `"unsupported"` (e.g., `IAL1`/`IAL2`/`CREDENTIALS` types or non-refreshable endpoints), `"unknown"` (endpoint max auth period not known), or `"indefinite"` (no expiry). Flexpa calculates this value from the endpoint's [`maxAuthPeriod`](https://www.flexpa.com/docs/network#endpoints) and the time the patient authorized. It does not reflect a shorter access duration that the patient selects while authorizing (for example, in Epic MyChart).

  - `sync` (object):
Current data synchronization status.

    - `resourceTypes` (object):
Map of FHIR resource type names to the count of resources synced (e.g. `{"ExplanationOfBenefit": 42}`). Omitted when `skipResourceCounts=true`.

    - `lastSyncedAt` (number | null):
Unix timestamp (seconds) of the last completed sync.

    - `state` (string):
Sync job state: `CREATED`, `WAITING`, `ACTIVE`, `COMPLETED`, or `FAILED`. Omitted when no sync job has run yet.

  - `scope` (object):
SMART on FHIR scope information. Present when scope data is available.

    - `requested` (string[]):
Scopes requested during authorization.

    - `granted` (string[]):
Scopes granted by the endpoint.

    - `rejected` (string[]):
Scopes that were requested but not granted.

*Request*

```bash
ACCESS_TOKEN=your-access-token

curl https://api.flexpa.com/rest/consent \
  -H "Authorization: Bearer $ACCESS_TOKEN"
```

```js
const response = await fetch("https://api.flexpa.com/rest/consent", {
  headers: {
    "authorization": `Bearer ${accessToken}`,
  },
});

const consent = await response.json();
```

```json
{
  "id": "01234567-89ab-cdef-0123-456789abcdef",
  "state": "EXCHANGED",
  "mode": "LIVE",
  "createdAt": "2024-01-15T10:30:00.000Z",
  "updatedAt": null,
  "consentedAt": "2024-01-15T10:30:10.000Z",
  "user": { "externalId": "user-123" },
  "patientAuthorizations": [
    {
      "id": "98765432-10fe-dcba-9876-543210fedcba",
      "type": "IAL2",
      "state": "EXCHANGED",
      "usage": "ONE_TIME",
      "createdAt": "2024-01-15T10:31:00.000Z",
      "authorizedAt": "2024-01-15T10:31:30.000Z",
      "exchangedAt": "2024-01-15T10:32:00.000Z",
      "revokedAt": null,
      "expiredAt": null,
      "refreshExpiredAt": null,
      "patient": "Patient/33512af4-5ab2-4be2-90f5-3945cff01c1a",
      "identityProvider": "CLEAR",
      "endpoint": null,
      "active": true,
      "timeUntilMaxAuth": "unsupported",
      "sync": {
        "resourceTypes": { "Condition": 12, "Patient": 1 },
        "lastSyncedAt": 1705312200,
        "state": "COMPLETED"
      }
    },
    {
      "id": "abcdef01-2345-6789-abcd-ef0123456789",
      "type": "OAUTH",
      "state": "EXCHANGED",
      "usage": "ONE_TIME",
      "createdAt": "2024-01-15T10:33:00.000Z",
      "authorizedAt": "2024-01-15T10:33:30.000Z",
      "exchangedAt": "2024-01-15T10:34:00.000Z",
      "revokedAt": null,
      "expiredAt": null,
      "refreshExpiredAt": null,
      "patient": "Patient/33512af4-5ab2-4be2-90f5-3945cff01c1a",
      "endpoint": {
        "id": "d39433b7-0fbd-4bc2-bdae-fb276799979f",
        "name": "epic-mychart",
        "label": ["Epic MyChart"],
        "refreshable": true
      },
      "active": true,
      "timeUntilMaxAuth": 86400,
      "sync": {
        "resourceTypes": { "ExplanationOfBenefit": 42, "Patient": 1 },
        "lastSyncedAt": 1705312200,
        "state": "COMPLETED"
      },
      "scope": {
        "requested": ["patient/*.read", "offline_access"],
        "granted": ["patient/Patient.read", "patient/Coverage.read"],
        "rejected": ["patient/Observation.read"]
      }
    }
  ]
}
```

---

### Consent by ID

Returns a specific consent by ID. Application tokens can look up any consent belonging to their application. Patient tokens can access only the consent whose ID matches their own token.

**Request headers**

- `Authorization` (string):
An `Authorization: Bearer` header containing either a [Patient Access Token](https://www.flexpa.com/docs/records#patient-access-token) or an [Application Access Token](https://www.flexpa.com/docs/records#application-access-token).

**Query parameters**

- `skipResourceCounts` (boolean):
When set to `true`, omits `sync.resourceTypes` from each patient authorization and skips the FHIR resource count lookup. Defaults to `false`.

- `include` (string[]):
Optional list of additional fields to include in the response. Supported values: `tefcaOutcomes`. When included, each `IAL2` patient authorization will contain a `sync.tefcaOutcomes` object with a summary of the most recent TEFCA sync. Omitted by default.

**Response fields**

Returns the same fields as [Consent](#consent-1). When `?include=tefcaOutcomes` is passed, `IAL2` patient authorizations include an additional field under `sync`:

- `tefcaOutcomes` (object):
Summary of the most recent TEFCA network query. Only present for `IAL2` patient authorizations where at least one sync has completed.

  - `qhinNetwork` (string):
QHIN network queried: `COMMONWELL` or `KNO2`.

  - `patientsFound` (number): Number of patient records matched across the network.

  - `facilitiesQueried` (number): Total number of facilities queried during discovery.

  - `documentsRetrieved` (number): Total clinical documents retrieved across all matched facilities.

  - `failureReason` (string | null):
Error message if the TEFCA query failed, otherwise `null`.

  - `fhirFunnel` (object):
Counts for facilities with SMART-capable FHIR endpoints that were offered as sign-in options.

    - `facilitiesPresented` (number): Number of FHIR sign-in capable facilities shown to the patient.

    - `facilitiesSignInAttempted` (number): Number of facilities the patient clicked Sign-In for.

    - `facilitiesSignedIn` (number): Number of facilities where the patient completed OAuth sign-in.

  - `completedAt` (string): ISO 8601 timestamp when the TEFCA query completed.

*Request*

```bash
ACCESS_TOKEN=your-access-token
CONSENT_ID=your-consent-id

curl "https://api.flexpa.com/rest/consent/$CONSENT_ID?include=tefcaOutcomes" \
  -H "Authorization: Bearer $ACCESS_TOKEN"
```

```js
const response = await fetch(`https://api.flexpa.com/rest/consent/${consentId}?include=tefcaOutcomes`, {
  headers: {
    "authorization": `Bearer ${accessToken}`,
  },
});

const consent = await response.json();
```

```json
{
  "id": "01234567-89ab-cdef-0123-456789abcdef",
  "state": "EXCHANGED",
  "mode": "LIVE",
  "createdAt": "2024-01-15T10:30:00.000Z",
  "updatedAt": null,
  "consentedAt": "2024-01-15T10:30:10.000Z",
  "user": { "externalId": "user-123" },
  "patientAuthorizations": [
    {
      "id": "98765432-10fe-dcba-9876-543210fedcba",
      "type": "IAL2",
      "state": "EXCHANGED",
      "usage": "ONE_TIME",
      "createdAt": "2024-01-15T10:31:00.000Z",
      "authorizedAt": "2024-01-15T10:31:30.000Z",
      "exchangedAt": "2024-01-15T10:32:00.000Z",
      "revokedAt": null,
      "expiredAt": null,
      "refreshExpiredAt": null,
      "patient": "Patient/33512af4-5ab2-4be2-90f5-3945cff01c1a",
      "identityProvider": "CLEAR",
      "endpoint": null,
      "active": true,
      "timeUntilMaxAuth": "unsupported",
      "sync": {
        "resourceTypes": { "Patient": 1, "DocumentReference": 4 },
        "lastSyncedAt": 1705312200,
        "state": "COMPLETED",
        "tefcaOutcomes": {
          "qhinNetwork": "COMMONWELL",
          "patientsFound": 7,
          "facilitiesQueried": 7,
          "documentsRetrieved": 4,
          "failureReason": null,
          "fhirFunnel": {
            "facilitiesPresented": 3,
            "facilitiesSignInAttempted": 2,
            "facilitiesSignedIn": 2
          },
          "completedAt": "2026-06-10T16:56:17.000Z"
        }
      }
    }
  ]
}
```

---

### List consents

List all consents for your application with cursor-based pagination. Requires an [Application Access Token](https://www.flexpa.com/docs/records#application-access-token).

Each consent in the list includes summary patient authorization data (state, endpoint, active status) but omits the detailed `sync` and `scope` fields returned by [Consent by ID](#consent-by-id). To get full details for a specific consent, use `GET /rest/consent/:consentId`.

**Request headers**

- `Authorization` (string):
An `Authorization: Bearer` header value must be presented with an [Application Access Token](https://www.flexpa.com/docs/records#application-access-token).

**Query parameters**

- `limit` (number):
Maximum number of consents per page. Defaults to 20, maximum 100.

- `cursor` (string):
Pagination cursor from a previous response's `meta.nextCursor`. Omit for the first page.

- `state` (string):
Filter by consent state: `CREATED`, `AUTHORIZED`, `EXCHANGED`, `ABANDONED`, or `REVOKED`.

**Response fields**

- `consents` (array):
An array of consent objects. Unlike [Consent by ID](#consent-by-id), the `patientAuthorizations` here are summaries — `sync` and `scope` are omitted. Use `GET /rest/consent/:consentId` for full details.

  - `id` (string):
The unique identifier for the consent record.

  - `state` (string):
The consent state: `CREATED`, `AUTHORIZED`, `EXCHANGED`, `ABANDONED`, or `REVOKED`.

  - `mode` (string):
The operational mode: `TEST` or `LIVE`.

  - `createdAt` (string):
When the consent was created, in ISO 8601 format.

  - `updatedAt` (string | null):
When the consent was last updated, in ISO 8601 format.

  - `consentedAt` (string | null):
When the member gave express, opt-in consent, in ISO 8601 format: when they accepted Flexpa's terms screen, or the opt-in time your application sent when it collected consent in its own interface. `null` when no opt-in time was recorded, for example on consents created before this field existed.

  - `user` (object | null):
The application user associated with this consent.

    - `externalId` (string):
Your application's user identifier, provided as `flexpa_external_id` during authorization.

  - `patientAuthorizations` (array):
Summary patient authorizations (no `sync` or `scope`).

    - `id` (string):
Unique identifier for the patient authorization.

    - `type` (string):
How the patient connected. `OAUTH` for SMART on FHIR authorization (health insurers and medical record systems), `CREDENTIALS` for patient portal authorization, `IAL2` for identity verification via nationwide exchanges (TEFCA IAS), or `IAL1` for TEFCA search-flow authorizations using self-attested demographics. See [network types](https://www.flexpa.com/docs/network#network-types).

    - `state` (string):
Authorization state: `CREATED`, `AUTHORIZED`, `EXCHANGED`, `ERRORED`, `ABANDONED`, `REVOKED`, `AUTHORIZING`, `BOUNCED`, or `EXPIRED`.

    - `usage` (string):
Data access pattern: `ONE_TIME` or `MULTIPLE`.

    - `createdAt` (string):
When the authorization was created (ISO 8601).

    - `authorizedAt` (string | null):
When the patient completed authorization (ISO 8601).

    - `exchangedAt` (string | null):
When the authorization code was exchanged for tokens (ISO 8601).

    - `revokedAt` (string | null):
When the authorization was revoked (ISO 8601).

    - `expiredAt` (string | null):
When the authorization expired (ISO 8601).

    - `refreshExpiredAt` (string | null):
When the endpoint stopped accepting refreshes for this authorization (ISO 8601), such as after Flexpa's refresh retries run out and it sends a [`refresh_expired`](https://www.flexpa.com/docs/records/webhooks#refresh_expired) webhook. The authorization's `state` stays `EXCHANGED` and `active` stays `true`. See [When an endpoint stops honoring the authorization](https://www.flexpa.com/docs/guides/usage#when-an-endpoint-stops-honoring-the-authorization).

    - `patient` (string | null):
FHIR Patient reference (formatted as `Patient/<id>`).

    - `identityProvider` (string | null):
Identity verification provider. Only present for `IAL2` authorizations: `CLEAR`, `IDME`, or `PERSONA`.

    - `endpoint` (object | null):
The health plan endpoint the patient connected to.

      - `id` (string):
Unique identifier for the endpoint.

      - `name` (string):
Machine-readable endpoint name.

      - `label` (string[]):
Human-readable display labels for the endpoint.

      - `refreshable` (boolean):
Whether this endpoint supports token refresh for `MULTIPLE` usage.

    - `active` (boolean):
Whether the authorization is currently usable (exchanged, not revoked or expired).

    - `timeUntilMaxAuth` (number | string):
Seconds remaining until the max authorization period expires, for `OAUTH` authorizations on refreshable endpoints (may be negative once past the window). Otherwise one of: `"unsupported"` (e.g., `IAL1`/`IAL2`/`CREDENTIALS` types or non-refreshable endpoints), `"unknown"` (endpoint max auth period not known), or `"indefinite"` (no expiry). Flexpa calculates this value from the endpoint's [`maxAuthPeriod`](https://www.flexpa.com/docs/network#endpoints) and the time the patient authorized. It does not reflect a shorter access duration that the patient selects while authorizing (for example, in Epic MyChart).

- `meta` (object):
Pagination metadata.

  - `hasMore` (boolean):
Whether more results are available beyond this page.

  - `nextCursor` (string | null):
Cursor to pass as the `cursor` query parameter for the next page. Null when there are no more results.

*Request*

```bash
ACCESS_TOKEN=your-application-access-token

# List all consents
curl "https://api.flexpa.com/rest/consents?limit=20" \
  -H "Authorization: Bearer $ACCESS_TOKEN"

# Filter by state and mode
curl "https://api.flexpa.com/rest/consents?state=EXCHANGED" \
  -H "Authorization: Bearer $ACCESS_TOKEN"
```

```js
// List all consents
const response = await fetch("https://api.flexpa.com/rest/consents?limit=20", {
  headers: {
    "authorization": `Bearer ${accessToken}`,
  },
});

const { consents, meta } = await response.json();

// Filter by state
const filtered = await fetch(
  "https://api.flexpa.com/rest/consents?state=EXCHANGED",
  {
    headers: {
      "authorization": `Bearer ${accessToken}`,
    },
  },
);
```

```json
{
  "consents": [
    {
      "id": "01234567-89ab-cdef-0123-456789abcdef",
      "state": "EXCHANGED",
      "mode": "LIVE",
      "createdAt": "2024-01-15T10:30:00.000Z",
      "updatedAt": null,
      "consentedAt": "2024-01-15T10:30:10.000Z",
          "user": { "externalId": "user-123" },
      "patientAuthorizations": [
        {
          "id": "98765432-10fe-dcba-9876-543210fedcba",
          "type": "OAUTH",
          "state": "EXCHANGED",
          "usage": "ONE_TIME",
          "createdAt": "2024-01-15T10:31:00.000Z",
          "authorizedAt": "2024-01-15T10:31:30.000Z",
          "exchangedAt": "2024-01-15T10:32:00.000Z",
          "revokedAt": null,
          "expiredAt": null,
          "refreshExpiredAt": null,
          "patient": "Patient/33512af4-5ab2-4be2-90f5-3945cff01c1a",
          "endpoint": {
            "id": "d39433b7-0fbd-4bc2-bdae-fb276799979f",
            "name": "humana-sandbox",
            "label": ["Humana"],
            "refreshable": true
          },
          "active": true,
          "timeUntilMaxAuth": 86400
        }
      ]
    }
  ],
  "meta": {
    "hasMore": true,
    "nextCursor": "eyJjcmVhdGVkQXQiOi..."
  }
}
```

## Discovery API

Standard discovery endpoints for OAuth 2.0 and SMART on FHIR clients to automatically discover authorization server capabilities.

**Note:**
For automated clients, prefer the discovery metadata for the endpoints and auth methods you use. These values are authoritative and may differ from examples elsewhere in the docs.

---

### OAuth Authorization Server Metadata

Returns OAuth 2.0 Authorization Server Metadata per [RFC 8414](https://datatracker.ietf.org/doc/html/rfc8414). Use this endpoint for standard OAuth 2.0 clients and MCP integrations.

**Response fields**

- `issuer` (string):
The authorization server's issuer identifier.

- `authorization_endpoint` (string):
URL of the authorization endpoint.

- `token_endpoint` (string):
URL of the token endpoint.

- `registration_endpoint` (string):
URL of the dynamic client registration endpoint.

- `jwks_uri` (string):
URL of the JSON Web Key Set.

- `response_modes_supported` (string[]):
Supported response modes: `query`.

- `grant_types_supported` (string[]):
Supported grant types: `authorization_code`, `refresh_token`, `client_credentials`.

- `code_challenge_methods_supported` (string[]):
Supported PKCE methods: `S256`.

- `token_endpoint_auth_methods_supported` (string[]):
Supported token endpoint authentication methods: `client_secret_basic`, `none`.

- `scopes_supported` (string[]):
Supported scopes: `launch/patient`, `offline_access`.

*Request*

```bash
curl https://api.flexpa.com/.well-known/oauth-authorization-server
```

```json
{
  "issuer": "https://api.flexpa.com",
  "authorization_endpoint": "https://api.flexpa.com/oauth/authorize",
  "token_endpoint": "https://api.flexpa.com/oauth/token",
  "registration_endpoint": "https://api.flexpa.com/oauth/register",
  "jwks_uri": "https://api.flexpa.com/.well-known/jwks.json",
  "response_types_supported": ["code"],
  "response_modes_supported": ["query"],
  "grant_types_supported": ["authorization_code", "refresh_token", "client_credentials"],
  "code_challenge_methods_supported": ["S256"],
  "token_endpoint_auth_methods_supported": ["client_secret_basic", "none"],
  "scopes_supported": ["launch/patient", "offline_access"]
}
```

---

### OpenID Configuration

Returns OpenID Connect Discovery metadata per [OpenID Connect Core 1.0](https://openid.net/specs/openid-connect-discovery-1_0.html). Use this endpoint for OIDC-compatible clients.

**Response fields**

- `issuer` (string):
The authorization server's issuer identifier.

- `authorization_endpoint` (string):
URL of the authorization endpoint.

- `token_endpoint` (string):
URL of the token endpoint.

- `registration_endpoint` (string):
URL of the dynamic client registration endpoint.

- `jwks_uri` (string):
URL of the JSON Web Key Set.

- `response_types_supported` (string[]):
Supported response types: `code`.

- `response_modes_supported` (string[]):
Supported response modes: `query`.

- `grant_types_supported` (string[]):
Supported grant types: `authorization_code`, `refresh_token`, `client_credentials`.

- `code_challenge_methods_supported` (string[]):
Supported PKCE methods: `S256`.

- `token_endpoint_auth_methods_supported` (string[]):
Supported authentication methods: `client_secret_basic`, `none`.

- `scopes_supported` (string[]):
Supported scopes: `launch/patient`, `offline_access`.

- `subject_types_supported` (string[]):
Supported subject types: `public`.

- `id_token_signing_alg_values_supported` (string[]):
Supported ID token signing algorithms: `RS256`.

- `service_documentation` (string):
URL to service documentation.

*Request*

```bash
curl https://api.flexpa.com/.well-known/openid-configuration
```

```json
{
  "issuer": "https://api.flexpa.com",
  "authorization_endpoint": "https://api.flexpa.com/oauth/authorize",
  "token_endpoint": "https://api.flexpa.com/oauth/token",
  "registration_endpoint": "https://api.flexpa.com/oauth/register",
  "jwks_uri": "https://api.flexpa.com/.well-known/jwks.json",
  "response_types_supported": ["code"],
  "response_modes_supported": ["query"],
  "grant_types_supported": ["authorization_code", "refresh_token", "client_credentials"],
  "code_challenge_methods_supported": ["S256"],
  "token_endpoint_auth_methods_supported": ["client_secret_basic", "none"],
  "scopes_supported": ["launch/patient", "offline_access"],
  "subject_types_supported": ["public"],
  "id_token_signing_alg_values_supported": ["RS256"],
  "service_documentation": "https://flexpa.com/docs"
}
```

---

### SMART Configuration

Returns SMART App Launch configuration per the [SMART App Launch specification](https://hl7.org/fhir/smart-app-launch/conformance.html). Use this endpoint for SMART on FHIR applications.

**Response fields**

- `issuer` (string):
The authorization server's issuer identifier.

- `authorization_endpoint` (string):
URL of the authorization endpoint.

- `token_endpoint` (string):
URL of the token endpoint.

- `jwks_uri` (string):
URL of the JSON Web Key Set.

- `grant_types_supported` (string[]):
Supported grant types: `authorization_code`, `refresh_token`, `client_credentials`.

- `response_types_supported` (string[]):
Supported response types: `code`.

- `scopes_supported` (string[]):
Supported scopes: `launch/patient`, `offline_access`.

- `code_challenge_methods_supported` (string[]):
Supported PKCE methods: `S256`.

- `token_endpoint_auth_methods_supported` (string[]):
Supported authentication methods: `client_secret_basic`, `none`.

- `capabilities` (string[]):
SMART capabilities: `launch-standalone`, `client-public`, `client-confidential-symmetric`, `context-standalone-patient`, `permission-offline`, `permission-patient`.

*Request*

```bash
curl https://api.flexpa.com/.well-known/smart-configuration
```

```json
{
  "issuer": "https://api.flexpa.com",
  "authorization_endpoint": "https://api.flexpa.com/link/auth",
  "token_endpoint": "https://api.flexpa.com/link/token",
  "jwks_uri": "https://api.flexpa.com/.well-known/jwks.json",
  "grant_types_supported": ["authorization_code", "refresh_token", "client_credentials"],
  "response_types_supported": ["code"],
  "scopes_supported": ["launch/patient", "offline_access"],
  "code_challenge_methods_supported": ["S256"],
  "token_endpoint_auth_methods_supported": ["client_secret_basic", "none"],
  "capabilities": [
    "launch-standalone",
    "client-public",
    "client-confidential-symmetric",
    "context-standalone-patient",
    "permission-offline",
    "permission-patient"
  ]
}
```

---

### JWKS

Returns the JSON Web Key Set per [RFC 7517](https://datatracker.ietf.org/doc/html/rfc7517). Use this endpoint to verify token signatures.

The set contains EC (`ES256`/`ES384`) keys used to sign tokens issued by Flexpa API, and may contain an RSA key used to sign the `client_assertion` JWTs Flexpa sends when authenticating to endpoints that require `private_key_jwt` authentication (`RS256` by default, `RS384` where the endpoint requires it). Each key's `kid` is the RFC 7638 thumbprint of the key.

*Request*

```bash
curl https://api.flexpa.com/.well-known/jwks.json
```

## Mobile applications

Consent is a standard OAuth 2.0 PKCE flow, so it works out of the box with the native authentication session APIs that iOS, Android, and React Native already ship: `ASWebAuthenticationSession` on iOS and Chrome Custom Tabs on Android. This is the mechanism Apple and Google define for authenticating with a third party, and it is what patients already recognize from other apps they log into.

Building on the OS-native session, rather than rendering the flow inside your app, is what makes the integration strong:

- **Works out of the box.** Consent is built to the OS standards for authentication, so you call the API the platform already gives you. There is no Flexpa SDK to install, no versions to keep in step with our releases, no native module to configure, and no WebView shim to maintain.
- **Password manager autofill.** The session runs in the system browser context, so the patient's saved credentials, passkeys, and biometrics work when they sign in with their health plan or provider. Payer login pages are the highest-friction step in the flow, and autofill removes most of that friction.
- **Shared browser session.** If the patient is already signed in to their payer in Safari or Chrome, they may not have to sign in again.
- **Credentials stay out of your app.** Your app never sees the patient's payer username and password, which keeps you out of scope for handling them.
- **Native return path.** The OS hands the authorization code back to your app through your registered URL scheme and closes the session automatically.

**Warning:**
Never render the consent flow in an embedded WebView (`WKWebView` on iOS or `WebView` on Android). Embedded WebViews let the host app read the credentials the patient types, break password manager autofill, and are prohibited by [OAuth 2.0 security best practices](https://datatracker.ietf.org/doc/html/rfc8252#section-8.12). Many payer login pages also block them.

The examples below all send `flexpa_resume=true`. Patients on a phone are the most likely to leave mid-flow, whether they switch to their email app for a verification code or dismiss the session by accident. Resume lets them pick up where they left off on the next attempt instead of starting over. It relies on the consent session cookie, which the native session keeps because it shares cookie storage with the system browser.

### iOS

Use `ASWebAuthenticationSession` (iOS 12+), Apple's native authentication session API. It:
- Opens a secure system browser context
- Shares authentication state and autofill with Safari
- Automatically handles the redirect callback and dismisses itself
- Prevents credential interception by the host app

**Setup:**
1. Register your redirect URI scheme in `Info.plist` and in the [Flexpa Portal](https://portal.flexpa.com)
2. Generate PKCE parameters before starting the session
3. Store the code verifier in Keychain or memory
4. Exchange the authorization code for tokens

**Note:**
Leave `prefersEphemeralWebBrowserSession` set to `false` so the session can reuse the patient's existing Safari cookies and offer saved credentials.

*iOS Implementation*

```swift
import AuthenticationServices
import CryptoKit

class FlexpaAuth: NSObject, ASWebAuthenticationPresentationContextProviding {
  private var codeVerifier: String?

  func authenticate() {
    // Generate PKCE parameters
    codeVerifier = generateCodeVerifier()
    let codeChallenge = generateCodeChallenge(codeVerifier!)

    // Build authorization URL
    var components = URLComponents(string: "https://api.flexpa.com/oauth/authorize")!
    components.queryItems = [
      URLQueryItem(name: "client_id", value: "pk_test_..."),
      URLQueryItem(name: "redirect_uri", value: "yourapp://callback"),
      URLQueryItem(name: "response_type", value: "code"),
      URLQueryItem(name: "code_challenge", value: codeChallenge),
      URLQueryItem(name: "code_challenge_method", value: "S256"),
      URLQueryItem(name: "scope", value: "launch/patient offline_access"),
      URLQueryItem(name: "flexpa_external_id", value: "usr_1234"),
      URLQueryItem(name: "flexpa_resume", value: "true"),
      URLQueryItem(name: "state", value: UUID().uuidString)
    ]

    let session = ASWebAuthenticationSession(
      url: components.url!,
      callbackURLScheme: "yourapp"
    ) { [weak self] callbackURL, error in
      guard let url = callbackURL,
            let code = URLComponents(url: url, resolvingAgainstBaseURL: false)?
              .queryItems?.first(where: { $0.name == "code" })?.value
      else { return }

      self?.exchangeCode(code)
    }

    session.presentationContextProvider = self
    session.prefersEphemeralWebBrowserSession = false
    session.start()
  }

  func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
    UIApplication.shared.connectedScenes
      .compactMap { $0 as? UIWindowScene }
      .flatMap { $0.windows }
      .first { $0.isKeyWindow }!
  }
}
```

### Android

Use **Chrome Custom Tabs**, Android's native in-app browser session. It:
- Opens a Chrome tab within your app's task
- Shares cookies, saved passwords, and autofill with Chrome
- Provides a native-feeling UI
- Prevents credential interception

**Setup:**
1. Add an intent filter for your redirect URI in `AndroidManifest.xml`, and register the same URI in the [Flexpa Portal](https://portal.flexpa.com)
2. Generate PKCE parameters before launching Custom Tabs
3. Store the code verifier in `EncryptedSharedPreferences`
4. Handle the redirect in your callback Activity

**Note:**
[AppAuth for Android](https://github.com/openid/AppAuth-Android) wraps this pattern, including PKCE generation and the token exchange. Point its `authorizationEndpoint` and `tokenEndpoint` at Flexpa and pass the Flexpa parameters as additional query parameters.

*Android Implementation*

```xml
<activity
  android:name=".OAuthCallbackActivity"
  android:exported="true"
  android:launchMode="singleTask">
  <intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data
      android:scheme="yourapp"
      android:host="callback" />
  </intent-filter>
</activity>
```

```kotlin
class FlexpaAuth(private val context: Context) {
  private val prefs = EncryptedSharedPreferences.create(...)

  fun authenticate() {
    // Generate and store PKCE verifier
    val codeVerifier = generateCodeVerifier()
    prefs.edit().putString("code_verifier", codeVerifier).apply()
    val codeChallenge = generateCodeChallenge(codeVerifier)

    // Build authorization URL
    val authUrl = Uri.Builder()
      .scheme("https")
      .authority("api.flexpa.com")
      .path("/oauth/authorize")
      .appendQueryParameter("client_id", "pk_test_...")
      .appendQueryParameter("redirect_uri", "yourapp://callback")
      .appendQueryParameter("response_type", "code")
      .appendQueryParameter("code_challenge", codeChallenge)
      .appendQueryParameter("code_challenge_method", "S256")
      .appendQueryParameter("scope", "launch/patient offline_access")
      .appendQueryParameter("flexpa_external_id", "usr_1234")
      .appendQueryParameter("flexpa_resume", "true")
      .appendQueryParameter("state", UUID.randomUUID().toString())
      .build()

    // Launch Custom Tabs
    CustomTabsIntent.Builder()
      .build()
      .launchUrl(context, authUrl)
  }
}

// In OAuthCallbackActivity
override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)
  intent?.data?.getQueryParameter("code")?.let { code ->
    val verifier = prefs.getString("code_verifier", null)
    exchangeCode(code, verifier)
  }
  finish()
}
```

### React Native

React Native reaches the same native authentication sessions as the platforms above. [`expo-auth-session`](https://docs.expo.dev/versions/latest/sdk/auth-session/) calls `ASWebAuthenticationSession` on iOS and Chrome Custom Tabs on Android, generates the PKCE verifier and challenge, and returns the authorization code to your component. You point it at the Flexpa endpoints the way you would any other OAuth provider, and the patient gets the same password manager autofill and shared browser session a fully native app would give them.

**Setup:**
1. Install `expo-auth-session` and `expo-web-browser`
2. Set a `scheme` in `app.config.js` (for example `flexpaexample`) and register the resulting redirect URI in the [Flexpa Portal](https://portal.flexpa.com)
3. Call `WebBrowser.maybeCompleteAuthSession()` at module scope so the session closes when the app is reopened by the deep link
4. Exchange the authorization code for tokens, sending `client_id` in the body since a mobile app is a public client

**Note:**
Bare React Native apps can use [`react-native-app-auth`](https://github.com/FormidableLabs/react-native-app-auth) instead. It wraps the same two native APIs and takes the same parameters, under the names `redirectUrl` and `additionalParameters`.

**Note:**
`makeRedirectUri` returns a custom-scheme URI built from the `scheme` in your app config, such as `flexpaexample://`. That is the value to register in the Portal, and it is available at runtime as `request.redirectUri`.

**Warning:**
Store the access and refresh tokens in the Keychain or Keystore, for example with `expo-secure-store`. Never put them in `AsyncStorage`, which is unencrypted on disk.

*React Native Implementation*

```ts
import * as WebBrowser from 'expo-web-browser';
import { makeRedirectUri, useAuthRequest, CodeChallengeMethod } from 'expo-auth-session';

// Closes the auth session when the app is reopened by the redirect
WebBrowser.maybeCompleteAuthSession();

const discovery = {
  authorizationEndpoint: 'https://api.flexpa.com/oauth/authorize',
  tokenEndpoint: 'https://api.flexpa.com/oauth/token',
};

export function useFlexpaAuth(externalId: string) {
  const redirectUri = makeRedirectUri({ scheme: 'flexpaexample' });

  const [request, response, promptAsync] = useAuthRequest(
    {
      clientId: 'pk_test_...',
      responseType: 'code',
      redirectUri,
      scopes: ['launch/patient', 'offline_access'],
      usePKCE: true,
      codeChallengeMethod: CodeChallengeMethod.S256,
      extraParams: {
        flexpa_external_id: externalId,
        flexpa_resume: 'true',
      },
    },
    discovery
  );

  return {
    request,
    response,
    promptAsync,
    redirectUri,
    codeVerifier: request?.codeVerifier,
    isReady: !!request,
  };
}
```

```tsx
import { Alert, Button } from 'react-native';
import { useFlexpaAuth } from './useFlexpaAuth';
import { exchangeCodeForTokens } from './flexpaClient';

export function ConnectScreen() {
  const { promptAsync, redirectUri, codeVerifier, isReady } = useFlexpaAuth('usr_1234');

  const handleConnect = async () => {
    if (!isReady || !codeVerifier) return;

    // Opens ASWebAuthenticationSession on iOS, Chrome Custom Tabs on Android
    const result = await promptAsync();
    if (result?.type !== 'success') return;

    try {
      const tokens = await exchangeCodeForTokens(
        result.params.code,
        codeVerifier,
        redirectUri
      );
      // Persist tokens in the Keychain or Keystore
    } catch {
      Alert.alert('Connection failed', 'Please try again.');
    }
  };

  return <Button title="Connect your plan" disabled={!isReady} onPress={handleConnect} />;
}
```

```ts
export async function exchangeCodeForTokens(
  code: string,
  codeVerifier: string,
  redirectUri: string
) {
  const response = await fetch('https://api.flexpa.com/oauth/token', {
    method: 'POST',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    body: new URLSearchParams({
      grant_type: 'authorization_code',
      // Public clients send client_id in the body instead of using Basic Auth
      client_id: 'pk_test_...',
      code,
      code_verifier: codeVerifier,
      redirect_uri: redirectUri,
    }).toString(),
  });

  if (!response.ok) throw new Error(`Token exchange failed: ${response.status}`);

  return response.json();
}
```

## Workflow ideas

Here are different techniques for embedding Consent into your application to ensure participation of new users and activate existing users:

### Onboarding

Incorporate the consent flow directly into your new user onboarding experience.
New users have higher engagement as completion of enrollment is a compelling event to complete the authorization.

As users sign up and learn to navigate your application, prompt them to link their payer account through Flexpa.
This could be presented as an initial setup step, or as a highlighted feature in a tutorial.
Make sure to explain the benefits and reassure the user about the security of the process.

### Integration

Embed the consent trigger as an activity or option within your existing patient interface.
This could be as a button, a banner, or a new tab in a user's profile settings.
Explain the value of linking their payer account and gently prompt users to do so.

#### Personalization

On the user's profile or dashboard, you can include a personalized status or progress bar indicating the completion of their profile setup, including whether they've linked their payer account.
This visual cue can motivate users to complete their setup.

#### In-app notifications

Use notifications in your application to remind users to link their payer accounts.
These can be triggered based on certain user behaviors, like logging in or navigating to certain sections.
Make sure these reminders are not too intrusive and clearly communicate the value.

### Campaigns

Send targeted marketing campaigns to encourage users to link their payer accounts:
- **Email**: Craft an informative email explaining the benefits and including a direct link
- **SMS**: Send a short, compelling message with a link to your application

### Incentivization

Consider offering small incentives or rewards for users who link their payer account.
This could be in the form of discounts, access to premium features, or other value-added benefits.

## Best practices

Below are some principles we recommend to optimize user conversion and ensure users complete the authorization flow.

### Explain the benefit

Your UI should tell the user why they want to use Flexpa and the value they get from linking their payer.
For example, linking their payer account might save them time inputting medication data manually or enable more relevant recommendations.

### Set expectations

Before starting the flow, explain to the user that they'll be redirected to authorize with their health plan.
Explain that they'll need to input their username and password, but also that they can create an account with their payer if they do not have one.
Explain what data your app collects and why it's needed.

### Present as default

Rather than presenting Flexpa and manual flows as equal alternatives, encourage your customers to use Flexpa through the size, positioning, and color of the consent entry point.
You can also use labels such as "Recommended" or "Preferred".

### Security

Let customers know that the consent flow is secure and uses industry-standard OAuth 2.0 with PKCE.
Explain how patients can revoke access and delete their data through [privacy.flexpa.com](https://privacy.flexpa.com).

### Polish your flow

A consent hosting flow that is aesthetically engaging, polished, and reflects your brand conveys the legitimacy and importance of linking an account.
Put your branding on the screens your app shows before and after Flexpa Consent.
Flexpa Consent does not support custom brand colors. You can set its default color scheme (light, dark, or system) in your application settings in the [Flexpa Portal](https://portal.flexpa.com). Patients can still change it with the theme toggle.

### Allow for multiple links

Patients sometimes have multiple payer accounts (primary and secondary insurance, or current and prior insurance).
To capture all of this information, allow patients to link multiple accounts to your app.

## Migration from v1

Flexpa Link will be removed on January 27, 2027. If you use the FlexpaLink SDK, follow the [migration guide](https://www.flexpa.com/docs/consent/migrate) to move to Flexpa Consent. The [v1 documentation](https://www.flexpa.com/docs/consent/v1) covers the FlexpaLink SDK itself.

## Troubleshooting

**Invalid code_challenge**

The code_challenge must be a base64url-encoded SHA256 hash (43 characters, no padding). Verify you're using SHA256 hashing and correct base64url encoding.

**Invalid redirect_uri**

The redirect_uri must exactly match a URI registered in the [Flexpa Portal](https://portal.flexpa.com) including protocol, domain, and path. Check for typos and ensure the URI is registered.

Redirect URIs are registered per mode. A URI registered in test mode is not valid for an authorization request made with a `pk_live_` key, which renders an error page with `invalid_redirect_uri` and "The redirect_uri is not registered for this application mode." Register your production URI in live mode before you cut over. Live mode *web* URIs must use HTTPS and cannot be localhost, a loopback address, or a private IP, so a test mode URI such as `http://localhost:3000` cannot be reused as-is.

A mobile app's custom scheme, such as `yourapp://callback`, is valid in both modes. The HTTPS rule above applies to web URIs, which travel over the network; a custom scheme is handed to your app by the operating system instead. Register the exact value your app receives at runtime, since matching is exact. With `expo-auth-session` that value is `request.redirectUri`, and in Expo Go it is an `exp://` URL that changes with your machine's address, so use a development build to get the stable `yourapp://` scheme.

**Invalid or unknown endpoint_id**

Endpoint IDs are also per mode. A `flexpa_endpoint_id` that resolves in test mode does not resolve in live mode — look the endpoint up again in live mode rather than hard-coding the test mode value. Unlike the redirect_uri error above, this one is delivered as a redirect to your `redirect_uri` carrying `error=invalid_request`, so look for it there rather than on screen.

**flexpa_line_of_business is not supported by this endpoint**

The endpoint named by `flexpa_endpoint_id` does not support the plan type in `flexpa_line_of_business`, or it is a provider, which has no plan type. Check the endpoint's supported lines of business in the [Directory](https://www.flexpa.com/docs/network/directory), or omit `flexpa_line_of_business` so the patient picks their plan type. This error is also delivered as a redirect to your `redirect_uri` carrying `error=invalid_request`.

**Token exchange fails**

Common causes:
- Authorization code expired (30 minute lifetime)
- Code already used (single-use only)
- `redirect_uri` doesn't match the authorization request
- `client_id` (publishable key) doesn't match

**Patient connected a source but no authorization code arrives**

Flexpa issues the authorization code only after the patient finishes the flow with **I'm done, share these records** on the last screen. A patient who has connected a source but has not selected **I'm done, share these records** has not finished. A consent session ends 30 minutes after it starts, and after 5 minutes if the patient is idle on the terms, demographics, identity verification, sign-in, or verification code screens. If the patient has not finished by then, Flexpa issues no authorization code or token. If the patient's browser is still open, it redirects to your `redirect_uri` with `error=access_denied`. If the patient closed the page, no redirect happens, so also set a timeout in your own application. There is nothing to recover, so send the patient through Flexpa Consent again with a new authorization request.

**Consent stays `AUTHORIZED` and never becomes `EXCHANGED`**

The patient completed the flow, but your application never exchanged the authorization code. Usually the callback did not reach your `redirect_uri`, or your application lost the `code_verifier` that it stored before the redirect. The initial sync can still complete, but you do not get an access token or a refresh token. A code cannot be exchanged without its original `code_verifier` or after it expires, so exchange it as soon as the callback arrives. To find these consents, call [List consents](#list-consents) with `state=AUTHORIZED`, then ask those patients to connect again.

**Success:**
For additional help, contact [support@flexpa.com](mailto:support@flexpa.com) with the `X-Request-Id` response header from the failed request.

## Next steps
