名称: api-docs 描述: 创建OpenAPI规范、SDK文档和API文档。用于API文档或客户端库生成。
API文档
创建全面的API文档和OpenAPI规范。
何时使用
- 文档化新API
- 创建OpenAPI/Swagger规范
- 生成SDK文档
- 编写API指南
- 创建Postman集合
OpenAPI模板
openapi: 3.0.3
info:
title: API名称
version: 1.0.0
description: API描述
servers:
- url: https://api.example.com/v1
description: 生产环境
paths:
/users:
get:
summary: 列出用户
operationId: listUsers
parameters:
- name: limit
in: query
schema:
type: integer
default: 20
responses:
"200":
description: 成功
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/User"
post:
summary: 创建用户
operationId: createUser
requestBody:
required: true
content:
application/json:
schema:
$ref: "#/components/schemas/CreateUser"
responses:
"201":
description: 已创建
content:
application/json:
schema:
$ref: "#/components/schemas/User"
components:
schemas:
User:
type: object
properties:
id:
type: string
format: uuid
name:
type: string
email:
type: string
format: email
required:
- id
- name
- email
CreateUser:
type: object
properties:
name:
type: string
email:
type: string
format: email
required:
- name
- email
securitySchemes:
bearerAuth:
type: http
scheme: bearer
bearerFormat: JWT
security:
- bearerAuth: []
文档结构
# API参考
## 认证
所有请求都需要在Authorization头部中包含Bearer令牌。
## 端点
### 用户
#### 列出用户
`GET /users`
**参数:**
| 名称 | 类型 | 必填 | 描述 |
| ----- | ------- | -------- | ---------------- |
| limit | 整数 | 否 | 每页结果数 |
**响应:**
```json
[{ "id": "...", "name": "...", "email": "..." }]
```
#### 创建用户
`POST /users`
**请求体:**
```json
{ "name": "John", "email": "john@example.com" }
```
## 错误代码
| 代码 | 描述 |
| ---- | --------------------- |
| 400 | 错误请求 |
| 401 | 未授权 |
| 404 | 未找到 |
| 500 | 内部服务器错误 |
示例
输入: “文档化用户API” 操作: 创建包含所有端点、模式、示例、错误代码的OpenAPI规范
输入: “从代码生成API文档” 操作: 提取端点、推断类型、创建结构化文档