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

# Nano Banana 2

> 使用 nano-banana-2 通过 OpenAI Images API 调用 Gemini 3.1 Flash Image

> 更新日期：2026-06-06

# Nano Banana 2

```
POST /v1/images/generations
```

`nano-banana-2` 是 `nano-banana-2` 的公开别名。用户侧统一使用 OpenAI Images 兼容协议；服务端会把 `output_format`、`output_compression`、`resolution`、`aspect_ratio` 等统一参数转换成 Gemini / Vertex 原生请求。

<Warning>
  `nano-banana-2` 属于长耗时图片模型。生产接入请优先使用主承接线路 `https://api.crazyrouter.com/v1`，完整生成端点为 `https://api.crazyrouter.com/v1/images/generations`。账号登录、充值和控制台仍使用 `https://crazyrouter.com`。
</Warning>

## 模型名

| 推荐模型名           | 等价底层模型          |
| --------------- | --------------- |
| `nano-banana-2` | `nano-banana-2` |

推荐在客户代码里使用 `nano-banana-2`。如果你的系统已经直接写了 `nano-banana-2`，也可以继续通过同一个 `/v1/images/generations` 入口调用。

## 请求参数

| 参数                   | 类型                 | 必填 | 说明                                     |
| -------------------- | ------------------ | -- | -------------------------------------- |
| `model`              | string             | 是  | 推荐 `nano-banana-2`；也兼容 `nano-banana-2` |
| `prompt`             | string             | 是  | 生成或编辑指令                                |
| `image_input`        | string 或 string\[] | 否  | 参考图 URL；不传则为文生图                        |
| `resolution`         | string             | 否  | `1K`、`2K`、`4K`                         |
| `aspect_ratio`       | string             | 否  | 如 `1:1`、`16:9`、`9:16`、`21:9`、`auto`    |
| `output_format`      | string             | 否  | `png`、`jpeg` / `jpg`                   |
| `output_compression` | integer            | 否  | `0-100`，用于 JPEG 压缩质量                   |
| `n`                  | integer            | 否  | 生成数量，默认 `1`                            |

<Note>
  不要在客户请求里传 Gemini 原生字段，例如 `generationConfig`、`imageConfig`、`imageOutputOptions`、`image_output_options`。这些字段由 Crazyrouter 在服务端按当前上游协议自动生成。
</Note>

## 文生图示例

<CodeGroup>
  ```bash cURL theme={null}
  curl -X POST https://api.crazyrouter.com/v1/images/generations \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer YOUR_API_KEY" \
    -d '{
      "model": "nano-banana-2",
      "prompt": "A clean ecommerce hero shot of a red apple on a white background",
      "resolution": "1K",
      "aspect_ratio": "16:9",
      "output_format": "jpeg",
      "output_compression": 85
    }'
  ```

  ```python Python theme={null}
  from openai import OpenAI

  client = OpenAI(
      api_key="YOUR_API_KEY",
      base_url="https://api.crazyrouter.com/v1",
  )

  response = client.images.generate(
      model="nano-banana-2",
      prompt="A clean ecommerce hero shot of a red apple on a white background",
      n=1,
      extra_body={
          "resolution": "1K",
          "aspect_ratio": "16:9",
          "output_format": "jpeg",
          "output_compression": 85,
      },
  )

  print(response.data[0].url or response.data[0].b64_json)
  ```
</CodeGroup>

## 参考图编辑示例

```bash cURL theme={null}
curl -X POST https://api.crazyrouter.com/v1/images/generations \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "model": "nano-banana-2",
    "prompt": "把参考图改成横幅广告图，保留主体，背景更干净",
    "image_input": [
      "https://example.com/reference.png"
    ],
    "resolution": "1K",
    "aspect_ratio": "16:9",
    "output_format": "png"
  }'
```

## 响应

响应保持 OpenAI Images 风格。默认优先读取 `data[0].url`；少数兼容渠道可能返回 `data[0].b64_json`。

```json theme={null}
{
  "created": 1778990000,
  "data": [
    {
      "url": "https://media.crazyrouter.com/task-artifacts/2026/05/17/sync-image/request-id-1.png"
    }
  ]
}
```

## 迁移说明

旧代码如果直接调用：

```text theme={null}
POST /v1beta/models/nano-banana-2:generateContent
```

建议迁移到：

```text theme={null}
POST https://api.crazyrouter.com/v1/images/generations
```

并把 Gemini 原生参数改成统一参数：

| Gemini 原生概念                             | Crazyrouter 统一参数     |
| --------------------------------------- | -------------------- |
| `imageConfig.aspectRatio`               | `aspect_ratio`       |
| `imageConfig.imageSize`                 | `resolution`         |
| `imageOutputOptions.mimeType`           | `output_format`      |
| `imageOutputOptions.compressionQuality` | `output_compression` |
