Files
mintlify__docs/zh/api-playground/overview.mdx
locadex-agent[bot] 4b2d51b2b4 docs(locadex): add translations (#2795)
Co-authored-by: locadex-agent[bot] <217277504+locadex-agent[bot]@users.noreply.github.com>
2026-01-22 10:08:32 -08:00

175 lines
6.2 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: "Playground"
description: "让开发者可直接在文档中测试 API 端点。"
keywords: ["交互式", "API", "端点测试", "实时 API 请求", "请求构建器"]
---
<div id="overview">
## 概览
</div>
API playground 是一个交互式环境,便于用户测试和探索你的 API 端点。开发者可以构造 API 请求、提交请求,并在不离开文档的情况下查看响应。
参见 [触发更新](/zh/api/update/trigger) 以查看 API playground 的实际效果。
<Frame>
<img src="/images/playground/API-playground-light.png" alt="用于“触发更新”端点的 API playground。" className="block dark:hidden" />
<img src="/images/playground/API-playground-dark.png" alt="用于“触发更新”端点的 API playground。" className="hidden dark:block" />
</Frame>
Playground 会基于你的 OpenAPI 规范或 AsyncAPI 模式为端点生成交互式页面。若你修改了 API,Playground 会自动更新相关页面。
我们建议基于 OpenAPI 规范生成 API playground。或者,在你在 `docs.json` 中定义了基础 URL 和认证方式后,也可以手动创建 API 参考页面。
<div id="get-started">
## 入门
</div>
<Steps>
<Step title="添加你的 OpenAPI 规范文件。">
<Tip>
使用 [Swagger Editor](https://editor.swagger.io/) 或 [Mint CLI](https://www.npmjs.com/package/mint) 命令 `mint openapi-check <filename>` 验证你的 OpenAPI 规范文件。
</Tip>
```bash {3}
/your-project
|- docs.json
|- openapi.json
```
</Step>
<Step title="生成端点页面。">
更新你的 `docs.json` 以引用 OpenAPI 规范。
**要为 OpenAPI 规范中的所有端点自动生成页面**,请在任意导航元素中添加 `openapi` 属性。
此示例会为 `openapi.json` 中指定的每个端点生成一个页面,并将这些页面归类到 “API reference” 组中。
```json Generate all endpoint pages
"navigation": {
"groups": [
{
"group": "API reference",
"openapi": "openapi.json"
}
]
}
```
**若只为特定端点生成页面**,请在该导航元素的 `pages` 属性中列出这些端点。
此示例仅为 `GET /users` 和 `POST /users` 端点生成页面。若需生成其他端点页面,请将对应端点添加到 `pages` 数组中。
```json Generate specific endpoint pages
"navigation": {
"groups": [
{
"group": "API reference",
"openapi": "openapi.json",
"pages": [
"GET /users",
"POST /users"
]
}
]
}
```
</Step>
</Steps>
<div id="customize-your-playground">
## 自定义 playground
</div>
在 `docs.json` 中定义以下属性以自定义 API playground。
<ResponseField name="playground" type="object">
API playground 的配置。
<Expandable title="playground" defaultOpen="True">
<ResponseField name="display" type="&#x22;interactive&#x22; | &#x22;simple&#x22; | &#x22;none&#x22;">
API playground 的显示模式。
* `"interactive"`:显示交互式 playground。
* `"simple"`:显示可复制的端点,不包含 playground。
* `"none"`:不显示任何内容。
默认为 `interactive`。
</ResponseField>
<ResponseField name="proxy" type="boolean" defaultOpen="True">
是否通过代理服务器转发 API 请求。默认为 `true`。
</ResponseField>
</Expandable>
</ResponseField>
<ResponseField name="examples" type="object">
自动生成的 API 示例的配置。
<Expandable title="examples" defaultOpen="True">
<ResponseField name="languages" type="array of string">
自动生成的 API 代码片段所使用的示例语言。
语言按指定顺序显示。
</ResponseField>
<ResponseField name="defaults" type="&#x22;required&#x22; | &#x22;all&#x22;">
是否在 API 示例中显示可选参数。默认为 `all`。
</ResponseField>
<ResponseField name="prefill" type="boolean">
是否使用来自 schema 示例的数据预填充 API playground。启用后,playground 会根据 OpenAPI 规范中的示例值自动填充请求字段。默认为 `false`。
</ResponseField>
<ResponseField name="autogenerate" type="boolean">
是否根据 API 规范为端点生成代码示例。默认为 `true`。当设置为 `false` 时,只有手动编写的代码示例(来自 OpenAPI 规范中的 `x-codeSamples` 或 MDX 中的 `<RequestExample>` 组件)会显示在 API playground 中。
</ResponseField>
</Expandable>
</ResponseField>
<div id="example-configuration">
### 配置示例
</div>
此示例将 API playground 配置为可交互,并提供 cURL、Python 和 JavaScript 的示例代码片段。代码片段仅显示必填参数,且 playground 会为请求体预填示例值。
```json
{
"api": {
"playground": {
"display": "interactive"
},
"examples": {
"languages": ["curl", "python", "javascript"],
"defaults": "required",
"prefill": true
}
}
}
```
<div id="custom-endpoint-pages">
### 自定义 endpoint 页面
</div>
当你需要对 API 文档进行更精细的控制时,可以在 OpenAPI 规范中使用 `x-mint` 扩展,或者为各个 endpoint 创建单独的 MDX 页面。
这两种方式都可以让你:
* 自定义页面 metadata
* 添加示例等额外内容
* 按页面控制 playground 的行为
推荐使用 `x-mint` 扩展,这样你的所有 API 文档都可以从 OpenAPI 规范中自动生成,并集中维护在一个文件中。
对于小型 API,或者当你希望在单个页面上逐页试验更改时,推荐使用单独的 MDX 页面。
<div id="further-reading">
## 延伸阅读
</div>
* [OpenAPI 设置](/zh/api-playground/openapi-setup),了解更多关于如何创建 OpenAPI 文档的信息。
* [x-mint 扩展](/zh/api-playground/openapi-setup#x-mint-extension),了解更多关于如何自定义端点页面的信息。
* [MDX 设置](/zh/api-playground/mdx-setup),了解更多关于如何手动创建单个 API 参考页面的信息。
* [AsyncAPI 设置](/zh/api-playground/asyncapi-setup),了解更多关于如何创建 AsyncAPI 规范以生成 WebSocket 参考页面的信息。