> For the complete documentation index, see [llms.txt](https://gitbook.com/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gitbook.com/docs/documentation/ja-gitbook-documentation/docs-as-code/gitbook-cli.md).

# GitBook CLI

GitBook CLI（`@gitbook/cli`）は、ターミナルから直接 GitBook のコンテンツや組織を操作するためのコマンドラインツールです。

GitBook API を一連のコマンドとして扱えるようにするため、組織の一覧表示、スペースやページの確認、ドキュメントへの質問、連携の作成と公開が、すべてシェルを離れずに行えます。

## 人間主導とエージェント主導のワークフロー

CLI は 2 種類の使い方を想定して設計されています:

* **人間主導** — ターミナルでコマンドを入力して調べ物をしたり、単発のタスクをスクリプト化したり、連携を手動で管理したりします。出力は対話的なシェルで読みやすいように整形されます。
* **エージェント主導のコーディング** — AI コーディングエージェント（Claude Code、Codex、Cursor など）が、より大きなタスクの一部としてあなたの代わりに CLI を実行します。機械可読な出力（`--json`）と予測可能なコマンド構造により、エージェントはコマンドを呼び出し、結果を解析し、それらを連結しやすくなります。

{% hint style="info" %}
目的に合わせて作られたプロトコルを使って GitBook の API 経由でコンテンツの作成・編集を AI エージェントにさせたい場合は、 [GitBook MCP](/docs/documentation/ja-gitbook-documentation/docs-as-code/gitbook-mcp.md)。CLI は、スクリプト可能なコマンド、連携開発、またはターミナルで快適に動作するエージェントがすでにある場合に適しています。
{% endhint %}

## インストール

GitBook CLI には Node v18 以降が必要です。npm からグローバルにインストールします:

```bash
npm install @gitbook/cli -g
```

これにより `gitbook` コマンドがインストールされます。動作確認を行います:

```bash
gitbook --version
```

## Authenticate

一度サインインすると、CLI は認証情報をローカルに保存し、必要に応じて更新します。

{% tabs %}
{% tab title="ブラウザ（OAuth）" %}
最も手早いサインイン方法はブラウザを使うことです:

```bash
gitbook login
```

これによりブラウザで GitBook が開き、CLI の認可を求められ、生成されたトークンがローカルに保存されます。セッションは自動的に更新されます。

これは日常利用に推奨される方法です。
{% endtab %}

{% tab title="個人用 API トークン" %}
ブラウザの手順を省略するには — CI、スクリプト、または公開用の連携のために — 個人用 API トークンで認証します。次のページで作成できます: [app.gitbook.com/account/developer](https://app.gitbook.com/account/developer)、次に次を実行します:

```bash
gitbook auth --token <token>
```

これを省略すると `--token`、CLI が入力を求めます。
{% endtab %}
{% endtabs %}

いつでも、現在どのアカウントでサインインしているか確認できます:

```bash
gitbook whoami
```

サインアウトするには、次を実行します `gitbook logout`.

{% hint style="warning" %}
連携を公開するには（`gitbook integration publish` / `公開解除`）には個人用 API トークンが必要です — ブラウザ（OAuth）セッションではこれらの操作は実行できません。次を実行します `gitbook auth --token <token>` 公開ワークフロー用です。2 つの認証情報は共存できるため、日常的なコマンドにはブラウザでサインインし、公開にはトークンを使えます。
{% endhint %}

## 最初のコマンドを実行する

ほとんどのコマンドは GitBook API から生成され、リソースごとにグループ化されています — `組織`, `スペース`, `コレクション`などです。まず、所属している組織を一覧表示します:

```bash
gitbook organizations list
```

その出力から組織 ID を取得し、次にそのスペースを一覧表示します:

```bash
gitbook spaces list --organization <organizationId>
```

単一のスペースの詳細を取得します:

```bash
gitbook spaces get <spaceId>
```

スペース内のページを一覧表示します:

```bash
gitbook spaces content pages list <spaceId>
```

{% hint style="info" %}
次のようなパスパラメータは `<spaceId>` 位置引数としてもフラグとしても渡せます — `gitbook spaces get <spaceId>` 、 `gitbook spaces get --spaceId <spaceId>` は同等です。
{% endhint %}

次を実行します `gitbook --help` でコマンドツリー全体を参照するか、 `--help` を任意のコマンドに追加します（たとえば `gitbook spaces --help`）とすると、そのサブコマンドとオプションを確認できます。

## 出力形式

すべての API コマンドは同じ出力フラグをサポートしています:

| フラグ        | 出力                        |
| ---------- | ------------------------- |
| `--pretty` | 人間が読みやすい要約（対話型ターミナルでの既定値） |
| `--json`   | JSON — スクリプトやエージェントに最適    |
| `--yaml`   | YAML                      |
| `--full`   | 簡易要約の代わりにすべてのフィールドを表示します  |

フラグを指定しない場合、CLI は妥当な既定値を選びます: 対話型ターミナルでは見やすい出力、出力がパイプまたはリダイレクトされる場合は YAML です。指定してください `--json` たとえば次のようなツールへパイプする場合は、明示的に `jq`:

```bash
gitbook organizations list --json | jq '.items[].title'
```

## ドキュメントに質問する

CLI は自然言語でコンテンツを検索し、生成されるにつれて回答をストリーミングできます:

```bash
gitbook organizations ask stream <organizationId> --query "How do I reset my password?"
```

回答はターミナルにストリーミングされ、その後に出典と提案されたフォローアップ質問が続きます。次を押して `Ctrl-C` で早めに停止し、それまでにストリーミングされた内容をそのまま残します。

## AI コーディングエージェントから CLI を操作する

CLI はスクリプト化でき、JSON も扱えるため、AI コーディングエージェントは作業中にツールとして利用できます。上記のコマンドをエージェントに使わせ、認証、コンテンツの探索、結果に基づく操作を任せてください。

{% prompt description="Explore an organization’s docs from the terminal." %}

```markdown
`gitbook` CLI を使って、GitBook コンテンツの全体像をつかむのを手伝ってください。

1. `gitbook whoami` を実行して、サインインしていることを確認してください。サインインしていない場合は、`gitbook login` を実行するよう伝えてください。
2. `gitbook organizations list --json` で自分の組織を一覧表示し、名前と ID を示してください。
3. どの組織を調べるか私に尋ねてから、そのスペースを一覧表示してください。
4. 見つかった内容を要約してください — スペースがいくつあるか、そして各スペースがタイトルから見て何を扱っていそうか。

すべてのコマンドで `--json` を使い、出力を確実に解析できるようにしてください。また、実行した正確なコマンドを示してください。
```

{% endprompt %}

{% prompt description="Answer a question using my docs and cite sources." %}

```markdown
`gitbook` CLI を使って、私のドキュメントからの質問に答えてください。

1. `gitbook whoami` でサインインしていることを確認してください。
2. 私の組織を一覧表示し、どれを検索するか確認してください。
3. `gitbook organizations ask stream <organizationId> --query "<my question>"` を実行し、回答を伝えてください。
4. CLI が返す出典も含め、元のページと照合して回答を確認できるようにしてください。
```

{% endprompt %}

## 連携を構築する

コンテンツの検索だけでなく、CLI は開発のための主要なツールです [GitBook 連携](/docs/developers/integrations/quickstart.md)。次のコマンドで新しいプロジェクトをひな形生成します:

```bash
gitbook integration new
```

次に `gitbook integration dev` でローカル実行し、 `gitbook integration publish` 公開します。次を参照してください: [連携のドキュメント](/docs/developers/integrations/quickstart.md) で、完全な開発ワークフローを確認してください。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://gitbook.com/docs/documentation/ja-gitbook-documentation/docs-as-code/gitbook-cli.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
