CareflowDocumentation

Frontend Embed SDK

The Careflow Embed SDK connects an application running in an iframe to its Careflow host. Use it to receive bootstrap metadata, including session information supplied by the host, and exchange messages with Careflow.

The SDK is optional. Your application can manage its own login and sessions, or integrate the SDK to use the Careflow session.

Install the SDK

Install @careflow/embed-sdk from a built SDK distribution containing package.json and dist/:

sh
pnpm add file:/absolute/path/to/embed-sdk

The path must point to the distribution package, rather than the SDK source directory with workspace dependencies.

Connect to Careflow

Initialize the SDK in browser code when your application loads inside Careflow. Import the client from @careflow/embed-sdk/client to create an instance owned by your application:

ts
import { SdkMessagingClient } from '@careflow/embed-sdk/client';
 
async function connectToCareflow() {
  if (window.parent === window) {
    throw new Error('Open this application inside Careflow.');
  }
 
  const sdk = new SdkMessagingClient();
 
  try {
    await sdk.requestHandshake();
    const metadata = await sdk.requestBootstrap();
    return { sdk, metadata };
  } catch (error) {
    sdk.destroy();
    throw error;
  }
}

Call connectToCareflow() from your application's browser startup code. Handle connection errors in the application UI. In a server-rendered application, create the client only after entering the browser lifecycle.

The handshake establishes communication with the host. It does not validate a user session or guarantee that bootstrap metadata has arrived. requestBootstrap() requests that metadata and resolves to the data directly.

By default, the SDK detects the parent origin from document.referrer. If your application's referrer policy prevents that, supply the configured Careflow host origin explicitly:

ts
const sdk = new SdkMessagingClient({
  targetOrigin: 'https://careflow.example.com',
});

Use the actual host origin, including the protocol and port when applicable. targetOrigin is an origin, without the application's deployment path. It must match the parent window; '*' is not a supported wildcard for receiving messages.

Use the Careflow session

Bootstrap metadata is supplied by the host and can contain fields such as userId and sessionToken. Validate the fields your application requires before using them.

When using the Careflow session, send the session information to your application backend for session validation and authorization before displaying protected content or returning protected data. Receiving bootstrap metadata alone does not authenticate the user to your backend. Keep application client secrets on the backend.

Backend services can use the session APIs in the Python SDK or .NET SDK.

Receive metadata updates

Listen for CORE_BOOTSTRAP_UPDATE on the connected client to receive updated metadata:

ts
sdk.on('CORE_BOOTSTRAP_UPDATE', (message) => {
  const updatedMetadata = message.data;
  // Update application state and repeat session validation as needed.
});

Here, sdk is the client returned by connectToCareflow(). Your application owns how updated session information affects its authenticated state.

Angular integration

Angular applications can use the optional wrapper from @careflow/embed-sdk/angular. The consuming application must provide @angular/core and rxjs.

ts
import { inject, provideAppInitializer } from '@angular/core';
import { EmbedSdkService, provideEmbedSdk } from '@careflow/embed-sdk/angular';
 
export const appConfig = {
  providers: [
    provideEmbedSdk(),
    provideAppInitializer(() => inject(EmbedSdkService).initialize()),
  ],
};

EmbedSdkService exposes the messaging client, isEmbedded, and a readonly bootstrapData() signal. Initialization waits up to five seconds for bootstrap metadata when embedded. Outside an iframe, initialization is skipped and metadata remains null.

The wrapper does not validate application-specific metadata or authorize users. Perform your application's backend session checks after initialization. The application injector destroys the client when it is disposed.

Clean up the client

For clients your application creates directly, call destroy() when the application or owning component is disposed:

ts
sdk.destroy();

Use one client at the application boundary. Avoid creating a client for every child component or also importing the runtime singleton from @careflow/embed-sdk when you already own a client.