Gemini Banana 影像生成 (Nano Banana)

Google Gemini 系列同步影像生成 (T2I / I2I / 多圖合成 / 多輪編輯, 原生 Interactions API 格式)

POST /v1/images/generations

鉴权: {'type': 'bearer', 'description': '在 HTTP header `Authorization: Bearer sk-xxx` 中传入您的 API Key'}

**Gemini Banana (Nano Banana)** 是 Google Gemini 系列的多模態影像生成模型, 支援: - **文生圖** (Text-to-Image) - **影像編輯** (Image-to-Image, 傳入 1 張參考圖 + 文本指令) - **多圖合成** (Multi-Image Composition) — `gemini-3.1-flash-image-preview` 支援最多 **14 張參考圖** (10 張物體 + 4 張人物) - **多輪編輯** (服務端持有對話歷史, 通過 `previous_interaction_id` 連結迭代修改) ## 呼叫方式 按 **Google Gemini Interactions API** (2026-05 新協議, 欄位全 snake_case) 傳 body, 同步返回。 客戶端繼續使用我方 OpenAI 相容路徑 `https://api.router.ai/v1/images/generations`, 不需要直連 Google。 ## 支援的模型 | Model | 說明 | |---|---| | `gemini-3.1-flash-image-preview` | Nano Banana 主力推薦, 4 檔解析度 (512px/1K/2K/4K), 含 thinking 模式, 14 種比例 | | `gemini-3-pro-image-preview` | Nano Banana 專業版, 含 Thinking 推理 + 高保真文本渲染 | | `gemini-2.5-flash-image` | Nano Banana 經典 (GA), 高吞吐低延遲, 1K 單檔 | | `gemini-2.5-flash-image-preview` | Nano Banana 經典預覽版 (相容老 SDK 別名) | ## 輸出解析度配置 通過 `response_format.image_size` 指定: | image_size | 實際尺寸 (含 aspect_ratio=1:1) | 適用模型 | |---|---|---| | `"512px"` | 512×512 (注意 **px 字尾**) | 僅 `gemini-3.1-flash-image-preview` | | `"1K"` | 1024×1024 | 全部 (推薦預設) | | `"2K"` | 2048×2048 | gemini-3.1-flash / gemini-3-pro | | `"4K"` | 4096×4096 | gemini-3.1-flash / gemini-3-pro | **`aspect_ratio` 取值** (`gemini-3.1-flash` 支援 14 種): `"1:1"` / `"16:9"` / `"9:16"` / `"3:2"` / `"2:3"` / `"4:3"` / `"3:4"` / `"5:4"` / `"4:5"` + `"1:4"` / `"4:1"` / `"1:8"` / `"8:1"` (僅 `gemini-3.1-flash`) 不傳 `response_format` 時走預設 (通常 1K 1:1)。 ## 欄位命名 REST API 全部 **snake_case** (`response_format`, `mime_type`, `image_size`, `aspect_ratio`, `previous_interaction_id`, `generation_config`, `system_instruction` 等)。 ## 多輪編輯鏈 (重要) 服務端預設持有對話歷史 (`store: true`)。多輪編輯 1 張圖時: 1. 首輪 POST 拿到 `id` (`v1_xxx...`) 2. 第二輪把上次 `id` 放入 `previous_interaction_id` 欄位, 重新 POST (無需重傳圖片, 服務端記得) **資料保留**: 付費層 55 天, 免費層 1 天。 參考: [Google Interactions Image Generation](https://ai.google.dev/gemini-api/docs/interactions/image-generation)

请求头

AuthorizationstringrequiredBearer sk-xxx
Content-Typestringrequired

请求体

modelstringrequired模型 ID, 見上方支援的模型列表
inputstring|arrayrequired輸入內容. 單字串 (`"a red apple"`) 或 input block 陣列 (混合 text + image)
    typestringinput block 類型 (僅 array 形態)
    textstring文本提示 (type=text 時使用)
    mime_typestring影像 MIME 類型 (type=image 時使用)
    datastring影像 base64 編碼字串 (不含 data: URI prefix, type=image 時使用)
system_instructionstring系統提示 (可選, 控制模型行為)
response_formatobject輸出格式 (可選). 不傳時走預設 1K 1:1
    typestring響應類型, 影像生成必傳 `image`
    mime_typestring輸出影像 MIME 類型
    image_sizestring輸出解析度. `512px` 僅 3.1 Flash 支援 (注意 `px` 字尾); 其他模型僅 1K/2K/4K
    aspect_ratiostring寬高比. 14 種 (3.1 Flash 全部支援; 其他模型僅 9 種基礎)
generation_configobject生成參數 (可選)
    thinking_levelstring思考強度 (gemini-3.1-flash / gemini-3-pro 支援). 取 `minimal` (預設快速) 或 `high` (高品質推理)
    temperaturenumber取樣溫度 (0.0-2.0, 影響生成多樣性)
previous_interaction_idstring多輪連結. 上一次 POST 響應中的 `id` (`v1_xxx`); 服務端持有 history, 客戶端無需重傳上下文
toolsarray工具呼叫 (可選). 注: `google_search` 類工具會被閘道器入口安全過濾層移除, 詳見提示
storeboolean服務端是否持有對話歷史. `true` (預設) 服務端記憶; `false` 客戶端自己維護 history

响应

调用示例

# 1. 文生图 (T2I, input 单字符串, 默认 1K 1:1)
curl -X POST https://api.router.ai/v1/images/generations \
  -H "Authorization: Bearer sk-xxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemini-3.1-flash-image-preview",
    "input": "Create a picture of a nano banana dish in a fancy restaurant with a Gemini theme"
  }'

# 2. T2I + 指定分辨率 2K 16:9 (response_format)
curl -X POST https://api.router.ai/v1/images/generations \
  -H "Authorization: Bearer sk-xxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemini-3.1-flash-image-preview",
    "input": "a futuristic city at sunset, photorealistic, cinematic",
    "response_format": {
      "type": "image",
      "mime_type": "image/jpeg",
      "aspect_ratio": "16:9",
      "image_size": "2K"
    }
  }'

# 3. I2I 图像编辑 (input 数组, text + image base64)
curl -X POST https://api.router.ai/v1/images/generations \
  -H "Authorization: Bearer sk-xxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemini-3.1-flash-image-preview",
    "input": [
      {"type": "text", "text": "Create a picture of my cat eating a nano-banana in a fancy restaurant under the Gemini constellation"},
      {"type": "image", "mime_type": "image/jpeg", "data": "<BASE64_IMAGE_DATA>"}
    ]
  }'

# 4. 多图合成 (Multi-Image Composition, 多张参考图)
curl -X POST https://api.router.ai/v1/images/generations \
  -H "Authorization: Bearer sk-xxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemini-3.1-flash-image-preview",
    "input": [
      {"type": "text", "text": "An office group photo of these people, they are making funny faces."},
      {"type": "image", "mime_type": "image/png", "data": "<BASE64_DATA_IMG_1>"},
      {"type": "image", "mime_type": "image/png", "data": "<BASE64_DATA_IMG_2>"},
      {"type": "image", "mime_type": "image/png", "data": "<BASE64_DATA_IMG_3>"}
    ],
    "response_format": {
      "type": "image",
      "mime_type": "image/jpeg",
      "aspect_ratio": "5:4",
      "image_size": "2K"
    }
  }'

# 5. 多轮编辑链 (上次 id 放入 previous_interaction_id, 无需重传图片)
curl -X POST https://api.router.ai/v1/images/generations \
  -H "Authorization: Bearer sk-xxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemini-3.1-flash-image-preview",
    "input": "Update this infographic to be in Spanish. Do not change any other elements of the image.",
    "previous_interaction_id": "v1_ChdEdGNPYXY3bkhNV00xZThQNjlyaXdBMBIXRHRjT2F2N25ITVdNMWU4UDY5cml3QTA",
    "response_format": {
      "type": "image",
      "mime_type": "image/jpeg",
      "aspect_ratio": "16:9",
      "image_size": "2K"
    }
  }'

# 6. Thinking 模式 (gemini-3.1-flash / gemini-3-pro 支持; minimal 默认 / high 推理强度)
curl -X POST https://api.router.ai/v1/images/generations \
  -H "Authorization: Bearer sk-xxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemini-3-pro-image-preview",
    "input": "draw a step-by-step guide on how to bake a banana bread",
    "generation_config": {
      "thinking_level": "high"
    },
    "response_format": {
      "type": "image",
      "image_size": "1K"
    }
  }'

API 文件