> 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/kontentsuwo/openapi/guides/adding-custom-code-samples.md).

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

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 dynamically by asking a question.

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

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
