# 代理支付快速入门

使用 InFlow CLI 为一次 Tomba 请求付费，然后查看支付交互在网络上的实际样子。价格和计费规则见[面向 AI 代理的 Tomba](/mpp/introduction#pricing)。

## 开始之前

您需要一个生产环境中的 [InFlow](https://inflowpay.ai) 账户，并带有 USDC 或 USDT 余额。您不需要 Tomba 账户或 API 密钥。

## 使用 InFlow CLI 为请求付费

1. 安装 InFlow CLI。在 macOS 和 Linux 上：

    ```bash
    curl -fsSL https://inflowcli.ai/install.sh | bash
    ```

    在 Windows 上，于 PowerShell 中运行：

    ```powershell
    irm https://inflowcli.ai/install.ps1 | iex
    ```

    上述命令都会下载并运行安装脚本。如需在运行前阅读脚本，请在浏览器中打开该 URL。

2. 登录 InFlow。客户端名称会显示在浏览器中打开的审批页面上。

    ```bash
    inflow auth login --client-name "my-agent"
    ```

3. 为请求付费：

    ```bash
    inflow mpp pay "https://agents.tomba.io/domain-search?domain=stripe.com" --interval 5 --max-attempts 180
    ```

    CLI 会读取 `402` 质询，创建付款，等待 InFlow 报告付款就绪，然后携带凭据再次发送请求并输出结果。Tomba 的响应位于 `body` 中；付款结算后，`outcome` 为 `paid`。如果 InFlow 要求您批准付款，请在命令等待期间批准。

    如果不使用 `--interval`，命令会在创建付款后返回，并给出 `transaction_id` 以及用于完成请求的 `inflow mpp fetch` 命令。

其他端点的用法相同。请使用对应的 [Tomba API 端点](/mpp/introduction#endpoints)的查询参数：

```bash
inflow mpp pay "https://agents.tomba.io/email-finder?domain=stripe.com&first_name=Jane&last_name=Doe" --interval 5 --max-attempts 180
inflow mpp pay "https://agents.tomba.io/email-verifier?email=jane.doe@stripe.com" --interval 5 --max-attempts 180
inflow mpp pay "https://agents.tomba.io/technology?domain=stripe.com" --interval 5 --max-attempts 180
```

## 自动批准付款

默认情况下，InFlow 会将每笔 MPP 扣款挂起，等待人工审批。付款会以 **Pending approval** 状态停留在 [Approvals](https://app.inflowpay.ai/approvals/) 中，直到您点击绿色对勾。这就是上面的命令传入 `--interval 5 --max-attempts 180` 的原因：CLI 会在等待您的同时轮询最多 15 分钟。无人值守的代理则会一直停滞。

策略可以消除等待。在 [app.inflowpay.ai/policies/add](https://app.inflowpay.ai/policies/add/) 创建策略：

| 字段             | 值                                                                  |
| ----------------- | ---------------------------------------------------------------------- |
| **Policy Type**   | `MPP`                                                                  |
| **Policy Action** | `Approve Requests`                                                     |
| **Currency**      | 您用于支付的币种：`USDC` 或 `USDT`                             |
| **Budget**        | 策略在一个周期内可批准的总额，例如 `10`           |
| **Threshold**     | 单笔付款的可选上限。不得超过预算           |
| **Period**        | `Once`、`Daily`、`Monthly` 或 `Yearly`                                 |
| **Reserved**      | 从可用余额中预留预算。仅适用于 `Once` 周期 |
| **Seller**        | `All Sellers`，或选择 `A Specific Seller` 将策略限定于 Tomba     |
| **Expires**       | 可选。超过此时间后，策略不再匹配新的请求       |

如果您将策略限定为仅适用于此服务，请使用 Tomba 的卖家 ID：

```text
4e0db086-a9c2-4291-a278-acb98f994572
```

策略生效后，扣款一到达就会被批准，`inflow mpp pay` 无需轮询即可返回。

<Callout type="caution">
    策略按币种匹配。为 `USDT` 编写的策略不会批准 `USDC` 质询，付款会再次等待人工审批；请参阅[选择币种](#choose-a-currency)。

    `Once` 周期永不重置，因此预算用完后策略会停止批准，付款会再次静默排队。如果代理开始等待，请在策略上对比 **Spent** 与 **Budget**。如需长期有效的额度，请使用 `Daily` 或 `Monthly`。

</Callout>

### 通过 API 创建策略

仪表板中的表单对应 `POST /v1/policies`。只有 `type` 和 `action` 是必填项：

```bash
curl -X POST https://api.inflowpay.ai/v1/policies \
  -H "X-API-Key: $INFLOW_PRIVATE_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "type": "MPP",
    "action": "APPROVE",
    "currency": "USDT",
    "budget": 10,
    "threshold": 0.05,
    "period": "MONTHLY",
    "sellerId": "4e0db086-a9c2-4291-a278-acb98f994572"
  }'
```

它会返回 `200` 和所创建的策略：

```json
{
    "policyId": "9aaaf91e-0ae4-4b01-8970-aa29e0d2e6ad",
    "type": "MPP",
    "action": "APPROVE",
    "status": "ACTIVE",
    "currency": "USDT",
    "budget": "0.01",
    "threshold": "0.005",
    "period": "ONCE",
    "requesterId": "4e0db086-a9c2-4291-a278-acb98f994572",
    "created": "2026-10-06T10:54:01.903Z",
    "updated": "2026-10-06T10:54:01.903Z"
}
```

`action` 取 `APPROVE`、`DECLINE` 或 `REVIEW` 之一；`period` 取 `ONCE`、`DAILY`、`MONTHLY` 或 `YEARLY` 之一；`currency` 取 `USDC`、`USDT` 或 `PYUSD` 之一。省略 `sellerId` 可将策略应用于所有卖家。

<Callout type="caution">
    `budget`、`threshold`、`currency` 和 `period` 仅在 `action` 为 `APPROVE` 时生效。如果与 `REVIEW` 或 `DECLINE` 一起发送，它们会**被丢弃且不报错**：请求仍返回 `200`，而创建的策略完全没有支出限制。创建策略后请重新读取它，并检查您发送的字段是否存在。

    您作为 `sellerId` 发送的卖家会以 `requesterId` 返回。没有 `requesterId` 的策略适用于所有卖家。

</Callout>

使用 `GET /v1/policies` 列出现有策略，使用 `GET /v1/policies/{policyId}` 读取单个策略，使用 `DELETE /v1/policies/{policyId}/delete` 删除策略。

## 选择币种

每个端点都提供 1 个 USDC 质询和 1 个 USDT 质询。请检查端点提供了哪些质询，以及您的账户可以用什么币种支付：

```bash
inflow mpp inspect "https://agents.tomba.io/domain-search?domain=stripe.com"
inflow mpp supported
inflow balances list
```

然后使用您持有的币种支付：

```bash
inflow mpp pay "https://agents.tomba.io/domain-search?domain=stripe.com" --currency USDT --interval 5 --max-attempts 180
```

<Callout type="caution">
    如果不使用 `--currency`，CLI 不会检查您的余额是哪种币种。如果它选择了另一种币种的质询，付款会因资金不足而失败。
</Callout>

## 支付交互示例

不带凭据调用端点，即可查看质询：

```bash
curl -i "https://agents.tomba.io/domain-search?domain=stripe.com"
```

响应的状态为 `402`，并带有 1 个 `WWW-Authenticate` 响应头，其中按币种各列出 1 个 `Payment` 质询，以逗号分隔（此处的值已缩短）：

```http
HTTP/2 402
content-type: application/problem+json
cache-control: no-store
www-authenticate: Payment id="uVqFx76…", realm="agents.tomba.io", method="inflow", intent="charge", request="eyJhbW91bnQiOiIwLjAwMzEyIiwiY3VycmVuY3kiOiJVU0RDIiwi…", expires="2026-09-29T17:08:48.175Z", opaque="eyJfbXBweF9zY29wZSI6…", Payment id="jK_Djgg…", realm="agents.tomba.io", method="inflow", intent="charge", request="eyJhbW91bnQiOiIwLjAwMzEyIiwiY3VycmVuY3kiOiJVU0RUIiwi…", expires="2026-09-29T17:08:48.569Z", opaque="eyJfbXBweF9zY29wZSI6…"
```

- `request` 是包含支付条款的 base64url 编码 JSON，例如 `{"amount":"0.00312","currency":"USDC","methodDetails":{"rail":"balance"},"recipient":"…"}`。
- `opaque` 将质询绑定到您调用的端点。
- `expires` 是一个时间点，超过该时间后，质询以及为其签发的任何凭据都不再被接受。

响应体会重复价格和 CLI 命令，并添加为 LLM 代理编写的 `agent_instructions` 和 `agent_memory` 字段：

```json
{
    "error": {
        "code": "payment_required",
        "message": "Payment required. Use the InFlow CLI to pay and retry."
    },
    "payment": {
        "amount": "0.00312",
        "currency": "USDC",
        "accepted_currencies": ["USDC", "USDT"],
        "display": "$3.12 per 1,000 requests",
        "supported_rails": ["mpp"]
    },
    "how_to_pay": {
        "inflow": "inflow mpp pay https://agents.tomba.io/domain-search?domain=stripe.com",
        "inspect": "inflow mpp inspect https://agents.tomba.io/domain-search?domain=stripe.com"
    }
}
```

已付费的响应包含 Tomba 的状态和响应体，以及以下响应头：

| 响应头              | 值                                                                                              |
| ------------------- | -------------------------------------------------------------------------------------------------- |
| `Payment-Receipt`   | 付款证明，格式为 base64url 编码的 JSON。仅出现在已扣费的响应中。                            |
| `X-Payment-Charged` | 请求已扣费时为 `true`，未扣费时为 `false`。请参阅[计费](/mpp/introduction#billing)。 |
| 速率限制响应头  | 从 Tomba API 转发；请参阅[速率限制](/mpp/introduction#rate-limits)。                    |
| `X-Request-ID`      | 此响应的关联 ID。提交支持请求时请提供该 ID。如果请求中带有关联 ID，则原样返回。            |

## 不使用 CLI 付款

要从您自己的代码中付款，请发出与 CLI 相同的调用。在 `X-API-KEY` 请求头中使用您账户的 API 密钥向 InFlow 进行认证。

1. 发送请求并读取 `402` 响应的 `WWW-Authenticate` 响应头。将其拆分为各个 `Payment` 质询，并选择解码后的 `request` 中币种为您所持币种的那个质询。
2. 为该质询创建交易。以 JSON 对象形式发送质询的参数，所有值保持不变：

    ```bash
    curl -X POST "https://api.inflowpay.ai/v1/transactions/mpp" \
      -H "X-API-KEY: $INFLOW_API_KEY" \
      -H "Content-Type: application/json" \
      -d '{
        "challenge": {
          "id": "<id>",
          "realm": "agents.tomba.io",
          "method": "inflow",
          "intent": "charge",
          "request": "<request>",
          "expires": "<expires>",
          "opaque": "<opaque>"
        },
        "options": {}
      }'
    ```

3. 响应中包含 `state`。当其为 `pending` 时，等待 `retryAfterSeconds` 秒后轮询该交易，直到状态变为 `ready`。状态为 `failed` 或 `expired` 时，本次尝试结束。

    ```bash
    curl "https://api.inflowpay.ai/v1/transactions/<transactionId>/mpp" \
      -H "X-API-KEY: $INFLOW_API_KEY"
    ```

4. 使用就绪交易中的 `credential` 再次发送原始请求：

    ```bash
    curl -i "https://agents.tomba.io/domain-search?domain=stripe.com" \
      -H "Authorization: Payment <credential>"
    ```

[与 AI 代理配合使用](/mpp/ai-agents)提供了该流程的 [Python 辅助函数](/mpp/ai-agents#python-helper)，以及基于 InFlow 买家 SDK 构建的 JavaScript 版本。
