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

# Authorize a transaction

> Create a transaction for a merchant's UCP checkout or for a checkout page your agent completes, then follow each nextAction until payment is ready.

A transaction ties one purchase from one merchant to one of the buyer's saved cards. You create it with the buyer's key, Sapiom checks it against your rules and asks the buyer to approve it, and every response says what to do next.

**Before you start:**

* You have the buyer's key. See [Manage buyers](https://docs.sapiom.ai/buyers/create-buyers.md).
* The buyer has a card that is ready to pay. See [Vault a card](https://docs.sapiom.ai/cards/vault-a-card.md).
* Your [browser worker](https://docs.sapiom.ai/transactions/browser-pay) is ready to finish checkout with Browser Pay attached.

## Where each step runs

| Step | Runs in | Uses |
| - | - | - |
| Check the merchant, create, read, change or cancel the transaction | Your backend or agent | REST API, buyer key |
| Send approval links to the buyer | Your agent or app | Links from `nextAction` |
| Fill and submit checkout | Your browser worker | Browser Pay, buyer key |
| Refresh a UCP checkout when asked | Your backend | REST API, buyer key |

Your backend stores each buyer's key and passes it to whatever acts for that buyer.

## Choose the purchase type

How you describe the purchase depends on the merchant. Check it first:

```bash theme={null}
curl -G https://api.sapiom.ai/v1/transaction-compatibility \
  -H "x-api-key: $SAPIOM_BUYER_KEY" \
  -d domain=shop.example
```

> [card] UCP checkout
>
> **When `status` is `supported`.** You send the merchant's UCP checkout, and Sapiom opens it with the merchant. The merchant states the items and total.

> [card] Browser checkout
>
> **For any other merchant.** Your agent completes the merchant's checkout page and tells Sapiom what it's buying and the total.

`unknown` means the check failed, not that the merchant lacks UCP. Check again after `retryAfterSeconds`, or use browser checkout.

## Create the transaction

Send the purchase, the card in `funding`, and your own unique key in an `Idempotency-Key` header. Omit `paymentCardId` to use the buyer's default card.

> [tab] UCP checkout
>
> ```bash theme={null}
> curl -X POST https://api.sapiom.ai/v1/transactions \
>   -H "x-api-key: $SAPIOM_BUYER_KEY" \
>   -H "Idempotency-Key: order-7f3a" \
>   -H "Content-Type: application/json" \
>   -d '{
>     "purchase": {
>       "type": "ucp_checkout",
>       "merchant": { "origin": "https://shop.example" },
>       "checkout": {
>         "currency": "USD",
>         "line_items": [
>           { "item": { "id": "merchant-product-id" }, "quantity": 1 }
>         ]
>       }
>     },
>     "funding": {
>       "source": "external_card",
>       "paymentCardId": "00000000-0000-4000-8000-000000000001"
>     },
>     "shipping": {
>       "address": {
>         "first_name": "Jane",
>         "last_name": "Buyer",
>         "street_address": "1 Market St",
>         "address_locality": "San Francisco",
>         "address_region": "CA",
>         "postal_code": "94105",
>         "address_country": "US"
>       },
>       "contact": { "email": "jane@example.com" }
>     }
>   }'
> ```
>
> * `checkout` is a UCP checkout request. `item.id` is the merchant's product ID. Don't add an `id` to the line item itself.
> * `shipping` is where the order ships and how the merchant reaches the buyer. If nothing ships, such as a digital order, send only `contact`. If you send `shipping`, leave `buyer` out of `checkout`.
> * Sapiom opens the merchant's checkout in the same call, so the response already reflects the merchant's items and total.
> * Only USD is supported. If the merchant's checkout is in another currency, `nextAction` is `blocked` with `reason: unsupported_currency`.

> [tab] Browser checkout
>
> Fill in everything on the checkout page except the card, such as the shipping address and delivery option. Then read the final total from the page and create the transaction:
>
> ```bash theme={null}
> curl -X POST https://api.sapiom.ai/v1/transactions \
>   -H "x-api-key: $SAPIOM_BUYER_KEY" \
>   -H "Idempotency-Key: order-9c21" \
>   -H "Content-Type: application/json" \
>   -d '{
>     "purchase": {
>       "type": "browser_checkout",
>       "merchant": {
>         "origin": "https://store.example",
>         "name": "Example Store"
>       },
>       "total": { "amount": "54.18", "currency": "USD" },
>       "claimedLineItems": [
>         {
>           "title": "Trail running shoes, size 10",
>           "quantity": 1,
>           "unitAmount": "48.00"
>         }
>       ],
>       "pageUrl": "https://store.example/checkout"
>     },
>     "funding": {
>       "source": "external_card",
>       "paymentCardId": "00000000-0000-4000-8000-000000000001"
>     }
>   }'
> ```
>
> * `merchant.origin` is the origin of the page that submits the card. The card is relayed only from that origin.
>
> * `total` is the full amount the page will charge, including shipping and tax. Your spending limits and the buyer approve this amount. Sapiom can't hold the merchant to it. Only USD is supported: another currency returns `400` with `code: invalid_purchase`.
>
> * The buyer sees `merchant.name` and each item's `title` and `quantity` when they approve.
>
> * Don't send `shipping`: it is refused. Enter the address on the page.
>
> > [!WARNING]
> >
> > Sapiom can't see the checkout page, so it doesn't check your agent's statement against the merchant. Once the card is relayed, the merchant charges what its checkout says. Read the total from the page immediately before you create the transaction. If the page's total changes, don't submit it: cancel the transaction and create a new one.

The response includes the transaction's `id`. Save it: every later step uses it. All fields are in [Create a transaction](https://docs.sapiom.ai/api-reference/transactions/create-a-transaction.md).

## Keep the same idempotency key

The `Idempotency-Key` header identifies this transaction. Replacing a UCP checkout and canceling take the same header, each with its own key. To resume an unanswered create, send the identical request with the same key. A key used for a different request returns `409 idempotency_conflict`.

An uncertain update or cancellation is different: check [Recover without buying twice](https://docs.sapiom.ai/transactions/confirm-and-recover.md#recover-without-buying-twice) before you resend it. Never start another transaction to escape an uncertain outcome.

The key is required for UCP checkout. Browser checkout accepts a request without one, but send one anyway.

## Follow nextAction

Every response has a `nextAction` object. Its `type` says what to do, and its `actor` says who does it. Act on it, then [get the transaction](https://docs.sapiom.ai/api-reference/transactions/get-a-transaction.md) for the next one. To wait for a change instead of polling, pass `maxWaitSeconds` (up to 25) and your last `version` as `since`:

```bash theme={null}
curl -G "https://api.sapiom.ai/v1/transactions/$TRANSACTION_ID" \
  -H "x-api-key: $SAPIOM_BUYER_KEY" \
  -d maxWaitSeconds=20 \
  -d since="$VERSION"
```

| `type` | `actor` | What to do |
| - | - | - |
| `wait` | `system` | Sapiom is still working. Read again after `retryAfterSeconds`. |
| `buyer_security_code` | `buyer` | Send the buyer the `url`. They approve by re-entering their CVV before `expiresAt`. |
| `approval` | `approver` | An approval rule on your account applies. Send the approver the `url` before `expiresAt`. |
| `payment_ready` | `agent` | Pass the transaction `id` to your browser worker. See [Pay in the browser](https://docs.sapiom.ai/transactions/browser-pay). |
| `payment_submitted` | `none` | The card went to the merchant's payment processor. Stop reading and never pay again. For a UCP checkout, [get the confirmation](https://docs.sapiom.ai/transactions/confirm-and-recover.md#show-the-order-confirmation). |
| `blocked` | `none` | Stop and tell the buyer why, from `reason` and `messages`. If `reason` is `payment_ended`, the payment ended before Sapiom sent the card, so Sapiom charged nothing. Cancel the transaction. Then create a new transaction if the buyer still wants it. |
| `closed` | `none` | Stop and tell the buyer why, from `reason` and `message`. |
| `none` | `none` | The checkout is complete. Nothing is left to do. |

A UCP checkout can also return these, because Sapiom is talking to the merchant:

| `type` | `actor` | What to do |
| - | - | - |
| `update_checkout`, `revise` | `agent` | The checkout needs a change, explained in `messages` or `reasons`. [Change it](#change-or-cancel-a-transaction). |
| `merchant_escalation` | `buyer` | The merchant needs the buyer. Send them the `url` and `messages`. |
| `allowance_verification` | `buyer` | Send the buyer the `url` before `expiresAt`. |
| `resend_request` | `agent` | After `retry.afterSeconds`, send the original request again with the same key. |
| `reconcile_checkout` | `human` | Your backend refreshes the checkout. See [Refresh the checkout](https://docs.sapiom.ai/transactions/confirm-and-recover.md#refresh-the-checkout). |
| `closed` | `agent` | The checkout never opened. Correct the request and send it with a new key. |

## Change or cancel a transaction

* **Change a UCP checkout:** [replace the checkout](https://docs.sapiom.ai/api-reference/transactions/replace-the-checkout.md) with `PUT /v1/transactions/{id}/ucp/checkout`, an `Idempotency-Key` header and the full checkout, built from the latest one plus your change. Changing items, the total or the delivery address can mean the buyer approves again, even at the same total. [Follow `nextAction`](#follow-nextaction) again.
* **Change a browser checkout:** you can't. Cancel it and create a new transaction with a new key.
* **Cancel:** call [Cancel a transaction](https://docs.sapiom.ai/api-reference/transactions/cancel-a-transaction.md). For a UCP checkout, Sapiom cancels the merchant's checkout first. For a browser checkout, cancel returns `409 card_relayed` after your agent pays. Cancelling never refunds an order.

## Use the MCP tools

An agent connected to the [hosted MCP server](https://docs.sapiom.ai/buyers/connect-agent.md) can use `sapiom_request_transaction`, `sapiom_get_transaction`, `sapiom_update_transaction` and `sapiom_cancel_transaction`. They return the same `nextAction` values, and their descriptions say how to call them. See the [MCP tools reference](https://docs.sapiom.ai/api-reference/mcp-tools.md).

> [accordion] nextAction on the /ucp/checkout routes
>
> The `/ucp/checkout` routes use their own `nextAction.action` names. Get a transaction returns the `type` names above.
>
> | `/ucp/checkout` `nextAction.action` | `nextAction.type` |
> | - | - |
> | `update` | `update_checkout` |
> | `requires_action` | `buyer_security_code`, `approval`, `allowance_verification` or `merchant_escalation`, by `kind` |
> | `hand_off` | `payment_ready` |
> | `submitted` | `payment_submitted` |
> | `refresh` | `reconcile_checkout` |
> | `done` | `none` |
>
> `wait`, `revise`, `blocked` and `closed` have the same names.


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