# 添加自定义代码示例

GitBook 可以自动为每个 API 操作生成通用代码示例。如果您更希望展示自定义或更详细的代码片段，请添加 `x-codeSamples` 到您的 OpenAPI 定义中。这样，您可以控制端点的演示方式，并提供特定语言或 SDK 的示例。

{% code title="openapi.yaml" %}

```yaml
paths:
  /users:
    get:
      summary: 获取用户
      x-codeSamples:
        - lang: JavaScript
          label: Node SDK
          source: |
            import { createAPIClient } from 'my-api-sdk';

            const client = createAPIClient({ apiKey: 'my-api-key' });
            client.users.list().then(users => {
              console.log(users);
            });
        - lang: Java
          label: Java SDK
          source: |
            MyApiClient client = new MyApiClient("my-api-key");
            List<User> users = client.getUsers();
            System.out.println(users);
```

{% endcode %}

**关键点**

* `x-codeSamples` 是一个代码示例对象数组。
* 每个对象定义：
  * `lang`：代码的语言（例如，JavaScript、Java）。
  * `标签`：代码块的简短标签。
  * `source`：实际的代码片段。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gitbook.com/docs/documentation/zh/api-references/guides/adding-custom-code-samples.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
