For the complete documentation index, see llms.txt. This page is also available as Markdown.

为 API 编写文档

将 OpenAPI 规范添加到页面,并让访客通过交互式模块直接在页面上测试端点。

手动编写 REST API 文档可能是一个耗时的过程。幸运的是,GitBook 通过允许你导入 OpenAPI 文档来简化这项任务,这些文档详细说明了你的 API 结构和功能。

OpenAPI 规范(OAS)是开发者用来编写 REST API 文档的框架。它以 JSON 或 YAML 编写,概述了你的所有端点、参数、模式和身份验证方案。

一旦导入 GitBook,这些文档就会被转换为可交互、可测试的 API 区块,以可视化方式展示你的 API 方法——无论规范是作为文件提供还是从 URL 加载。

OpenAPI 兼容性

GitBook 支持导入并渲染以下规范版本:

Add a new pet to the store.

post

Add a new pet to the store.

必需范围
此端点需要以下范围:
  • : modify pets in your account
  • : read your pets
授权
OAuth2implicit必填
Authorization URL:
请求体
idinteger · int64可选Example: 10
namestring必填Example: doggie
photoUrlsstring[]必填
statusstring · enum可选

pet status in the store

可能的值:
响应
200

Successful operation

idinteger · int64可选Example: 10
namestring必填Example: doggie
photoUrlsstring[]必填
statusstring · enum可选

pet status in the store

可能的值:
post/pet
POST /api/v3/pet HTTP/1.1
Authorization: Bearer YOUR_OAUTH2_TOKEN
Content-Type: application/json
Accept: */*
Content-Length: 133

{
  "id": 10,
  "name": "doggie",
  "category": {
    "id": 1,
    "name": "Dogs"
  },
  "photoUrls": [
    "text"
  ],
  "tags": [
    {
      "id": 1,
      "name": "text"
    }
  ],
  "status": "available"
}
{
  "id": 10,
  "name": "doggie",
  "category": {
    "id": 1,
    "name": "Dogs"
  },
  "photoUrls": [
    "text"
  ],
  "tags": [
    {
      "id": 1,
      "name": "text"
    }
  ],
  "status": "available"
}

试一试(由 Scalar 提供支持)

GitBook 的 OpenAPI 区块还支持“试用”功能,允许访问者使用编辑器中填写的数据和参数来测试你的 API 方法。

因为它由 Scalar,你无需离开文档就能查看你的 API 方法实际运行。上方有一个示例。

常见问题

为什么我的规范没有加载?

注意: 此信息仅适用于 通过 URL 添加的规范.

如果你是通过 URL 添加规范的,那么你的 API 必须 允许跨源 来自文档站点的 GET 请求。在你的 API 的 CORS 设置中,允许托管文档的确切来源(例如, https://your-site.gitbook.io 或 https://docs.example.com)。 如果你的端点是公开的且不使用凭证,你也可以返回: Access-Control-Allow-Origin: *

最后更新于

这有帮助吗?