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

> ## Agent Instructions
> Sapiom Pay is for platforms whose agent shops for many end users. Each end user is a buyer with their own Sapiom key and saved cards.
> Use the buyer's key for every call made on that buyer's behalf. Never use one buyer's key for another user, and never give the agent the platform key.
> Backends call the REST API at https://api.sapiom.ai/v1. Agents can also call the hosted MCP tools at https://api.sapiom.ai/v1/mcp/pay. Both take the key in an x-api-key header.
> Keys that start with sk_test_ reach the sandbox, a separate test environment with its own buyers and cards, at the same addresses. Use test card numbers only there.
> Never ask a buyer for a card number, expiry date or CVV in chat. Buyers enter card details only on Sapiom's hosted pages.
> Check the merchant before buying. Use purchase.type ucp_checkout only when the merchant check returns supported. Otherwise use browser_checkout.
> Follow the nextAction in each transaction result. Action links are secrets for the intended buyer only.
> Use one idempotency key per transaction. Resume an unanswered create with the same request and key. If a UCP update or cancellation is uncertain, stop and hand off to a person. If a browser cancellation is uncertain, read the transaction and resend the same cancellation, with the same key, only if it is not closed. Never start a new transaction to escape an uncertain outcome.
> For a ucp_checkout purchase, report an order as placed only when order.outcome is confirmed; if it is unknown, stop and hand off to a person. A browser_checkout purchase has no order confirmation (order is null): confirm the order on the merchant's page.

# Pay in the browser

> Attach the Browser Pay SDK to the browser that runs checkout, so the merchant receives the real card while your code only handles a placeholder.

Your browser worker is the code that runs the merchant's checkout in a browser session you control. When a transaction is ready, it fills the card form with a placeholder card. Our Browser Pay SDK catches the request that carries the placeholder and relays it through Sapiom, which sends it on with the buyer's real card. UCP checkout and browser checkout both pay this way.

**You need:**

* A transaction whose `nextAction.type` is `payment_ready`. See [Authorize a transaction](https://docs.sapiom.ai/transactions/authorize.md).
* A Chromium browser your code controls, with a Chrome DevTools Protocol (CDP) endpoint.
* The buyer's key.

## Install

```bash theme={null}
npm install @sapiom/browser-pay
```

## Connect your browser provider

Browser Pay needs the CDP URL of the browser session your worker drives: `ws://`, `wss://`, or an `http(s)://` [discovery URL](https://docs.sapiom.ai/api-reference/browser-pay-sdk.md#cdp-urls). It opens its own connection to that session. Treat the URL as a secret: anyone who has it can control the browser.

| Provider | CDP URL |
| - | - |
| [Anchor](https://docs.sapiom.ai/transactions/browsers/anchor.md) | `data.cdp_url` |
| [Kernel](https://docs.sapiom.ai/transactions/browsers/kernel.md) | `cdp_ws_url` |
| [Browserbase](https://docs.sapiom.ai/transactions/browsers/browserbase.md) | `connectUrl` |
| [Browser Use](https://docs.sapiom.ai/transactions/browsers/browser-use.md) | `cdpUrl` |

Keep the session open until checkout settles. Ending it does not cancel the Sapiom transaction. A browser that connects does not mean its checkout can be paid; see [Which checkouts Browser Pay can pay](#which-checkouts-browser-pay-can-pay).

## Get the placeholder card

[Get the transaction](https://docs.sapiom.ai/api-reference/transactions/get-a-transaction.md) with the buyer's key. When `nextAction.type` is `payment_ready`, the placeholder is in `payment.placeholder`:

```typescript theme={null}
const transaction = await fetch(
  `https://api.sapiom.ai/v1/transactions/${transactionId}`,
  { headers: { "x-api-key": buyerKey } },
).then((res) => res.json());

if (transaction.nextAction.type !== "payment_ready") {
  throw new Error(`Not ready to pay: ${transaction.nextAction.type}`);
}
const placeholder = transaction.payment.placeholder;
```

Each placeholder has a `number`, `expMonth`, `expYear`, `cvc`, `brand`, `expiresAt`, `injectionRef` and `transactionId`. Keep placeholders out of logs and out of your agent's prompt.

Reading the transaction again returns the same placeholder until it is relayed, canceled or expired, or the card is removed. After that, `payment.placeholder` is gone; follow `nextAction` instead.

The placeholder expires about one hour after the buyer approves the transaction. `expiresAt` gives the exact time. Fill the card form before that time. Submit the form before that time. If the placeholder expires before Sapiom sends the card, Sapiom charges nothing.

* If `nextAction.type` is `blocked` with `reason: payment_ended`, cancel the transaction. Then create a new one.
* If `nextAction.type` is `closed` with `reason: payment_expired`, create a new transaction.

## Attach Browser Pay, then fill the form

Attach before typing any card field, and wait for `attachInterceptor` to resolve:

```typescript theme={null}
import { attachInterceptor, httpPaymentRelay } from "@sapiom/browser-pay";

const handle = await attachInterceptor(cdpUrl, {
  relay: httpPaymentRelay({
    baseUrl: "https://api.sapiom.ai",
    apiKey: buyerKey,
    transactionId,
  }),
  placeholder: { number: placeholder.number, injectionRef: placeholder.injectionRef },
});

handle.on((event) => {
  if (event.type === "refused" || event.type === "failed" || event.type === "relayed_unfulfilled") {
    // Do not start a new transaction yet. Read this transaction and follow nextAction.
    // payment_ended: Sapiom did not send the card. payment_submitted: the card can be with the merchant.
  }
});
```

`cdpUrl` is the session's CDP URL, or a Puppeteer browser or page. With Playwright, pass the URL, not the `Browser` or `Page`.

At a Shopify checkout, also pass `profileId: "shopify-card-vault"`, which adds Sapiom's stricter checks for Shopify's card form.

Then pay on the merchant's page:

* **UCP checkout:** open `nextAction.continueUrl`, the merchant's checkout for this transaction. The merchant supplies this URL, and its host can be different from `merchant.origin`. For example, a Shopify store on its own domain gives a `myshopify.com` link that redirects to the store's domain. Do not compare `continueUrl` with `merchant.origin`. Before you fill the card fields, make sure that the page is on `merchant.origin`.
* **Browser checkout:** stay on the checkout page your agent filled in. It must be on the transaction's `merchant.origin`.

Fill the card fields with the placeholder's `number`, expiry and `cvc`, and submit. When checkout finishes, call `await handle.detach()`.

### Fill the card fields

Fill the card fields with your browser driver. Your agent sees the page, but it does not need the card values.

* A Shopify checkout shows each card field in a separate frame. Find each field in its frame, for example with Playwright's `frameLocator`. These frames are the real card fields.
* Some forms have one expiry field. Type the expiry as `MM / YY` in that field.
* Compare the total on the page with the total that the buyer approved. Use the amount as the page shows it. MCP results give `totals` in minor units: `399` is `$3.99`.

If a browser agent fills the form for you, give it the placeholder through the agent's secret values. Do not put the placeholder in its prompt. Give it the same three facts.

If your agent controls checkout, give it a tool that fills the card fields with the placeholder. The tool must not return the placeholder to your agent.

## If the browser session ends before you submit

Your browser session can end before the card form is submitted. For example, the session can time out or the worker can stop. The card was not sent. Do not record the purchase as paid or as uncertain.

1. Read the transaction.
2. If `nextAction.type` is `payment_ready`, start a new browser session. Attach Browser Pay again and pay with the same transaction and placeholder.
   * **UCP checkout:** open `nextAction.continueUrl` again.
   * **Browser checkout:** the new session does not have the old cart. Fill in the checkout again on `merchant.origin`. Before you fill the card fields, compare the page's items and total with the transaction's `purchase`. If they do not agree, do not submit. Cancel the transaction and create a new one.
3. If `nextAction.type` is `blocked` with `reason: payment_ended`, the placeholder expired. [Cancel the transaction](https://docs.sapiom.ai/api-reference/transactions/cancel-a-transaction.md). Then create a new transaction if the buyer still wants it.
4. If `nextAction.type` is `closed` with `reason: payment_expired`, the placeholder expired and Sapiom closed the transaction. Create a new transaction if the buyer still wants it.
5. If `nextAction.type` is `payment_submitted`, the card was sent. Do not pay again. [Confirm the order](https://docs.sapiom.ai/transactions/confirm-and-recover.md).

Do not create a second transaction while the first one reads `payment_ready`.

## Which checkouts Browser Pay can pay

From version 0.3.0, Sapiom relays a card request, for either purchase type, when:

* it goes by `POST` to a public `https` URL, as JSON or form fields
* it comes from a page on the transaction's merchant origin (for a UCP checkout, the page after `continueUrl` redirects)
* the placeholder's number appears in the request body exactly once, and nowhere in the URL or headers

To send the same request on, Browser Pay forwards the checkout's own request headers, including any `Authorization` header. Sapiom records their names, never their values.

Browser Pay can't pay a form that encrypts the card fields in the page, such as Adyen's client-side encryption, because the placeholder never appears in the request. A payment provider may also decline a card sent from Sapiom's card vault rather than from the buyer's browser.

## Check the result

| Event | Meaning |
| - | - |
| `relayed` | The payment processor answered. The order is not confirmed yet. |
| `relayed_unfulfilled` | The relay finished, but the page could not receive the answer. |
| `refused` | Sapiom refused the relay. `reason` says why. |
| `failed` | The request could not be relayed. `reason` says why. |

Whatever the event, the next step is the same: read the transaction and [confirm the order](https://docs.sapiom.ai/transactions/confirm-and-recover.md). No event proves that money did or did not move. The transaction tells you if the card was sent: only `payment_submitted` means that the card can be with the merchant. Options, events and reason codes are in the [Browser Pay SDK reference](https://docs.sapiom.ai/api-reference/browser-pay-sdk.md).


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