> ## 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.

# Browser Pay SDK reference

> Exports, options, events and reason codes for the @sapiom/browser-pay package.

`@sapiom/browser-pay` intercepts the card request that carries a Sapiom placeholder and relays it through Sapiom. It needs Node.js 18 or later. For a walkthrough, see [Pay in the browser](https://docs.sapiom.ai/transactions/browser-pay).

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

## attachInterceptor(target, options)

Connects to the browser and starts intercepting. Resolves to an `InterceptorHandle` once interception is active. Fill card fields only after it resolves.

`target` is one of:

* a CDP WebSocket URL, `ws://` or `wss://`
* a DevTools discovery URL, `http://` or `https://`, such as `http://localhost:9222`
* an object with `wsEndpoint()`, such as a Puppeteer browser
* a Puppeteer page, through `browser().wsEndpoint()`
* a custom object whose `browser()` or `context().browser()` returns an object with `wsEndpoint()`

Browser Pay opens its own CDP connection, so `wsEndpoint()` must return a CDP URL. A Puppeteer browser launched with `pipe: true` has none. Playwright's `Browser` and `Page` have no `wsEndpoint()`, and `BrowserServer` returns a Playwright protocol URL. With Playwright, pass the CDP URL here and to `chromium.connectOverCDP()`.

### CDP URLs

Discovery URLs need `@sapiom/browser-pay` 0.4.0 or later.

Browser Pay uses a `ws://` or `wss://` URL as is. For an `http://` or `https://` URL, it reads `webSocketDebuggerUrl` from `json/version/` under the URL's path, with its query string, and connects there.

* `webSocketDebuggerUrl` must be `ws://` or `wss://`, and `wss://` if discovery used `https://`.
* Redirects are not followed.
* A path ending in `/devtools/browser/<id>` or `/devtools/page/<id>` skips discovery. Give Playwright the `ws://` or `wss://` form of such a URL, because `connectOverCDP()` treats every `http(s)` URL as discovery.
* Other schemes are rejected.

Trust the discovery endpoint as much as the browser, because it decides where Browser Pay connects. See [Connect your browser provider](https://docs.sapiom.ai/transactions/browser-pay#connect-your-browser-provider).

### AttachOptions

| Option | Type | Notes |
| - | - | - |
| `relay` | `PaymentRelay` | Usually `httpPaymentRelay(...)`. |
| `placeholder` | `HeldPlaceholder` | `{ number, injectionRef }` from the placeholder for this transaction. |
| `profileId` | `string` | Optional. `shopify-card-vault` at a Shopify checkout. Leave it out elsewhere. |
| `timeoutMs` | `number` | Total time to wait for a final relay outcome. Default 55,000. |
| `attemptTimeoutMs` | `number` | Time before one relay attempt is aborted and retried. Default 25,000. |
| `logger` | `PayLogger` | Receives warnings. Defaults to discarding them. |

### InterceptorHandle

| Member | Notes |
| - | - |
| `on(listener)` | Subscribes to events. Returns an unsubscribe function. |
| `detach()` | Stops intercepting. Fails any request still paused, so call it only after checkout settles. |

## httpPaymentRelay(options)

Builds a relay that posts to `POST /v1/transactions/{transactionId}/payment/relay`.

Without a `profileId`, the relay forwards the checkout request's headers, including `Authorization`, but not `cookie`, `host` or the browser's `sec-*` headers. Sapiom records header names, never values.

| Option | Type | Notes |
| - | - | - |
| `baseUrl` | `string` | `https://api.sapiom.ai`, without `/v1`. |
| `apiKey` | `string` | The buyer key that owns the transaction. Sent as `x-api-key`. |
| `transactionId` | `string` | The ID of the transaction being paid. |
| `fetchImpl` | `typeof fetch` | Optional custom fetch. |

## Events

| `type` | Fields | Meaning |
| - | - | - |
| `paused` | `url`, `method`, `targetType` | A card request was paused for relay. |
| `relayed` | `url`, `status` | The processor's response reached the page. |
| `relayed_unfulfilled` | `url`, `status` | The relay returned, but the page could not receive the response. |
| `refused` | `url`, `reason: RefusalReason`, `requestId?: string` | Sapiom refused the relay. |
| `failed` | `url`, `reason: FailureReason` | The request could not be relayed. |

Event URLs contain only the origin.

## Retries

Timeouts, network errors, HTTP 502, 503 and 504, and `relay_in_progress` are retried with the same idempotency key until `timeoutMs`. Any other refusal fails the request. A relay that returns `outcome: "unknown"` is never sent again.

## RefusalReason

`unknown_injection`, `injection_expired`, `payment_released`, `key_rotated`, `already_relayed`, `relay_in_progress`, `relaying_under_other_key`, `card_removed`, `card_unavailable`, `payment_not_authorized`, `total_changed`, `destination_not_allowed`, `card_fields_invalid`, `merchant_mismatch`, `provider_unavailable`, `merchant_unavailable`, `payments_stopped`, `sapiom_error`. Each reason's meaning is in [Relay refusals](https://docs.sapiom.ai/api-reference/errors.md#relay-refusals).

The `reason` is `unrecognized` if the relay's reason does not match `^[a-z]+(_[a-z]+)*$` or has more than 48 characters. Browser Pay shows all other reasons from the relay without change.

## FailureReason

`intercept_unavailable`, `unknown_session`, `unreadable_body`, `detached`, `page_origin`, `unknown`, `timeout`, `network`, `response_credential`, `headers_unrelayable`.

Each reason's meaning is in [Browser Pay failures](https://docs.sapiom.ai/api-reference/errors.md#browser-pay-failures).

## Other exports

Types: `AttachOptions`, `InterceptorEvent`, `InterceptorHandle`, `InterceptorTarget`, `PaymentRelay`, `RelayCallOptions`, `RelayRequest`, `RelayResponse`, `HeldPlaceholder`, `PayLogger`, `HttpRelayOptions`, `FailureReason`, `RefusalReason`.


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