> ## Documentation Index
> Fetch the complete documentation index at: https://docs.withbenji.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Initialize the Benji Connect SDK

> Importing and initializing the Benji Connect SDK to start connecting your users to their loyalty platforms.

## Installing the Benji Connect SDK

The **Benji Connect SDK** can be added to your application by including the SDK script in your HTML head.

```html theme={null}
<script src="https://d1ochlq3jv1msw.cloudfront.net/connect/sdk/connect-sdk.js"></script>
```

or by installing the package via npm

```bash theme={null}
npm install @benji-money/connect-sdk
```

or by installing the package via yarn

```bash theme={null}
yarn add @benji-money/connect-sdk
```

or by installing the package via pnpm

```bash theme={null}
pnpm add @benji-money/connect-sdk
```

> [!WARNING]
>
> Note that you will be able to access the above packages/scripts once
> authorized to use the Benji SDKs. Contact your account manager for additional
> info.

## How the Benji Connect SDK loads Connect

When you call **`open()`**, the **Benji Connect SDK** creates a modal overlay and loads the Connect experience inside it.

## Initializing the Benji Connect SDK

The **Benji Connect SDK** is initialized with the following parameters:

**environment** `string` *required*

Environment for the Connect flow: `development` | `sandbox` | `production`.

**token** `string` *required*

Connect token from the [Create Token](https://docs.withbenji.com/connect/create_token.md) API.

**onSuccess** `callback function`

Called when the flow completes successfully (`FLOW_SUCCESS`). Receives the
exchange token and [metadata](https://docs.withbenji.com/connect/methods.md#onsuccess).

**onExit** `callback function`

Called when the user leaves the Connect UI (`FLOW_EXIT`), whether or not
`onSuccess` ran.

**onError** `callback function`

Called when an error is reported from the Connect flow (`ERROR`).

**onEvent** `callback function`

Called for mid-flow and auxiliary events (e.g. `AUTH_SUCCESS`). See [Methods &
Events](https://docs.withbenji.com/connect/methods.md#onevent).

Types are **optional**. The same runtime behavior applies to JavaScript and TypeScript; TypeScript users can import types from `@benji-money/connect-sdk` for autocomplete.

```javascript JavaScript theme={null}
import { ConnectSDK } from "@benji-money/connect-sdk";

let sdk = null;

function initializeSDK(token) {
  sdk = new ConnectSDK({
    environment: "sandbox",
    token: token,

    onSuccess: (token, metadata) => {
      console.log("Benji Connect successful");
      // Exchange `token` via Exchange Token API; use metadata for UI
    },

    onError: (error, errorId, metadata) => {
      console.log("Benji Connect onError", error, errorId);
    },

    onExit: (metadata) => {
      console.log("Benji Connect onExit", metadata.trigger);
    },

    onEvent: (type, metadata) => {
      console.log("Benji Connect onEvent", type);
    },
  });
}
```

```typescript TypeScript theme={null}
import {
  ConnectSDK,
  type BenjiConnectOnSuccessMetadata,
  type BenjiConnectMetadata,
  type BenjiConnectOnExitMetadata,
  type BenjiConnectEventType,
} from "@benji-money/connect-sdk";

let sdk: ConnectSDK | null = null;

function initializeSDK(token: string): void {
  sdk = new ConnectSDK({
    environment: "sandbox",
    token,

    onSuccess: (token: string, metadata: BenjiConnectOnSuccessMetadata) => {
      console.log("Benji Connect successful", token, metadata.action);
    },

    onError: (
      error: Error,
      errorId: string,
      metadata: BenjiConnectMetadata,
    ) => {
      console.log("Benji Connect onError", error, errorId);
    },

    onExit: (metadata: BenjiConnectOnExitMetadata) => {
      console.log("Benji Connect onExit", metadata.trigger);
    },

    onEvent: (type: BenjiConnectEventType, metadata: BenjiConnectMetadata) => {
      console.log("Benji Connect onEvent", type);
    },
  });
}
```

To open the UI after initialization, call **`await sdk.open()`** (see [Methods & Events](https://docs.withbenji.com/connect/methods.md#open)).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.