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

# How to integrate Vault SDK

> Configure the platform boundary around Agent Vault SDK without rereading the SDK payment guide.

This page covers only the platform adaptation layer around Agent Vault SDK: server credentials, user and card associations, browser boundaries, and rollout checks. Vault SDK can coexist with Browser Pay on the same platform, but choose one path before each payment attempt starts. The SDK state machine and PSP behavior are not repeated here. For `attach()`, `pay()`, `reconcile()`, states, errors, and complete code, see the [Agent Vault SDK guide](https://docs.clinkbill.com/guides/agent/vault_sdk) and [SDK API reference](https://docs.clinkbill.com/guides/agent/vault_sdk/api_reference.md).

> [!NOTE]
>
> Vault SDK does not require end-user OAuth and does not connect to a user's MCP. The platform server calls Vault with Clink application credentials through OAuth `client_credentials`; the user participates only when hosted card setup or merchant verification requires it.

## 1. Provision credentials and confirm coverage

The SDK runs in a trusted platform service or checkout worker that controls a Playwright or CDP browser. The Agent and merchant page can see only the placeholder card provided by the SDK.

Vault SDK uses a dedicated Vault `client_id` and `client_secret` issued through Pre-registration; see [How to get platform credentials](https://docs.clinkbill.com/guides/agent/platform_credentials.md) for how to request them. When you request them, also describe your target merchants or PSP scenarios.

| Item | Platform requirement |
| - | - |
| Credentials | A dedicated Vault `client_id` and `client_secret` with the `vault:session vault:pay vault:read` scopes; do not mix them with the platform client. The SDK obtains and refreshes application tokens automatically through `client_credentials`. |
| Environment | `sandbox` for UAT (`https://uat-api.clinkbill.com`) and `production` for live traffic (`https://api.clinkbill.com`). Store credentials and card associations separately for each. |
| Browser boundary | The platform owns navigation, product selection, shipping details, and reading the merchant result; the SDK attaches to the checkout page and supported PSP requests. |
| Coverage | `vault.getCoverage()` lists only the PSPs enabled in the current environment; it does not list merchants. Confirm target merchants with the coverage list Clink provides, and use `checkout.getPaymentMethods()` to check a specific checkout page. |

Keep the Client Secret, `cardToken`, and server-side card associations only on the trusted platform server. Do not expose them to a model, Agent tool, browser storage, logs, or user-facing messages.

## 2. Install the SDK and initialize the client

The runtime requires Node.js `>=20.19` (ESM-only). When using a Playwright `Page`, Playwright `>=1.40` is also required; it is not needed if you use only raw CDP.

```bash theme={null}
npm install @clink-ai/agent-vault playwright
```

```typescript theme={null}
import { ClinkVault } from '@clink-ai/agent-vault';

const vault = new ClinkVault({
  clientId: process.env.CLINK_CLIENT_ID!,
  clientSecret: process.env.CLINK_CLIENT_SECRET!,
  environment: 'sandbox', // Use 'production' for the production environment
});
```

The SDK uses these credentials to obtain and refresh application tokens automatically through `client_credentials`; the platform does not need to call the OAuth endpoint itself. Create separate clients for UAT and Production, and do not share credentials between them.

## 3. Plan the user and card data to store

Use the platform's stable user ID as `externalUserId`. Do not use an email address as the primary key; when a user changes email, a card must not be silently associated with another platform user.

| Data | Platform responsibility |
| - | - |
| `externalUserId` | Stable platform user identifier passed to Vault. Keep a separate association for each tenant and environment. |
| `email` | Verified email required to create a hosted card setup session when no usable card exists. It is changeable user data. |
| `customerId` | Vault customer association, returned by `sessions.create()` and `onCardBound`. Save it per platform user and environment after card setup completes. |
| `cardToken` | Card token stored on the server and passed back to `attach()`; never send it to the Agent or the merchant page. |
| `sessionId` and `url` | Short-lived hosted card setup context. The creation response is not a usable card credential; deliver `url` to the user unchanged. |
| `window.expiresAt` | Payment window deadline returned with a usable card. The payment window is 30 minutes; rely on the returned value. After it expires, handle it as a new user-action branch. |

Confirm with Clink how `externalUserId`, email, and `customerId` are matched when a user changes email, deletes or freezes a card, or merges accounts. Do not infer these rules from the SDK method names.

Card setup sessions and payment windows are Vault's own hosted flows: they are not user OAuth and cannot access other wallet data, and a payment window is not a Browser Pay purchase authorization (Instruction). When one platform enables both paths, manage credentials, permissions, user actions, card identifiers, and recovery separately for each path, and do not let both paths handle the same checkout at the same time.

## 4. Bind a card or reuse an existing card

For a returning user, load the saved `customerId` and `cardToken` from the server. For first use or an expired payment window, pass `email` to `attach()`: `pay()` returns `wait_user` / `bind_card` with a hosted card setup link, and `onCardBound` returns the card association once card setup completes. You can also create a card setup session before checkout and read the result after the user finishes:

```typescript theme={null}
const session = await vault.sessions.create({
  email: currentUser.email,
  externalUserId: currentUser.id,
});
await deliverUserAction(currentUser.id, { type: 'bind_card', url: session.url }); // deliver url unchanged

// The user completes card setup from the link; poll until the session ends
let result = await vault.sessions.retrieve(session.sessionId);
while (result.status === 'pending' || result.status === 'opened') {
  await new Promise(resolve => setTimeout(resolve, 5_000));
  result = await vault.sessions.retrieve(session.sessionId);
}
if (result.status === 'completed' && result.cardToken) {
  await saveVaultCard(currentUser.id, {
    customerId: session.customerId, // returned by sessions.create()
    cardToken: result.cardToken,
  });
}
```

The `sessionId` and `url` returned on creation are not card credentials. Save `customerId` and `cardToken` only after the session completes: `customerId` comes from `sessions.create()`, and `cardToken` from the completed session. When deciding whether a saved association can be reused, rely on the latest `expiresAt` and card status returned by the SDK.

## 5. Attach the platform browser and complete the payment

Each checkout maps to one platform-controlled browser task and one checkout controller. Pass the platform user association and implement callbacks that deliver user actions and read the merchant result:

```typescript theme={null}
const checkout = await vault.attach(page, {
  customerId: savedCard?.customerId,
  cardToken: savedCard?.cardToken,
  email: currentUser.email,
  externalUserId: currentUser.id,
  onUserAction: action => deliverUserAction(currentUser.id, action),
  onCardBound: card => saveVaultCard(currentUser.id, card),
  resolveMerchantResult: () => readMerchantOrderResult(page),
});
```

With raw CDP, call `vault.attachToCdp()`. Do not share a controller, page session, `customerId`, or `cardToken` across platform users. Close or detach the current controller when the checkout ends.

During `checkout.pay()`, first-time card setup and an expired payment window both return the same `wait_user` / `bind_card` action. The platform should:

* deliver the hosted card setup link to the user unchanged;
* wait for the user to add a card or re-enter the CVV;
* save the `customerId` / `cardToken` returned by `onCardBound`;
* continue the same checkout with the SDK placeholder card.

3DS, QR code, and redirect actions belong to the current merchant browser or the user's device. The platform delivers them as returned by the SDK, and follows the [Agent Vault SDK guide](https://docs.clinkbill.com/guides/agent/vault_sdk) to continue and reconcile each payment method.

## 6. Read the merchant result and reconcile

The platform reads the merchant's order result through `resolveMerchantResult`; a PSP tokenization response or SDK status alone does not prove that the order completed. Use `reconcile()`, and follow the [Agent Vault SDK guide](https://docs.clinkbill.com/guides/agent/vault_sdk) to distinguish completed, failed, pending, user-action, and unknown outcomes.

While the merchant result is pending or requires user action, keep the same checkout controller and call `reconcile()` again once the merchant page shows a result. When the result is unknown, verify the original order before doing anything else; do not switch cards or start a second payment. Call `retry()` only after confirming that no charge occurred, and call it before `detach()`. Call `detach()` when the checkout ends; it releases interception resources and does not cancel requests already sent.

Store the merchant order number separately from Vault identifiers. The result shown to the user should be based on the merchant result, not only on a PSP response or the result reported to Clink.

## Related docs

* [How to get platform credentials](https://docs.clinkbill.com/guides/agent/platform_credentials.md): request a Vault `client_id` and `client_secret`.
* [Agent Vault SDK guide](https://docs.clinkbill.com/guides/agent/vault_sdk): SDK behavior, payment states, PSP coverage, and complete examples.
* [SDK API reference](https://docs.clinkbill.com/guides/agent/vault_sdk/api_reference.md): methods, options, return values, and error codes.
* [How to integrate MCP](https://docs.clinkbill.com/guides/agent/platform_integration/mcp_integration.md): integrate Browser Pay through MCP + OAuth.
* [How to integrate RESTful API](https://docs.clinkbill.com/guides/agent/platform_integration/restful_api.md): integrate Browser Pay through the Customer Portal API.


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