# CLI

> 透過終端機執行 Management API 指令碼、為網站集合產生 TypeScript 型別，並檢查 Headless 前端的設定。

> Markdown version of https://kanorio.com/zh-Hant/developers/cli. Append `.md` to any developer docs URL for this format. Index: https://kanorio.com/llms.txt · MCP: https://app.kanorio.com/api/mcp
>
> Translated from the English original: https://kanorio.com/en/developers/cli.md

Kanorio CLI 為 Management API 提供指令列封裝，適用於指令碼與 CI 環境，並新增兩項開發者工具：型別產生與 Headless 前端健康檢查。

```bash
npx @kanorio/cli --help
```

## 登入

```bash
npx @kanorio/cli login --token knr_pat_...
```

權杖會以僅限擁有者存取的權限儲存於 `~/.config/kanorio/config.json`。在 CI 環境中，請改為設定 `KANORIO_TOKEN`，其優先順序高於儲存的權杖。使用 [服務帳戶](https://kanorio.com/zh-Hant/developers/authentication) 權杖亦可。

## 常見指令

```bash
kanorio sites list
kanorio pages list ws_example123
kanorio pages replace-sections ws_example123 pg_example456 --file sections.json
kanorio media upload ws_example123 --file hero.jpg
kanorio publish ws_example123
kanorio domains status ws_example123
```

會覆寫內容的指令會先讀取資源並傳送其時間戳記，因此若發生衝突，系統會回傳 `CONFLICT` 錯誤，而非覆寫他人的編輯。執行 `kanorio --help` 可查看完整清單。

## 輸出與結束代碼

- 加入 `--json` 參數可列印原始 API 回應（包含錯誤），便於撰寫指令碼。
- 錯誤訊息會以 `<CODE> (HTTP <status>): <message>` 格式輸出至 stderr，並以 `1` 結束程式。用法錯誤則以 `2` 結束。
- 關於已棄用操作的警告會輸出至 stderr。

## 產生型別

```bash
kanorio types --site https://s-xxxxxxxxxxxx.kanorio.com --out src/kanorio.d.ts
```

`types` 指令會讀取網站的公開集合結構描述，並為每個集合寫入 `FieldData`、`Expanded` 與 `Item` 型別，以及頁面、全域變數與網站詮釋資料的型別。當結構描述變更時請再次執行，或訂閱 `collections.schema.changed` Webhook 以自動化此流程。

## 檢查 Headless 前端

```bash
kanorio doctor --site ws_example123
```

`doctor` 指令會檢查您的前端是否正確提供 sitemap、robots.txt、llms.txt、feeds、JSON-LD 與 IndexNow 金鑰檔案，並確認頁面內容是否同步，以及瀏覽量與 Webhook 是否正常送達。在將網域從 Kanorio 託管網站遷移至您自己的前端之前，請加上 `--switching` 參數。
