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

# Connect your platform's AI agent

> Ask a connected business to allow your platform's AI agent, then act for that business with the agent's own key.

Let your AI agent create invoices and checkout sessions for connected
businesses. Each business approves what the agent can do and can revoke
access at any time.

This guide takes you from creating an agent to creating a checkout session
for one business. Start in **Sandbox**.

## Before you begin

* An active Platform Program with delegated API access enabled in the mode you
  use. Ionic enables it for your platform.
* A connected business, with a connection that includes the permissions your
  agent needs. See [Connect an existing merchant](https://docs.ionicfi.com/guides/connect-a-merchant.md).
* A platform [secret key](https://docs.ionicfi.com/guides/authentication.md) with `platform:act`, from
  **Connect → API keys**. Your server uses it to ask businesses.

## 1. Create your AI agent

1. On the **Agents** page, select **Create agent**. Owners and admins can
   create agents. The first agent asks you to claim your account's agent
   handle prefix.
2. Give the agent a name and a handle, then choose what it can do. Creating
   the agent allows it in your platform's own account in the current mode,
   with those permissions, and an owner can issue a key for that account. That
   access and key cover only your own account.
3. Open the agent and go to its **Keys** tab. Under **Keys for connected
   businesses**, create a key. Only owners can issue keys.
4. Choose **Create checkout sessions** (`sessions:write`) for this example.
   Your Platform Program must allow this permission.
5. Copy the key. It is shown once. Store it where the agent runs.

Use your platform secret key to request approval. Use the agent's key after
the business approves:

| Variable | Used by | Purpose |
| - | - | - |
| `IONIC_PLATFORM_SECRET_KEY` | Your server | Create a request and check the business's answer. |
| `AGENT_PLATFORM_KEY` | Your AI agent | Act for an approved business. |

Keep both in your server's secret storage. The agent key needs `Ionic-Account`
on every call. Platform keys default to 365 days in test and 180 days in live.

## 2. Ask a business

Call `POST /v1/agent_grant_requests` from your server with your platform secret
key, without `Ionic-Account`. The request is in the key's mode.
Replace the example agent and business IDs with yours. Set `REQUEST_KEY` to a
unique value for this request and reuse it if you retry.

```bash theme={null}
curl https://api.ionicfi.com/v1/agent_grant_requests \
  -H "Authorization: Bearer $IONIC_PLATFORM_SECRET_KEY" \
  -H "Idempotency-Key: $REQUEST_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "agent_id": "agt_3Wd9Kp2Lm7Xq4R",
    "account": "mer_7Hk2Pq9Lm4Xw1R",
    "permissions": ["sessions:write"]
  }'
```

Save the request `id` with the business. Send the `url` to an owner of the
business, for example in your app or by email. The request expires after 7
days. Its initial `status` is `pending` and `agent_grant` is `null`.

Request only permissions included in your connection and Platform Program.
See [AI agent permissions](https://docs.ionicfi.com/api-reference/agent-permissions.md).

To ask from the dashboard, open the agent's **Businesses** tab, select
**Ask a business**, and search by business name or merchant ID. Choose the
business and permissions, then select **Create request**. Search covers your
connected-business directory. Changing between Sandbox and Live clears the
selection so you can prepare a request in the new mode.

## 3. Confirm approval

The owner opens the link and signs in to the business's Ionic account if
needed. Sign-in returns them to the request. They review your platform, the
agent, its permissions, the mode, and the expiry, then select **Allow** or
**Decline**. Admins can see requests but cannot decide them. Pending requests
also appear under **Requests** on the business's **Agents** page.

If your connection, Platform Program, or agent changed after you asked, the
approval is refused with `AGENT_GRANT_REQUEST_STALE` and nothing is granted.
Send a new request.

Retrieve the request with your platform secret key to check the answer.
Replace the example request ID with the `id` you saved:

```bash theme={null}
curl https://api.ionicfi.com/v1/agent_grant_requests/agrq_8Fq2LmV0cX4nR7 \
  -H "Authorization: Bearer $IONIC_PLATFORM_SECRET_KEY"
```

Continue when `status` is `approved` and `agent_grant` has `status: active`.
For pending, declined, canceled, and expired requests, `agent_grant` is `null`.
The response includes:

```json theme={null}
{
  "id": "agrq_8Fq2LmV0cX4nR7",
  "status": "approved",
  "agent_grant": {
    "id": "agr_5Tn8Bv2Qx6Lm3P",
    "status": "active",
    "revoked_at": null
  },
  "decided_at": 1793790000
}
```

Your platform also receives [`agent_grant_request.updated`](https://docs.ionicfi.com/webhooks/events)
when an owner approves or declines, and when you cancel. Its `data.object` is
the same request. A request that expires sends no event; it reads as `expired`.

## 4. Create a checkout session

Send the agent's platform key with `Ionic-Account` set to the business. This
example creates a checkout session for the business. Set `SESSION_KEY` to a
unique value and reuse it if you retry:

```bash theme={null}
curl https://api.ionicfi.com/v1/checkout/sessions \
  -H "Authorization: Bearer $AGENT_PLATFORM_KEY" \
  -H "Ionic-Account: mer_7Hk2Pq9Lm4Xw1R" \
  -H "Idempotency-Key: $SESSION_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "mode": "payment",
    "line_items": [
      { "name": "Tune-up", "amount": 4500, "currency": "usd", "quantity": 1 }
    ],
    "success_url": "https://platform.example.com/paid"
  }'
```

The mode comes from the key. Every request checks the key, the agent, the
business's approval, your connection with the business, and your Platform
Program. Writes need an `Idempotency-Key`; reuse it when you retry.

A successful call returns `201` and a checkout session with an ID beginning
`cs_`. The session starts unpaid. Use the
[checkout guide](https://docs.ionicfi.com/checkout/overview.md) to present checkout to the customer and
confirm the payment result before fulfilling the order.

See [AI agent permissions](https://docs.ionicfi.com/api-reference/agent-permissions.md) for other tasks
and the operations available to platform agents.

## Return the owner to your app

Optionally include `return_url` when creating the request. First add its
redirect domain under **Connect → Domains**. After the owner decides, Ionic
returns their browser with `agent_grant_request` added to the URL, provided
the origin is still approved. Retrieve the request to verify the decision;
the redirect alone does not confirm approval.

## Stop or restore access

* **The business revokes the agent.** Its next request is refused with
  `403` `AGENT_ACCESS_DENIED`. The request reads `agent_grant.status`
  `revoked`, with `revoked_at` set. To act for the business again, send a new
  request.
* **You withdraw a pending request.** Call
  `POST /v1/agent_grant_requests/{id}/cancel` with an `Idempotency-Key`. The
  link then shows the business that you withdrew it.
* **You stop the agent.** On the agent's page, **Suspend** refuses every call
  from its keys until you resume it. **Deactivate** stops it permanently.
  Revoking one key stops only that key.

## See your agents across businesses

* **Agents**, in your Connect workspace, lists each agent with the businesses
  that allow it, the ones waiting, and its actions over the last 30 days.
* The agent's **Businesses** tab shows each business's latest answer, the
  agent's actions there, and the business's payment volume. Select a business
  to narrow the agent's Activity to it. **View all activity** keeps that
  business selected; **Clear** shows all businesses again.
* On **Connected merchants**, use **Has agents** or **Agents waiting** to
  filter the list. Open a business's **Agents** tab to see your agents there
  and their activity.

## Troubleshooting

* **The request is rejected:** check that the agent is active, the business is
  connected in the same mode, and the requested permission is included in
  both the connection and your Platform Program.
* **The agent gets `AGENT_ACCESS_DENIED`:** check its key permissions, the
  business's current approval, and the `Ionic-Account` value. An approved
  request does not override a revoked grant or an inactive connection.
* **The agent gets `AGENT_OPERATION_UNAVAILABLE`:** check the
  [supported operations](https://docs.ionicfi.com/api-reference/agent-permissions.md).
* **The dashboard cannot load businesses or requests:** select **Try again**
  in the error message. A failed read does not mean there are no requests.

For individual error codes, see [Errors](https://docs.ionicfi.com/api-reference/errors.md).


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