> 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/configuring-the-test-it-button.md).

# 配置“测试它”按钮

您可以在 GitBook 中使用多个 OpenAPI 扩展来配置“Test it”按钮及其配套窗口。这些扩展可以帮助改进并配置供用户使用的测试套件。

### 隐藏“Test it”按钮

你可以通过添加 `x-hideTryItPanel` 到某个端点，或添加到 OpenAPI 规范的根级别，来隐藏端点上的“试一试”按钮。

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

```yaml
openapi: '3.0'
info: ...
tags: [...]
paths:
  /example:
    get:
      summary: 示例摘要
      description: 示例描述
      operationId: examplePath
      responses: [...]
      parameters: [...]
      x-hideTryItPanel: true
```

{% endcode %}

### 代理“Test it”请求

某些 API 会阻止浏览器请求，通常是因为 CORS。

路由 **试一试** 通过 GitBook 转发流量，只需在规范中添加 `x-enable-proxy` 即可。

示例请参阅 [使用 OpenAPI 代理](https://gitbook.com/docs/documentation/zh/chuang-jian-nei-rong/openapi/guides/using-openapi-proxy.md) 。

### 在测试窗口中启用身份验证

只有在规范中声明了身份验证方式，请求运行器才能显示和应用该方式。在 `components.securitySchemes`下定义安全方案，然后通过 `security` 在全局范围内应用（适用于所有操作），或按操作单独应用（覆盖全局设置）。

#### 声明你的身份验证方案

下面是常见模式。请在 YAML 中使用直引号。

{% tabs %}
{% tab title="HTTP Bearer（例如 JWT）" %}

```yaml
openapi: '3.0.3'
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT
```

{% endtab %}

{% tab title="位于请求头中的 API 密钥" %}

```yaml
openapi: '3.0.3'
components:
  securitySchemes:
    apiKeyAuth:
      type: apiKey
      in: header
      name: X-API-Key
```

{% endtab %}

{% tab title="OAuth2（authorizationCode）" %}

```yaml
openapi: '3.0.3'
components:
  securitySchemes:
    oauth2:
      type: oauth2
      flows:
        authorizationCode:
          authorizationUrl: 'https://auth.example.com/oauth/authorize'
          tokenUrl: 'https://auth.example.com/oauth/token'
          scopes:
            read:items: '读取项目'
            write:items: '写入项目'
```

{% endtab %}
{% endtabs %}

#### 全局或按操作应用方案

{% tabs %}
{% tab title="全局" %}

```yaml
openapi: '3.0.3'
security:
  - bearerAuth: []
paths: ...
```

{% endtab %}

{% tab title="按操作" %}

```yaml
paths:
  /reports:
    get:
      summary: 获取报告
      security:
        - apiKeyAuth: []
      responses:
        '200':
          description: 确定
```

{% endtab %}
{% endtabs %}

### 使用 `servers`

控制端点 URL。请求运行器会将请求发送到你在 `servers` 数组中定义的 URL。声明一个或多个服务器；也可以使用变量对其进行参数化。

{% tabs %}
{% tab title="单个服务器" %}

```yaml
openapi: '3.0.3'
servers:
  - url: https://instance.api.region.example.cloud
```

{% endtab %}

{% tab title="多个服务器" %}

```yaml
servers:
  - url: https://api.example.com
    description: 生产环境
  - url: https://staging-api.example.com
    description: 预发布环境
```

{% endtab %}

{% tab title="服务器变量" %}

```yaml
servers:
  - url: https://{instance}.api.{region}.example.cloud
    variables:
      instance:
        default: acme
        description: 你的租户或实例 slug
      region:
        default: eu
        enum:
          - eu
          - us
          - ap
        description: 区域部署
```

{% endtab %}

{% tab title="按操作的服务器" %}

<pre class="language-yaml"><code class="lang-yaml"><strong>paths:
</strong>  /reports:
    get:
      summary: 获取报告
      servers:
        - url: https://reports.api.example.com
      responses:
        '200':
          description: 确定
</code></pre>

{% endtab %}
{% endtabs %}


---

# 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/configuring-the-test-it-button.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.
