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

# CLI

> 在终端里管理 Waffo Pancake 的店铺、商品、优惠和 webhook——人和 AI Agent 都能用

Waffo Pancake CLI（`pancake`）把商户 API 封装成一个命令行工具。你可以在终端交互使用、写进脚本，或者直接交给 AI 编程 Agent：每条命令都能以 JSON 形式发现，每次写入都能离线预览，没有显式确认参数就不会发生任何不可逆操作。

> [!NOTE]
>
> **候选版本。** CLI 以 `@waffo/pancake-cli` 发布在 npm 的 `next` 渠道（当前 `0.0.1-rc.2`），尚无稳定版；Homebrew 安装会随首个稳定版提供。需要 Node.js 22.14 或更高版本。

## 安装

```bash npm（全局） theme={"system"}
npm install -g @waffo/pancake-cli@next
pancake --version
```

```bash npx（免安装） theme={"system"}
npx @waffo/pancake-cli@0.0.1-rc.2 --help
```

自动化场景请固定具体版本号。在候选版渠道内更新，重新执行安装命令即可。

## 登录

### 浏览器登录（推荐）

```bash theme={"system"}
pancake login
```

CLI 会显示一个短码并打开 Dashboard 登录页。确认页面上的短码与终端一致、账号是你预期的，再点击 **Authorize CLI**。只批准你自己发起的登录——授权 15 分钟后过期。

浏览器登录默认进入**测试环境**。切换环境需显式执行，CLI 会先验证权限再保存：

```bash theme={"system"}
pancake whoami
pancake env test
pancake env prod
```

浏览器会话覆盖测试与生产环境的日常业务操作，范围不超过你现有的商户和店铺权限。它不能管理 API Key、提款或银行信息、其他设备，也读不到 webhook 的 `secret`。活跃的 CLI 会话列在 **Dashboard → 设置 → 安全**；`pancake logout` 会先在服务端撤销会话，再清除本地凭证。

### API Key 登录

CI 和无人值守脚本用 Merchant ID 加 RSA 私钥登录，私钥在 **Dashboard → API 与开发 → API Keys** 获取：

```bash theme={"system"}
chmod 600 /absolute/path/key.pem
pancake login --merchant-id MER_xxx --private-key-file /absolute/path/key.pem
```

`pancake login --key-login` 提供隐藏输入的交互方式。CLI 只保存私钥文件的绝对路径。测试还是生产由你使用的密钥决定，API Key 会话不能用 `env` 切换。

自动化也可以不保存登录，改用环境变量注入凭证：设置 `WAFFO_MERCHANT_ID`，再从 `WAFFO_PRIVATE_KEY_FILE` 或 `WAFFO_PRIVATE_KEY` 中二选一。完整的一对会覆盖已保存的登录；只给一半会直接报错。

配置保存在 `~/.waffo-pancake/config.json`（Windows 为 `%USERPROFILE%\.waffo-pancake\config.json`），可用 `WAFFO_CONFIG_DIR` 指定其他目录。

## 常用命令

所有命令都支持 `--json`，返回单个机器可读的结果信封；输出被管道接走时自动切换为 JSON。

| 领域 | 命令 |
| - | - |
| 身份 | `whoami`、`env test\|prod`、`doctor`、`config show`、`logout` |
| 店铺 | `stores list`、`stores get`、`stores create`、`stores update`、`stores delete` |
| 商品 | `products list`、`products get`、`products versions`、`products create`、`products update`、`products publish`、`products set-status`——均需 `--type onetime\|subscription` |
| 订阅商品组 | `groups create`、`groups update`、`groups delete`、`groups publish` |
| 订单与支付 | `orders list`、`subscriptions list`、`subscriptions cancel`、`payments list`、`payments get` |
| 退款 | `refunds list`、`refunds get`、`refunds create`、`refunds resubmit`、`refund-tickets list` |
| 优惠 | `coupons list\|get\|create\|update\|deactivate`、`promotion-codes list\|create\|generate\|deactivate` |
| 结账与客户 | `checkout create`、`sessions create`（客户会话 Token，仅限 API Key 登录） |
| Webhook | `webhook list\|add\|update\|remove`（仅测试 webhook）、`listen` |
| 通知与内容安全 | `recipients list\|add\|update\|remove`、`content scan` |
| 数据 | `graphql query`、`analytics query`（只读、单个操作） |
| 本地 | `commands`、`schema <command.id>`、`operations list\|show`、`diagnostics export` |

几个例子：

```bash theme={"system"}
pancake stores list --limit 20 --fields id,name,status --json
pancake products list --type onetime --store-id STO_xxx --json
pancake payments list --json
pancake coupons list --store-id STO_xxx --json
```

列表一次返回一页（`--limit`、`--offset`）；筛选条件通过 `--file` 以 JSON 文件提供。

## 安全地写入数据

所有写操作遵循同一套流程：用 JSON 文件描述输入，离线预览，再带显式确认执行。

**1. 先看输入契约**

```bash theme={"system"}
pancake schema products.create --json
```

`schema` 会输出任意命令的字段、参数、输出协议和示例。金额一律是**十进制字符串**，例如 `"19.99"`。

**2. 用 --dry-run 预览**

```json product.json theme={"system"}
{
  "storeId": "STO_xxx",
  "name": "Pro plan",
  "prices": { "USD": { "amount": "19.99", "taxCategory": "saas" } }
}
```

```bash theme={"system"}
pancake products create --type onetime --file product.json --dry-run --json
```

预览只做本地校验：不读凭证、不联网、不写操作记录。

**3. 带 --yes 执行**

```bash theme={"system"}
pancake products create --type onetime --file product.json --yes \
  --idempotency-key MER_xxx_product_001 --json
```

自己传 `--idempotency-key`，脚本重跑时就不会重复创建。CLI 对每次写入只发送一次请求并记录在本地，`operations show <id>` 可以查看结果。

### 确认参数

| 参数 | 何时必须 |
| - | - |
| `--yes` | 任何写操作 |
| `--confirm-production` | 在生产环境写入，以及 `products publish` / `groups publish`（它们把测试数据复制**到生产**） |
| `--confirm-shared` | 修改店铺或通知收件人——这些资源在测试与生产之间共享 |
| `--output <新文件>` | `checkout create`、`sessions create`、`graphql query`、`analytics query`——链接、Token 和完整查询结果写入新的私有文件，不输出到 stdout |

> [!WARNING]
>
> `products update` 会**替换整张价格表**。要改一个币种，必须把要保留的其他币种也一起传；完全不传 `prices` 才会保留原价格。

### 结果状态

| `meta.outcome` | 含义 |
| - | - |
| `completed` | 已完成。商品创建和测试 webhook 变更会回读资源核验；其他操作只校验响应。 |
| `rejected` | 服务端拒绝，什么都没写入。 |
| `unknown` | 请求可能已生效。先看 `operations show` 并查询资源，**不要**换个幂等键重放。 |
| `committed_unverified` | 服务端已接受，但回读、导出或本地记录失败。查询资源确认，不要重复写入。 |

退出码：`0` 成功，`1` 执行或核验失败，`2` 参数或配置错误，`130` 被中断。

## 把 webhook 转发到本地

不需要公网地址也能调试 webhook 处理逻辑。浏览器登录后：

```bash theme={"system"}
pancake listen --store-id STO_xxx --forward-to http://localhost:3000/webhook
pancake listen --store-id STO_xxx --forward-to http://localhost:3000/webhook \
  --events order.completed,subscription.created --json
```

`listen` 开启一个临时会话，把每个事件连同原始请求体、`X-Waffo-Signature` 和 `X-Waffo-Event` 头原样转发，你的处理器可以像生产环境一样验签——见 [Webhook](https://docs.waffo.ai/zh/guides/webhooks.md)。它始终从测试环境开始，加 `--live` 才监听生产。处理器返回 2xx 视为送达成功，否则最多重试三次。Ctrl+C 会清理会话，已配置的 webhook 不会被改动。

## 配合 AI Agent 使用

CLI 从设计上就考虑了被 Claude Code、Codex、Cursor 这类编程 Agent 驱动：

* `pancake commands --json` 列出全部命令，`pancake schema <command.id> --json` 返回输入契约，Agent 不用猜参数。
* `--dry-run` 让 Agent 在触碰账户前先验证方案，`--yes` 把是否写入的决定权留给你。
* `--no-interactive` 缺少输入时直接报错，不弹交互提示。
* 安装包自带 `SKILL.md` Agent 参考文档——[在 jsDelivr 上查看](https://cdn.jsdelivr.net/npm/@waffo/pancake-cli@0.0.1-rc.2/SKILL.md)。

想一句话搞定接入？Dashboard 的 **API 与开发 → AI** 页签会给出一段提示词，为你的店铺安装 [Pancake Skill](https://docs.waffo.ai/zh/integrate/skill.md) 或 CLI。

## 候选版本的限制

* Webhook 的 `add` / `update` / `remove` 只对**测试** webhook 生效。
* `sessions create` 需要 API Key 登录。
* `listen` 需要浏览器登录，且不能触发模拟事件。
* 没有 `orders get`、`subscriptions get` 和商品组的读取命令；未覆盖的查询用 `graphql query`。
* 商品写入拒绝 `taxIncluded` 字段——价格始终为税外价。

## 参考

> [card] [使用说明](https://cdn.jsdelivr.net/npm/@waffo/pancake-cli@0.0.1-rc.2/docs/usage.md)
>
> 随安装包提供的完整命令说明。

> [card] [API 覆盖矩阵](https://cdn.jsdelivr.net/npm/@waffo/pancake-cli@0.0.1-rc.2/docs/api-coverage.md)
>
> 每条命令对应的接口及其核验方式。

> [card] [GitHub 源码](https://github.com/waffo-com/waffo-pancake-cli)
>
> MIT 协议，欢迎提 issue 和贡献。

> [card] [商户 API 参考](https://docs.waffo.ai/zh/api-reference/introduction.md)
>
> CLI 底层调用的 REST Actions 与 GraphQL。


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