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

# Creating a cart

> Attach Agentcard to the browser your agent already uses, then let it shop.

Your agent builds the cart the way it already does: in a real browser, on the merchant's own website. Agentcard does not change how the agent shops. It attaches to the browser and waits for the moment the checkout form sends the card to the payment processor.

There is one rule: **attach before the agent reaches the payment form.** The SDK has to be watching when the page tries to send the card.

## Choose a browser

The SDK attaches to any Playwright Chromium page, which means it works with:

* **[KERNEL](https://www.kernel.sh)**: connect over `cdp_ws_url`. KERNEL also ships a [native Agentcard integration](https://www.kernel.sh/docs/integrations/wallets/agentcard) that needs no SDK. Pick one per checkout.
* **[Browserbase](https://www.browserbase.com)**: connect over the session's `connectUrl`.
* **[Browser Use](https://browser-use.com)**: connect over the browser's `cdpUrl`.
* **[Anchor](https://anchorbrowser.io)**: get the connected Playwright browser with `const { browser } = await createBrowser()`; attach to its first page.
* **Any CDP browser**: supply its CDP URL.

Your agent keeps control of the browser during approval and afterward.

## Install

```bash theme={null}
npm i @agent-cards/sdk playwright-core
# plus your browser provider, e.g. @onkernel/sdk
```

You need your Agentcard `client_id` and `client_secret` on your server, and the `user_id` of the person whose card will pay (from [Adding a card](https://docs.agentcard.com/vault/adding-a-card.md)).

## Attach to the page

```ts theme={null}
import { chromium } from 'playwright-core';
import { VaultClient, attachToPlaywright } from '@agent-cards/sdk';

const vault = new VaultClient({
  clientId: process.env.AGENTCARD_CLIENT_ID!,
  clientSecret: process.env.AGENTCARD_CLIENT_SECRET!,
});
await vault.syncRegistry(); // pulls the current processor list; safe on every run

const browser = await chromium.connectOverCDP(CDP_URL);
const context = browser.contexts()[0];
// Keep the store from starting a service worker, before any page loads.
await context.addInitScript(
  'try { if (navigator.serviceWorker) navigator.serviceWorker.register = async () => undefined; } catch {}',
);
const page = context.pages()[0] ?? (await context.newPage());

const checkout = await attachToPlaywright(page, {
  vault,
  user: 'usr_8f3k2m',
  merchant: 'shop.example.com',
  amount: 2306,
  currency: 'usd',
  onApprovalUrl: (url) => sendToUser(url),
});

// Now let the agent shop.
await page.goto('https://shop.example.com');
await runAgentShopping(page);
```

**user** `string` *required*

The Agentcard user whose vaulted card pays.

**merchant** `string` *required*

Shown on the approval screen. Pass what the user would recognize.

**amount** `number | string`

Your hint at the amount: an integer in the currency's smallest unit (2306 for \$23.06), or a decimal string in normal units (`"23.06"`). Pass with `currency`. Caps use the amount the processor charges. Send `amount` to have a purchase judged the moment your agent opens it.

**currency** `string`

ISO 4217 code for `amount`. Required with it.

**onApprovalUrl** `function`

Called with the approval link the moment the payment pauses. Deliver it to the user in your own thread or app.

## Block service workers

Add the init script to the context before your agent opens the store, as the sample above does:

```ts theme={null}
await context.addInitScript(
  'try { if (navigator.serviceWorker) navigator.serviceWorker.register = async () => undefined; } catch {}',
);
```

A store's call to register a service worker then returns nothing, and no worker starts in the session.

A service worker sends the page's requests itself, and Playwright cannot pause a request a service worker sends. The card request is one of them. `attachToPlaywright` refuses a page whose context already runs a store's service worker and throws:

```text theme={null}
Service workers are active; use a checkout context created with serviceWorkers: "block".
```

Playwright's own `serviceWorkers: 'block'` option does the same job, but only on a context you create with `browser.newContext()`. A cloud browser such as Browserbase, KERNEL, Anchor or Browser Use hands you a default context it already made, and a new context has none of the session's saved logins. The init script blocks service workers on the context you already have.

Add the script before the first navigation. `attachToPlaywright` checks for service workers only when you attach, so a worker a store starts later goes unnoticed, and the SDK cannot pause the card request.

Most stores load and check out as usual with service workers blocked. The store's offline pages and push prompts stay off for the session, and a store that runs its third-party tags through Partytown runs them on the page instead of in a worker.

A store whose page waits for its service worker to start, through `navigator.serviceWorker.ready`, waits forever with the block on. Check out on a store like that through CDP with `serviceWorkers: 'bypass'`, which lets the worker start and still pauses the card request.

A saved browser profile keeps the service workers an earlier session started, and the script cannot remove them. Start checkout sessions without a saved profile, or attach through CDP with `serviceWorkers: 'bypass'`, as [Your own browser](https://docs.agentcard.com/vault/integrations/agent-browsers/your-own.md#level-2-you-speak-cdp-directly) shows. `attachToPlaywright` ignores the `serviceWorkers` option, so `bypass` needs `attachToCdp`.

## Two things that break attachment

* **Service workers.** Block them before the store's first page loads, as [Block service workers](#block-service-workers) shows.
* **Attaching too late.** If the agent has already submitted the payment form, there is nothing to pause. Attach right after you get the page, before navigation.

## Don't want a browser?

Agentcard's [Commerce SDK](https://docs.agentcard.com/vault/integrations/ecommerce-apis/purchase-api.md) builds the cart and completes the checkout for you on the merchants it covers (Amazon, Walmart, Target, DoorDash, and more). No browser to run, same passkey approval for the user.


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