# 添加 SDK 示例 (/zh/api-playground/adding-sdk-examples)

<!-- agent-signals: reading_time_min: 1 · est_tokens: 249 · updated: 2026-07-30 -->
Related: [操作台](/zh/api-playground/overview.md), [OpenAPI 设置](/zh/api-playground/openapi-setup.md), [复杂数据类型](/zh/api-playground/complex-data-types.md), [管理页面可见性](/zh/api-playground/managing-page-visibility.md), [多种响应](/zh/api-playground/multiple-responses.md), [手动创建 API 文档页面](/zh/api-playground/mdx-setup.md)

如果用户通过 SDK 而不是直接通过网络请求与你的 API 交互，你可以使用 `x-codeSamples` 扩展在 OpenAPI 文档中添加代码示例，并将其展示在 OpenAPI 页面中。

将此属性添加到任何请求方法。它具有以下模式。

<ParamField body="lang" type="string">
  代码示例所使用的编程语言。
</ParamField>

<ParamField body="label" type="string">
  示例的标签。在为同一端点提供多个示例时很有用。
</ParamField>

<ParamField body="source" type="string">
  示例的源代码。
</ParamField>

下面是一个植物追踪应用的代码示例，该应用同时提供 Bash CLI 工具和 JavaScript SDK。

```yaml
paths:
  /plants:
    get:
      # ...
      x-codeSamples:
        - lang: bash
          label: 列出所有未浇水的植物
          source: |
            planter list -u
        - lang: javascript
          label: 列出所有未浇水的植物
          source: |
            const planter = require('planter');
            planter.list({ unwatered: true });
        - lang: bash
          label: 列出所有盆栽植物
          source: |
            planter list -p
        - lang: javascript
          label: 列出所有盆栽植物
          source: |
            const planter = require('planter');
            planter.list({ potted: true });
```
