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

# Build browser purchases with Spend

> Let your platform's agent buy with a business card the business approves.

> [!NOTE]
>
> Spend is in preview. Contact Ionic to enable it for your platform.

Spend lets your platform's agent complete a purchase with a business's card.
Ionic stores the card, asks the business owner to approve each purchase, and
delivers short-lived card details to your browser worker. Your platform runs the
shopping and checkout, and records whether the seller accepted the order.

## What your platform builds

* A mapping from each of your customers to their Ionic business, and secure
  storage for your agent's keys.
* A purchase job that stores the cart, amount, currency, checkout URL, and
  billing details the owner approves.
* Links to Ionic's card setup and approval pages, and a way to pause the job
  while the owner decides.
* A trusted browser worker that uses the returned proxy, enters the card, and
  submits checkout.
* A lock so only one worker submits each purchase, and a record of the
  seller's result.

Ionic hosts the card setup and approval pages.

## Set up the business and agent

Your platform needs an active connection to the business, and your Platform
Program and the connection must allow `spend:read` and `spend:write`. Your agent
needs approved access to that business and an agent secret key with the same
permissions.

Send the business's `mer_…` ID in `Ionic-Account` on every request.
Use your platform's secret key for setup sessions, and the agent's key for
cards and spend intents. Only the owner can approve a purchase.

Keep keys on your server. Store the business, agent, and grant IDs with each
purchase job, so a replacement worker continues under the same access.

## Add a card

Create a setup session from your server with `POST /v1/spend/setup_sessions`,
using `spend:write`, `Ionic-Account`, and an `Idempotency-Key`. The body can be
`{}`, or include a `label` and a `return_url`. The `return_url` must be an HTTPS
URL on one of your platform's approved origins, with no query string or
fragment. Otherwise the request fails with `400 invalid_return_url`.

Send the returned `url` privately to an owner of the business. They sign in and
add the card on the Ionic page. The session expires after 30 minutes.

To confirm setup, retrieve the session with `GET /v1/spend/setup_sessions/{id}`.
Setup is complete when `status` is `completed` and `card_id` is set. Don't treat
a visit to your `return_url` as confirmation.

If setup expires, create a new session with a new idempotency key. Don't retry
`setup_session_expired` (422) or `setup_access_denied` (403).

## Get access to the card

After setup, the owner gives your agent access to the card in the Ionic
dashboard. Share
[Manage cards and agent purchases](https://docs.ionicfi.com/guides/spend-card-access.md) with them for the
steps. Your agent then sees the card in `GET /v1/spend/cards`. An empty list
means the agent has no card access yet.

## Request approval for a purchase

Create a spend intent with `POST /v1/spend_intents`. Store the
`Idempotency-Key` before you send the request, and retry with the same key and
body. Every Spend `POST` takes `Content-Type: application/json` and an
`Idempotency-Key` of 16 to 255 printable ASCII characters with no spaces.

This request asks to spend \$60.00. Replace the card ID, checkout URL, and
`allowed_hosts` with your own.

```json theme={null}
{
  "action": "payment",
  "card_id": "spcard_1234567890ABCD",
  "purpose": "Printer paper for the front office",
  "seller": {
    "name": "Example Supplies",
    "checkout_url": "https://checkout.example.com/test-order"
  },
  "allowed_hosts": ["payments.processor.example"],
  "amount": 6000,
  "currency": "usd",
  "billing_details": {
    "name": "Alex Buyer",
    "address": {
      "line1": "1 Main Street",
      "city": "Austin",
      "postal_code": "78701",
      "country": "US"
    }
  },
  "reference": "order-184",
  "claim_within_seconds": 300
}
```

* `amount` is in the smallest currency unit.
* `checkout_url` must use HTTPS, with no query string, fragment, user
  information, or port.
* `allowed_hosts` is required. List the exact hosts where your worker submits
  card fields. They're fixed when the owner approves, and redirects don't add
  hosts. Payment frames often use a different host from the checkout page, so
  include it.

Store the returned spend intent ID with the job and send the owner to
`approval_url`. They review the seller, amount, purpose, and claim window. A
spending limit can require your platform's approval before the business
decides. The request stays `pending_approval` until every approval in
`approvals` is complete.

A request that breaks a limit set to block fails with `422 limit_exceeded`, and
the spend intent is saved as `blocked`. Don't retry it. If your SDK doesn't
return the spend intent with the error, find it by `reference` with
`GET /v1/spend_intents`.

## Set agent limits

Limits cap how much card access an agent can be approved for. Your platform and
each business set their own, and every active limit applies. For each rule, the
smallest value wins.

| Who sets it | Where | What it covers |
| - | - | - |
| Your platform | **Spend → Agent limits**, then the agent | Your agent at every business it serves |
| The business | **Spend → Agents**, then the agent | The agent at that business |

A limit covers one agent and one currency, with up to 10 rules:

| Rule | Window | Counts |
| - | - | - |
| **Per-request maximum** | Each request | The amount of one request |
| **Total per window** | 1 hour, 24 hours, 7 days, or 30 days | Card access approved at one business in the window |
| **Requests per window** | 1 hour, 24 hours, 7 days, or 30 days | Requests approved at one business in the window |

Windows are rolling and count usage at one business, even for your platform's
limits. Usage is the card access approved, not the amount the seller charged.

Each rule sets what happens when a request goes over it:

* **Ask for approval:** the request stays `pending_approval` and lists the rule
  in `limit_breaches`. When your platform's rule is exceeded, your platform
  approves first, and the business decides last. If more card access is
  approved before the last decision, Ionic checks the request again.
* **Block:** the request fails with `422 limit_exceeded`, and no one can
  approve it.

If a party has limits but none in the request's currency, the request fails
with `422 currency_not_allowed`. With no limits set, the business still
approves every request.

Owners create, change, and archive limits, and admins can view them. Each
party sees the other's limits but can change only its own. Archiving is
permanent; create a new limit to set rules again.

## Wait for approval

Subscribe to `spend_intent.updated` with your platform's
[connected-account webhooks](https://docs.ionicfi.com/guides/connect-webhooks.md). Verify the signature,
skip event IDs you've already processed, and match the business and spend
intent to your job. Ionic sends events for your agent's requests only to your
platform.

Before checkout, retrieve the spend intent with `GET /v1/spend_intents/{id}`.
If webhooks aren't available, poll that endpoint with backoff. Don't poll the
claim endpoint. Stop the job if the status is `blocked`, `declined`,
`canceled`, or `expired`.

The owner has 24 hours to approve. After approval, claim before
`claim_expires_at`. The claim window defaults to five minutes, and a longer
window returns `claim_window_unsupported`.

## Claim the card in your browser worker

When the worker reaches card entry, take the job's lock and retrieve the spend
intent again. Then call `POST /v1/spend_intents/{id}/claim` with `{}`, a claim
`Idempotency-Key` stored with the job, and the same agent key and business.

The response contains `card`, `billing_details`, `proxy`, `allowed_hosts`,
`browser_requirements`, and `expires_at`:

1. Set the browser's HTTPS proxy to the returned host, port, username, and
   password.
2. Trust the returned certificate authority, and keep TLS certificate and
   hostname checks on.
3. If `browser_requirements.reload_payment_page` is `true`, reload the payment page.
4. Enter the returned card number, expiry, holder name, and security code at
   checkout. `cvc` is empty when the checkout doesn't need one.

The card details in the response are stand-ins. The proxy replaces them with
the real card details only on requests to `allowed_hosts`. Your browser
provider must support authenticated HTTPS proxies and a custom certificate
authority.

Keep the response in the worker's memory only. Never put it in model prompts,
logs, analytics, screenshots, browser traces, recordings, or stored job data.
The card details stop working at `expires_at`. Treat the checkout page as
sensitive: some test checkouts show card values in the browser.

`allowed_hosts` controls where card details can be sent. Your worker must buy
only what the owner approved, at the approved amount, once.

## Submit once and record the result

Before entering the card, record on the job that checkout may have started. A
replacement worker must check that record after a crash, even if the lock has
expired. Claiming again doesn't make a second submission safe.

Record the seller's order ID, confirmation, charged amount, and receipt. A
`claimed` status means the card details were delivered, not that the purchase
succeeded.

If the browser crashes, times out, or loses a response after submitting, mark
the purchase as uncertain. Check the seller's order history before retrying or
asking for a new approval. Ionic can't detect a duplicate order at the seller.

## Requests and errors

Every `POST` takes a JSON body and an `Idempotency-Key` of 16 to 255 printable
ASCII characters with no spaces. Claim and cancel take `{}`. Store a separate
key for each setup, creation, claim, and cancellation, and retry with the same
key and body. Creating returns `201`; retrying the same creation returns `200`
with the existing object.

Spend errors include `error.code`, `error.message`, and `error.retryable`.
Authentication errors use the standard [Ionic error format](https://docs.ionicfi.com/api-reference/errors.md),
which has no `retryable`. Check the HTTP status as well as the code, and wait
for `Retry-After` when you're rate limited or a claim is in progress.

## Recover without creating another purchase

| Error or status | What to do |
| - | - |
| `claim_in_progress` | Wait for `Retry-After`, then retry with the same claim key. |
| `claim_key_conflict` | Use the claim key already stored for this job. Don't create a new one. |
| `issuance_unknown` | Stop the worker and contact Ionic. Don't create a replacement purchase. |
| `approval_required` | Keep waiting for approval through events or reads. |
| `checkout_configuration_unavailable` | The approved checkout changed or was disabled. Don't send the card anywhere else. |
| `intent_expired` | Stop. Confirm with the seller that nothing was submitted before you request a new approval. |
| `403` or `404` | The key can't see or act on the object. Check permissions, revoked access, `Ionic-Account`, and mode. Don't retry for another business. |

Retrying a claim with the same key returns the same card details and expiry,
while access is still valid.

## Test your integration

Complete one approved test purchase and confirm the seller's result. Then test
a decline, an expired approval, revoked card access, a duplicate webhook, two
workers at once, a lost claim response, and a crash after submission. See
[Test and recover agent purchases](https://docs.ionicfi.com/guides/spend-testing.md).

For every request and response field, see
[the spend intent object](https://docs.ionicfi.com/api-reference/spend-intents.md) and its endpoints.


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