> 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/zh/chuang-jian-nei-rong/openapi/guides/structuring-your-api-reference.md).

# 构建 API 参考结构

了解如何使用图标和描述在多个页面中组织你的 API 参考

GitBook 不仅仅会渲染你的 OpenAPI 规范。它还允许你自定义 API 参考文档，以获得更清晰的结构、更好的导航和品牌呈现。

要在 **每个标签一个页面** 和 **每个操作一个页面**之间进行选择，请参阅 [OpenAPI 布局](https://gitbook.com/docs/documentation/zh/chuang-jian-nei-rong/openapi/guides/openapi-layouts.md).

本页说明如何使用标签控制生成的导航。

### 使用标签组织生成的页面

GitBook 使用标签在两种布局中组织生成的 API 参考页面。

使用 **每个标签一个页面**时，GitBook 会为每个标签创建一个页面。使用 **每个操作一个页面**，GitBook 会为每个操作创建一个页面，并使用标签在目录中对这些页面进行分组。

为了将相关操作分组，请为每个操作分配相同的标签：

<pre class="language-yaml" data-title="openapi.yaml"><code class="lang-yaml">paths:
  /pet:
    put:
<strong>      tags:
</strong><strong>        - pet
</strong>      summary: 更新一个现有宠物。
      description: 通过 Id 更新现有宠物。
      operationId: updatePet
</code></pre>

### 在目录中重新排序页面

生成的标签页面或标签组的顺序与 OpenAPI 中标签的顺序一致： `tags` 数组：

<pre class="language-yaml" data-title="openapi.yaml"><code class="lang-yaml">tags:
<strong>  - name: pet
</strong><strong>  - name: store
</strong><strong>  - name: user
</strong></code></pre>

未在以下内容中声明标签的操作： `tags` 数组仍会显示在 API 参考中。GitBook 会将这些标签放在已声明的标签之后，并按照它们首次出现在规范中的顺序排列。若要控制标签的位置、标题和图标，请在 `tags` 中声明该标签。

### 将页面嵌套到组中

若要构建多级导航，请在标签中使用 `x-parent` （或 `parent`）来定义层级。此方法适用于这两种页面结构：

<pre class="language-yaml" data-title="openapi.yaml"><code class="lang-yaml">tags:
  - name: everything
  - name: pet
<strong>    x-parent: everything
</strong>  - name: store
<strong>    x-parent: everything
</strong></code></pre>

上面的示例会创建如下目录：

```
全部
├── 宠物
└── 商店
```

如果 GitBook 生成了一个父页面且该页面没有描述，它会为其子页面显示基于卡片的布局。

### 自定义页面标题、图标和描述

你可以通过在以下位置使用自定义扩展来增强生成的标签页面和导航标签 `tags` 部分。所有 [Font Awesome 图标](https://fontawesome.com/search) 可通过 `x-page-icon`.

{% code title="openapi.yaml" %}

```yaml
tags:
  - name: pet
    # 在目录和页面中显示的页面标题
    x-page-title: 宠物
    # 在目录和页面标题旁显示的图标
    x-page-icon: dog
    # 显示在标题正上方的描述
    x-page-description: 宠物很棒！
    # 页面的内容
    description: 关于你的宠物的一切
```

{% endcode %}

### 使用 GitBook Blocks 构建丰富的描述

标签描述字段支持 GitBook Markdown，包括 [高级内容块](https://gitbook.com/docs/documentation/zh/chuang-jian-nei-rong/blocks.md) ，例如标签页：

{% code title="openapi.yaml" %}

```yaml
---
tags:
  - name: pet
    description: |
      这里是宠物的详细信息。

      {% tabs %}
      {% tab title="Dog" %}
      这里是狗
      {% endtab %}

      {% tab title="Cat" %}
      这里是猫
      {% endtab %}

      {% tab title="Rabbit" %}
      这里是兔子
      {% endtab %}
      {% endtabs %}
```

{% endcode %}

### 突出显示 schema

你可以使用 GitBook Markdown 在 GitBook 描述中高亮一个 schema。以下示例突出显示了 “petstore” 规范中的 “Pet” schema：

{% code title="openapi.yaml" %}

```yaml
---
tags:
  - name: pet
      description: |
          {% openapi-schemas spec="petstore" schemas="Pet" grouped="false" %}
              Pet 对象
          {% endopenapi-schemas %}
```

{% endcode %}

### 记录一个 webhook 端点

GitBook 支持 OpenAPI 3.1，包括 webhook 端点。

该 `webhooks` 字段属于 OpenAPI 3.1，因此你的规范必须声明 OpenAPI 3.1 版本。你可以直接在 OpenAPI 文件中定义 Webhook，GitBook 会将其与其他 API 操作一同呈现。有关 OpenAPI 文档的版本支持，请参阅 [OpenAPI 兼容性](https://gitbook.com/docs/documentation/zh/chuang-jian-nei-rong/openapi.md#openapi-compatibility).

{% code title="openapi.yaml" %}

```yaml
---
openapi: 3.1.0 # Webhook 从 OpenAPI 3.1 开始可用

webhooks:
  newPet:
    post:
      summary: 新宠物事件
      description: 系统中新宠物的信息
      requestBody:
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/Pet"
      responses:
        "200":
          description: 返回 200 状态，表示数据已成功接收
```

{% endcode %}


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://gitbook.com/docs/documentation/zh/chuang-jian-nei-rong/openapi/guides/structuring-your-api-reference.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
