> For the complete documentation index, see [llms.txt](https://gitbook.com/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gitbook.com/docs/documentation/ja-gitbook-documentation/kontentsuwosuru/openapi/guides/adding-custom-code-samples.md).

# カスタムコードサンプルの追加

API エンドポイントと一緒に表示するカスタムコードサンプルの設定方法を学びましょう

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）。
  * `label`: コードブロックの短いラベル。
  * `source`: 実際のコードスニペット。

### クイックガイド

<details>

<summary>Fern からカスタムコードサンプルを追加するにはどうすればよいですか？</summary>

Fern で生成された SDK の例を、OpenAPI 定義内のカスタムコードサンプルとして使用します。

このガイドでは、生成された例の見つけ方、フォーマット `x-codeSamples`方法、および API 操作への追加方法を説明します。

<button type="button" class="button secondary" data-action="ask" data-query="How do I add custom code samples from Fern to my GitBook API documentation? Show me how to find Fern-generated SDK examples, format x-codeSamples, and add them to an OpenAPI operation. Include links to the relevant docs pages." data-icon="gitbook-assistant">ガイドを開く</button>

</details>

<details>

<summary>Stainless からカスタムコードサンプルを追加するにはどうすればよいですか？</summary>

Stainless で生成された SDK の例を、OpenAPI 定義内のカスタムコードサンプルとして使用します。

このガイドでは、生成された例の見つけ方、フォーマット `x-codeSamples`方法、および API 操作への追加方法を説明します。

<button type="button" class="button secondary" data-action="ask" data-query="How do I add custom code samples from Stainless to my GitBook API documentation? Show me how to find Stainless-generated SDK examples, format x-codeSamples, and add them to an OpenAPI operation. Include links to the relevant docs pages." data-icon="gitbook-assistant">ガイドを開く</button>

</details>

<details>

<summary>HeyAPI からカスタムコードサンプルを追加するにはどうすればよいですか？</summary>

HeyAPI で生成された SDK の例を、OpenAPI 定義内のカスタムコードサンプルとして使用します。

このガイドでは、生成された例の見つけ方、フォーマット `x-codeSamples`方法、および API 操作への追加方法を説明します。

<button type="button" class="button secondary" data-action="ask" data-query="How do I add custom code samples from HeyAPI to my GitBook API documentation? Show me how to find HeyAPI-generated SDK examples, format x-codeSamples, and add them to an OpenAPI operation. Include links to the relevant docs pages." data-icon="gitbook-assistant">ガイドを開く</button>

</details>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://gitbook.com/docs/documentation/ja-gitbook-documentation/kontentsuwosuru/openapi/guides/adding-custom-code-samples.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
