> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify-mintlify-add-hello-world-quickstart-48843.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# MDX 设置

> 使用 `MDX` 为 API 端点生成文档页面

你可以在单独的 `MDX` 文件中手动定义 API 端点，而不是使用 OpenAPI规范。此方法为自定义内容提供灵活性，但我们建议在大多数项目中从 OpenAPI规范 文件生成 API 文档，因为这样更易维护且功能更完善。不过，为 API 创建 `MDX` 页面在记录小型 API 或用于原型验证时也很有用。

要使用 `MDX` 为 API 端点生成页面，请在 `docs.json` 中配置 API 设置，为每个端点创建对应的 `MDX` 文件，并使用 `<ParamFields />` 等组件定义参数。基于这些定义，Mintlify 会生成交互式 API交互测试台、请求示例和响应示例。

<Steps>
  <Step title="配置你的 API">
    在 `docs.json` 文件中定义基础 URL 和认证方式：

    ```json
     "api": {
      "mdx": {
        "server": "https://mintlify.com/api", // string array for multiple base URLs
        "auth": {
          "method": "key",
          "name": "x-api-key" // options: bearer, basic, key.
        }
      }
    }
    ```

    如果你想隐藏 API交互测试台，请使用 `display` 字段。隐藏交互测试台时无需配置认证方式。

    ```json
    "api": {
      "playground": {
        "display": "none"
      }
    }
    ```

    在 [Settings](/zh/settings#api-configurations) 中查看完整的 API 配置项列表。
  </Step>

  <Step title="创建端点页面">
    每个 API 端点页面应有一个对应的 `MDX` 文件。在文件头部（frontmatter）定义 `title` 和 `api`：

    ```mdx
    ---
    title: 'Create new user'
    api: 'POST https://api.mintlify.com/user'
    ---
    ```

    你可以通过在路径中加入参数名并用 `{}` 包裹来指定路径参数：

    ```bash
    https://api.example.com/v1/endpoint/{userId}
    ```

    <Note>
      如果你在 `docs.json` 中配置了 `server` 字段，你可以使用相对路径（例如 `/v1/endpoint`）。
    </Note>

    你可以在该页面的 frontmatter 中添加 `playground` 来覆盖全局的 API交互测试台显示模式：

    ```mdx
    ---
    title: 'Create new user'
    api: 'POST https://api.mintlify.com/user'
    playground: 'none'
    ---
    ```

    * `playground: 'interactive'` - 显示交互式测试台。
    * `playground: 'simple'` - 仅显示可复制的端点，不包含测试台。
    * `playground: 'none'` - 隐藏测试台。
  </Step>

  <Step title="将端点添加到文档中">
    将这些页面的路径添加到 `docs.json` 的 `navigation` 字段中，以显示在侧边栏。在 [Navigation](/zh/navigation) 中了解更多文档结构的组织方式。
  </Step>
</Steps>

<div id="enabling-authentication">
  ## 启用认证
</div>

你可以在 `docs.json` 中添加一种认证方式，使其在所有页面全局生效，或者按页面单独配置。

如果同时设置了全局和页面级的认证方式，页面级设置将覆盖全局设置。

<div id="bearer-token">
  ### Bearer token
</div>

<CodeGroup>
  ```json docs.json
  "api": {
      "mdx": {
        "auth": {
          "method": "bearer"
        }
      }
  }
  ```

  ```mdx Page Metadata
  ---
  title: "Your page title"
  authMethod: "bearer"
  ---
  ```
</CodeGroup>

<div id="basic-authentication">
  ### Basic authentication
</div>

<CodeGroup>
  ```json docs.json
  "api": {
      "mdx": {
        "auth": {
          "method": "basic"
        }
      }
  }
  ```

  ```mdx Page Metadata
  ---
  title: "Your page title"
  authMethod: "basic"
  ---
  ```
</CodeGroup>

<div id="api-key">
  ### API key
</div>

<CodeGroup>
  ```json docs.json
  "api": {
      "mdx": {
        "auth": {
          "method": "key",
          "name": "x-api-key"
        }
      }
  }
  ```

  ```mdx Page Metadata
  ---
  title: "Your page title"
  authMethod: "key"
  ---
  ```
</CodeGroup>

<div id="none">
  ### None
</div>

在 docs.json 中设置默认认证后，`none` 方式可用于在特定端点禁用认证。

<CodeGroup>
  ```mdx Page Metadata
  ---
  title: "Your page title"
  authMethod: "none"
  ---
  ```
</CodeGroup>
