> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bettertoken.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Gemini 画像編集 API

> BetterToken の Gemini API で Nano Banana の画像を編集します。参照画像と編集指示を送信し、オンラインでリクエストを試して画像データを読み取れます。

export const GeminiImageInput = ({labels}) => {
  const [images, setImages] = useState([]);
  const [busy, setBusy] = useState(false);
  const [feedback, setFeedback] = useState("");
  const selection = useRef(0);
  const inputId = useId();
  const readImages = async event => {
    const version = ++selection.current;
    const files = Array.from(event.target.files || []);
    setImages([]);
    setFeedback("");
    if (files.some(file => !["image/png", "image/jpeg"].includes(file.type))) {
      setFeedback(labels[4]);
      setBusy(false);
      return;
    }
    setBusy(true);
    try {
      const next = await Promise.all(files.map(file => new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.onload = () => resolve({
          name: file.name,
          type: file.type,
          data: String(reader.result).split(",")[1]
        });
        reader.onerror = reject;
        reader.onabort = reject;
        reader.readAsDataURL(file);
      })));
      if (version === selection.current) setImages(next);
    } catch {
      if (version === selection.current) setFeedback(labels[5]);
    } finally {
      if (version === selection.current) setBusy(false);
    }
  };
  const copy = async value => {
    try {
      await navigator.clipboard.writeText(value);
      setFeedback(labels[2]);
    } catch {
      setFeedback(labels[3]);
    }
  };
  return <section className="bt-gemini-input not-prose" aria-label={labels[0]}>
      <label htmlFor={inputId}>{labels[0]}</label>
      <input id={inputId} type="file" accept="image/png,image/jpeg" multiple onChange={readImages} />
      <p>{labels[7]}</p>
      <p role="status" aria-live="polite">{busy ? labels[6] : feedback}</p>
      {images.map((image, index) => <div key={index} className="bt-gemini-input-image">
          <div className="bt-gemini-input-header">
            <img src={"data:" + image.type + ";base64," + image.data} alt={image.name} />
            <span>{image.name}</span>
            <code>{image.type}</code>
            <button type="button" title={labels[1]} aria-label={labels[1] + " " + image.name} onClick={() => copy(image.data)}><Icon icon="copy" size={16} /></button>
          </div>
          <textarea aria-label={labels[1] + " " + image.name} rows={3} readOnly value={image.data} spellCheck={false} />
        </div>)}
    </section>;
};

Nano Banana Pro、2、2.1 に参照画像と編集指示を渡し、変更後の画像を生成します。

`POST /v1beta/models/{model}:generateContent`

テキストからの画像生成と画像編集は同じ API を使用します。画像編集ではリクエストに参照画像を追加します。

## 参照画像の準備

文章と各画像は別々の part に入れます。`inlineData.data` は Data URL 接頭辞を含まない base64 です。参照画像には PNG または JPEG を使います。この API は JSON を受け付け、multipart や画像 URL は使いません。

送信前に `REPLACE_WITH_IMAGE_BASE64` を自分の画像の base64 に置き換え、ファイルに合わせて `inlineData.mimeType` を設定してください。指示の例はカップ用です。別の被写体を使う場合は指示も変更してください。

<GeminiImageInput labels={["参照画像","base64 をコピー","コピーしました","下の base64 を選択してコピーしてください。","PNG または JPEG を選んでください。","画像を読み取れません。もう一度選んでください。","画像を準備中...","画像はローカルで変換されます。Playground の inlineData.data にコピーした base64 を入れ、inlineData.mimeType に表示された MIME 型を設定します。追加画像はそれぞれ別の part に入れてください。"]} />

## リクエストを試す

先に参照画像を準備してください。**x-goog-api-key** に BetterToken API Key を入力し、**model** を選択します。**contents** に編集指示と画像データを設定します。`inlineData.data` の置き換えと `inlineData.mimeType` を確認してから **Send** を押してください。

<Note>
  **Send** を押すと実際の生成リクエストが送信され、料金が発生する場合があります。同期応答を待ってください。タイムアウトや切断時は、利用履歴と結果を確認してから再送してください。
</Note>

3 モデルとも `1K`、`2K`、`4K` を利用できます。Nano Banana 2 と 2.1 は 14 種類、Pro は 10 種類の比率に対応しています。 `X-BetterToken-Max-Images` は返却・課金する画像数の上限で、生成枚数を保証しません。 [対応比率](/ja/api-reference/nano-banana#aspect-ratios)

<Warning>
  BetterToken API は Google のウェブ検索や画像検索を利用した画像生成に対応していません。検索ツールを追加しないでください。
</Warning>

非ストリーミングの `generateContent` を使用します。Files / `fileData`、Batch、Interactions、ストリーミングには対応していません。

## 応答を読み取る

画像は `candidates[].content.parts[].inlineData` にあります。すべての part を走査し、`thought: true` を除外して `data` をデコードし、`mimeType` に応じた拡張子を選びます。画像が先頭の part や PNG とは限りません。

## 関連ガイド

* [テキスト画像生成](/ja/api-reference/gemini-images-generations)
* [SDK、画像保存と継続編集](/ja/api-reference/nano-banana)
* [モデル紹介](/ja/model-updates/nano-banana)
* <a href={"https://bettertoken.ai/pricing"}>モデルカタログ</a>


## OpenAPI

````yaml api-reference/gemini-images-edits.ja.json POST /v1beta/models/{model}:generateContent
openapi: 3.1.0
info:
  title: Gemini 画像編集 API
  description: >-
    BetterToken の Gemini API で Nano Banana
    の画像を編集します。参照画像と編集指示を送信し、オンラインでリクエストを試して画像データを読み取れます。
  version: 1.0.0
servers:
  - url: https://bettertoken.ai
security:
  - geminiApiKey: []
paths:
  /v1beta/models/{model}:generateContent:
    post:
      summary: Gemini 画像編集 API
      description: >-
        Nano Banana Pro、2、2.1 に参照画像と編集指示を渡し、変更後の画像を生成します。 BetterToken API は
        Google のウェブ検索や画像検索を利用した画像生成に対応していません。検索ツールを追加しないでください。
      operationId: editGeminiImage_ja
      parameters:
        - name: model
          in: path
          required: true
          description: BetterToken のモデル ID。Nano Banana の 3 モデルから選びます。
          schema:
            type: string
            enum:
              - nano-banana-pro
              - nano-banana-2
              - nano-banana-2.1
            default: nano-banana-2.1
        - name: X-BetterToken-Max-Images
          in: header
          description: 返却・課金する画像数の上限です。枚数保証ではありません。既定値は 1。
          schema:
            type: integer
            minimum: 1
            maximum: 4
            default: 1
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - contents
              properties:
                contents:
                  type: array
                  description: 会話履歴。継続編集ではモデルの完全な content と thoughtSignature を保持します。
                  minItems: 1
                  items:
                    type: object
                    required:
                      - parts
                    properties:
                      role:
                        type: string
                        description: 新規リクエストは user、編集履歴のモデル応答は model。
                        enum:
                          - user
                          - model
                        default: user
                      parts:
                        type: array
                        description: 文章と画像。text と inlineData は別々の part に入れます。
                        minItems: 1
                        items:
                          anyOf:
                            - type: object
                              required:
                                - text
                              properties:
                                text:
                                  type: string
                                  description: 画像の説明または編集指示。
                                  example: カップを赤色に変更してください。形、背景、照明はそのままにしてください。
                              additionalProperties: true
                            - type: object
                              required:
                                - inlineData
                              properties:
                                inlineData:
                                  type: object
                                  description: base64 で埋め込んだ参照画像。
                                  required:
                                    - mimeType
                                    - data
                                  properties:
                                    mimeType:
                                      type: string
                                      description: 参照画像の MIME 型。
                                      enum:
                                        - image/png
                                        - image/jpeg
                                      example: image/png
                                    data:
                                      type: string
                                      description: >-
                                        Data URL の接頭辞を含まない、画像の元データの base64。送信前に
                                        REPLACE_WITH_IMAGE_BASE64 を自分の PNG または
                                        JPEG のデータに置き換えてください。
                                      contentEncoding: base64
                                      example: REPLACE_WITH_IMAGE_BASE64
                                  additionalProperties: false
                              additionalProperties: true
                    additionalProperties: false
                generationConfig:
                  type: object
                  description: 画像生成の設定。
                  properties:
                    responseModalities:
                      type: array
                      description: 画像出力を明示的に指定します。
                      items:
                        type: string
                        enum:
                          - IMAGE
                      default:
                        - IMAGE
                    candidateCount:
                      type: integer
                      enum:
                        - 1
                      default: 1
                      description: 省略するか整数 1 を指定。ルートに n を入れないでください。
                    imageConfig:
                      type: object
                      description: 出力画像の設定。
                      properties:
                        imageSize:
                          type: string
                          description: 出力解像度。K は大文字。
                          enum:
                            - 1K
                            - 2K
                            - 4K
                          default: 1K
                        aspectRatio:
                          type: string
                          description: 比率。Pro は 10 種類、2 と 2.1 は 1:4、4:1、1:8、8:1 も対応。
                          enum:
                            - '1:1'
                            - '1:4'
                            - '1:8'
                            - '2:3'
                            - '3:2'
                            - '3:4'
                            - '4:1'
                            - '4:3'
                            - '4:5'
                            - '5:4'
                            - '8:1'
                            - '9:16'
                            - '16:9'
                            - '21:9'
                          default: '1:1'
                      additionalProperties: false
                  additionalProperties: false
              additionalProperties: false
            example:
              contents:
                - role: user
                  parts:
                    - text: カップを赤色に変更してください。形、背景、照明はそのままにしてください。
                    - inlineData:
                        mimeType: image/png
                        data: REPLACE_WITH_IMAGE_BASE64
              generationConfig:
                responseModalities:
                  - IMAGE
                candidateCount: 1
                imageConfig:
                  imageSize: 1K
                  aspectRatio: '1:1'
      responses:
        '200':
          description: 生成結果。最終画像の part と finishReason を確認します。
          content:
            application/json:
              schema:
                type: object
                properties:
                  candidates:
                    type: array
                    items:
                      type: object
                      properties:
                        content:
                          type: object
                          properties:
                            role:
                              type: string
                            parts:
                              type: array
                              items:
                                type: object
                                properties:
                                  text:
                                    type: string
                                  thought:
                                    type: boolean
                                  thoughtSignature:
                                    type: string
                                  inlineData:
                                    type: object
                                    properties:
                                      mimeType:
                                        type: string
                                      data:
                                        type: string
                                        contentEncoding: base64
                        finishReason:
                          type: string
                  promptFeedback:
                    type: object
                    additionalProperties: true
                  usageMetadata:
                    type: object
                    additionalProperties: true
              example:
                candidates:
                  - content:
                      role: model
                      parts:
                        - inlineData:
                            mimeType: image/png
                            data: BASE64_IMAGE_DATA
                    finishReason: STOP
        '400':
          description: 無効なパラメータまたは未対応機能。
        '401':
          description: API Key が未指定または無効。
        '402':
          description: 残高不足。
        '429':
          description: レート制限または上流サービスの混雑。
        '500':
          description: 生成エラー。再送前に結果を確認してください。
components:
  securitySchemes:
    geminiApiKey:
      type: apiKey
      in: header
      name: x-goog-api-key
      description: BetterToken API Key。

````

This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.