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

# Confirm the order and recovery

> Read the transaction until the card is sent, confirm the order with the merchant, refresh a UCP checkout when asked, and retry without buying twice.

After checkout submits, read the transaction until `nextAction.type` is `payment_submitted` or `order.outcome` is `confirmed`. Then show the buyer the merchant's confirmation.

## Read the transaction

```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"
```

`maxWaitSeconds` (up to 25) holds the call open until something changes. `since` is the `version` from your last read. Reading never pays. It asks the merchant only while the merchant is completing the order. An agent on the MCP tools reads with `sapiom_get_transaction`.

## After the card is sent

Once the card reaches the merchant's payment processor, `nextAction.type` is `payment_submitted`. Your agent stops reading and never pays again for this transaction. For a UCP checkout, your backend [gets the confirmation](#show-the-order-confirmation).

## When you can buy again

If the transaction reads `payment_submitted`, or its `order.outcome` is `confirmed` or `unknown`, the card can be with the merchant. Never buy again for it. A card payment that reads `blocked` with `reason: payment_ended` ended before Sapiom sent the card.

| What the transaction reads | Did Sapiom send the card? | What to do |
| - | - | - |
| `nextAction.type` is `payment_submitted`, or `order.outcome` is `confirmed` or `unknown` | Possibly | Never pay or buy again for this transaction. Confirm the order with the merchant. |
| `nextAction.type` is `blocked` with `reason: payment_ended` | No | Sapiom charged nothing. [Cancel the transaction](https://docs.sapiom.ai/api-reference/transactions/cancel-a-transaction.md). Then create a new transaction with a new key if the buyer still wants it. |
| `nextAction.type` is `closed` with `reason: payment_expired` | No | Sapiom charged nothing and closed the transaction. Create a new transaction with a new key if the buyer still wants it. |
| `nextAction.type` is `closed` | See `message` | Follow `message`. It tells you if a new transaction is possible. |

The most frequent cause of `payment_ended` is an expired approval. The placeholder card works for about one hour after the buyer approves. Get the approval when your browser worker is ready to pay.

## Order outcome

For a UCP checkout, `order.outcome` comes from the merchant's checkout. After `payment_submitted`, it changes only when your backend refreshes the checkout.

| `order.outcome` | Meaning | What to do |
| - | - | - |
| `confirmed` | The merchant completed the checkout. | Tell the buyer the order is placed and [show the confirmation](#show-the-order-confirmation). |
| `not_confirmed` | No order yet. | Follow `nextAction`. |
| `unknown` | The order may or may not exist. | Keep this transaction and follow `nextAction`. Never buy again. |

A browser checkout has no order outcome: `order` is `null`. The merchant's page is the only confirmation, so tell the buyer what it showed.

## Show the order confirmation

For a UCP checkout, the merchant's completed checkout is the order confirmation. After `payment_submitted`:

1. [Refresh the checkout](#refresh-the-checkout) every 5 seconds or more until `order.outcome` is `confirmed`.
2. Read the transaction.
3. Show the buyer what's in `order`.

| Field | What it is |
| - | - |
| `label` | Order number, such as `#1008`. Fall back to `id`. |
| `id` | The merchant's order ID. |
| `permalinkUrl` | The order on the merchant's site. |
| `links` | Policy and contact pages as `{ type, url, title? }`. Types include `refund_policy`, `shipping_policy`, `terms_of_service`, `privacy_policy` and `contact_information`. |

A field the merchant didn't send is `null`, and `links` is empty. Show only what's there. Sapiom drops any link that isn't `https`. Other outcomes carry only `outcome`.

```json theme={null}
"order": {
  "outcome": "confirmed",
  "id": "gid://shopify/Order/1",
  "label": "#1008",
  "permalinkUrl": "https://shop.example/orders/1",
  "links": [
    { "type": "refund_policy", "url": "https://shop.example/policies/refund-policy", "title": "Refund policy" },
    { "type": "contact_information", "url": "https://shop.example/pages/contact" }
  ]
}
```

MCP results carry the same fields. If the order never confirms, tell the buyer the payment went through and to check the merchant's email. Never buy again.

## Refresh the checkout

For a UCP checkout only. Your backend calls refresh with the buyer's key when `nextAction.type` is `reconcile_checkout`, or after `payment_submitted` to get the confirmation. Then your agent reads the transaction again:

```bash theme={null}
curl --request POST \
  "https://api.sapiom.ai/v1/transactions/$TRANSACTION_ID/ucp/checkout/refresh" \
  -H "x-api-key: $SAPIOM_BUYER_KEY"
```

## Recover without buying twice

| Situation | Do this |
| - | - |
| A create request was not answered, or `nextAction.type` is `resend_request` | Send the same create request with the same key. |
| `idempotency_conflict` | That key belongs to a different request. Check the original merchant, checkout and card. |
| A `400` from Create a transaction | Fix what its `code`, `field` and `message` describe, then send the corrected request. |
| An MCP error before the merchant opened checkout | Follow the error's `nextStep`. Some fixes need a corrected request and a new key. |
| Browser Pay reported `refused`, `failed` or `relayed_unfulfilled` | Read the same transaction. If it reads `payment_submitted`, stop and confirm the order with the merchant. Otherwise follow `nextAction`. |
| The approval expired before your browser worker paid, and `nextAction.type` is `blocked` with `reason: payment_ended` | Sapiom did not send the card. Cancel the transaction. Then create a new transaction with a new key if the buyer still wants it. |
| A cancel returned `409 card_relayed` | Never pay or buy again. Tell the buyer to check with the merchant. |
| A UCP update or cancellation timed out or reports `change_unconfirmed`, or a UCP result asks a person to reconcile | Stop the agent. Your team recovers it with the merchant, using the original request and key. Don't assume the payment authorization or budget was released. Never start another transaction. |
| A browser cancellation timed out or may not have been saved | Read the transaction. If it is not closed, send the same cancellation with the same key. |

Error formats and codes are in [Errors](https://docs.sapiom.ai/api-reference/errors.md).


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