# **Nano-banana 图像生成 API**

#### [下载本文档MD文件](https://beeroute.ai/doc/zh/coding/nano-banana.md)

**接口路径**：`POST /v1/images/generations`

**推荐模型**：

`nano-banana-2.1`（最新款，基于 Gemini 3.6 Flash：1K–4K 输出，最多 14 张参考图，价格约为上一代的一半）

`nano-banana-pro`（画质最高，基于 Gemini 3 Pro Image：适合 4K 海报和文字较多的图片）

旧模型（`nano-banana`、`nano-banana-2`、`nano-banana-2-lite`）仍可调用，用于兼容已有项目；新项目请使用上面两个模型。

**注意：生成图片URL为临时链接（2小时有效），重要作品请保存本地。**

---

### **模型对比**
| 特性 | `nano-banana-2.1` | `nano-banana-pro` |
|------|-------------------|-------------------|
| **适合** | 快速、低成本的生成和编辑 | 最高画质、4K、文字渲染 |
| **支持接口** | 图像生成接口 | 图像生成接口 |
| **返回格式** | URL 或 Base64 | URL 或 Base64 |
| **图片比例设置** | ✅ 支持 | ✅ 支持 |
| **图片尺寸设置** | ✅ 支持（1K/2K/4K） | ✅ 支持（1K/2K/4K） |
| **原生联网** | ✅ 支持 | ✅ 支持 |
| **图生图** | ✅ 支持（最多 14 张） | ✅ 支持 |
| **失败扣费** | ✅ 不扣费 | ✅ 不扣费 |
| **兼容格式** | DALL·E 格式 | DALL·E 格式 |

如需调用 Google 原生 Gemini 生图接口，见 [Gemini 原生格式（generateContent）](zh/coding/gemini.md)。

---

### **请求参数**

#### **Header**
| 参数名          | 类型   | 必需 | 默认值               | 说明                     |
|----------------|--------|------|----------------------|--------------------------|
| `Authorization`| string | ✅   | `Bearer {{YOUR_API_KEY}}` | API 认证令牌             |
| `Content-Type` | string | ✅   | `application/json`   | 请求体类型               |

#### **Body (`application/json`)**
| 参数名            | 类型             | 必需 | 默认值   | 说明                                                                 |
|-------------------|------------------|------|----------|----------------------------------------------------------------------|
| `model`           | string           | ✅   | -        | 模型名称：`nano-banana-2.1` 或 `nano-banana-pro` |
| `prompt`          | string           | ✅   | -        | 图像描述文本（支持中英文）                                            |
| `aspect_ratio`    | enum[string]     | ✅   | `auto`   | 图片比例：`auto`、`1:1`、`4:3`、`3:4`、`16:9`、`9:16`、`2:3`、`3:2`、`4:5`、`5:4`、`21:9` |
| `response_format` | string           | ❌   | `url`    | 返回格式：`url` 或 `b64_json`                                        |
| `image`           | array[string]    | ❌   | -        | **图生图**参考图片列表（URL 或 Base64），可传入一张或多张              |
| `image_size`      | enum[string]     | ❌   | `1K`     | 输出尺寸：`1K`、`2K`、`4K` |
| `search`          | boolean          | ❌   | `false`  | 是否开启原生联网 |

---

## 📌 使用场景示例

---

## 一、文生图（Text-to-Image）

> 仅通过文字 prompt 生成图片，不传入 `image` 参数。

### 1.1 基础文生图

#### JSON Body
```json
{
    "model": "nano-banana-2.1",
    "prompt": "一只橘猫躺在窗台上晒太阳，窗外是樱花盛开的春天",
    "aspect_ratio": "16:9"
}
```

#### cURL
```bash
curl -X POST 'https://beeroute.ai/v1/images/generations' \
  -H 'Authorization: Bearer {{YOUR_API_KEY}}' \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "nano-banana-2.1",
    "prompt": "一只橘猫躺在窗台上晒太阳，窗外是樱花盛开的春天",
    "aspect_ratio": "16:9"
}'
```

---

### 1.2 高清文生图（4K 画质）

#### JSON Body
```json
{
    "model": "nano-banana-pro",
    "prompt": "赛博朋克风格的未来城市夜景，霓虹灯倒映在雨水浸湿的街道上，4K超高清",
    "aspect_ratio": "21:9",
    "image_size": "4K"
}
```

#### cURL
```bash
curl -X POST 'https://beeroute.ai/v1/images/generations' \
  -H 'Authorization: Bearer {{YOUR_API_KEY}}' \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "nano-banana-pro",
    "prompt": "赛博朋克风格的未来城市夜景，霓虹灯倒映在雨水浸湿的街道上，4K超高清",
    "aspect_ratio": "21:9",
    "image_size": "4K"
}'
```

---

### 1.3 联网文生图（实时信息）

> 开启 `search` 后，模型可获取实时网络信息辅助生成。

#### JSON Body
```json
{
    "model": "nano-banana-2.1",
    "prompt": "生成2025年最新款保时捷911 Turbo S的产品宣传图，放在赛道场景中",
    "aspect_ratio": "16:9",
    "image_size": "2K",
    "search": true
}
```

#### cURL
```bash
curl -X POST 'https://beeroute.ai/v1/images/generations' \
  -H 'Authorization: Bearer {{YOUR_API_KEY}}' \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "nano-banana-2.1",
    "prompt": "生成2025年最新款保时捷911 Turbo S的产品宣传图，放在赛道场景中",
    "aspect_ratio": "16:9",
    "image_size": "2K",
    "search": true
}'
```

---

### 1.4 返回 Base64 格式

#### JSON Body
```json
{
    "model": "nano-banana-2.1",
    "prompt": "水彩风格的富士山日出，前景是湖面倒影",
    "aspect_ratio": "3:2",
    "response_format": "b64_json"
}
```
---

## 二、图生图（Image-to-Image）

> 传入 `image` 参数（参考图片的 URL 或 Base64），结合 prompt 对图片进行风格转换、编辑、扩展等操作。

### 2.1 图生图 — 传入图片 URL

#### JSON Body
```json
{
    "model": "nano-banana-2.1",
    "prompt": "将这张照片转换为吉卜力动画风格，保持原有构图不变，色彩更加温暖明亮",
    "aspect_ratio": "auto",
    "image": [
        "https://example.com/my-photo.jpg"
    ]
}
```

#### cURL
```bash
curl -X POST 'https://beeroute.ai/v1/images/generations' \
  -H 'Authorization: Bearer {{YOUR_API_KEY}}' \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "nano-banana-2.1",
    "prompt": "将这张照片转换为吉卜力动画风格，保持原有构图不变，色彩更加温暖明亮",
    "aspect_ratio": "auto",
    "image": [
        "https://example.com/my-photo.jpg"
    ]
}'
```
---

### 2.2 图生图 — 传入 Base64 图片

#### JSON Body 示例（Base64 已截断展示）
```json
{
    "model": "nano-banana-2.1",
    "prompt": "把这张人物照片变成赛博朋克风格的插画，添加霓虹灯光效果",
    "aspect_ratio": "1:1",
    "image": [
        "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAA..."
    ]
}
```

---

### 2.3 图生图 — 多图参考融合

> 传入多张参考图片，模型会综合参考所有图片进行创作。

#### JSON Body
```json
{
    "model": "nano-banana-pro",
    "prompt": "将第一张图的人物融合到第二张图的场景中，保持自然和谐，光影统一",
    "aspect_ratio": "4:3",
    "image_size": "2K",
    "image": [
        "https://example.com/person.jpg",
        "https://example.com/background.jpg"
    ]
}
```

#### cURL
```bash
curl -X POST 'https://beeroute.ai/v1/images/generations' \
  -H 'Authorization: Bearer {{YOUR_API_KEY}}' \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "nano-banana-pro",
    "prompt": "将第一张图的人物融合到第二张图的场景中，保持自然和谐，光影统一",
    "aspect_ratio": "4:3",
    "image_size": "2K",
    "image": [
        "https://example.com/person.jpg",
        "https://example.com/background.jpg"
    ]
}'
```

---

### 2.4 图生图 — 高清图片编辑

#### JSON Body
```json
{
    "model": "nano-banana-2.1",
    "prompt": "在这张风景照片中添加一道绚丽的彩虹，并将天空调整为黄昏暖色调",
    "aspect_ratio": "16:9",
    "image_size": "4K",
    "image": [
        "https://example.com/landscape.jpg"
    ]
}
```
---

## 三、响应格式

### 成功响应（HTTP 200）

#### 返回 URL 格式（默认）
```json
{
    "data": [
        {
            "url": "https://example.com/generated_image.png"
        }
    ]
}
```

#### 返回 Base64 格式
```json
{
    "data": [
        {
            "b64_json": "/9j/4AAQSkZJRgABAQAAAQABAAD..."
        }
    ]
}
```

---

## 四、常见场景速查表

| 场景 | 模型推荐 | 关键参数 | prompt 示例 |
|------|----------|----------|-------------|
| **基础文生图** | `nano-banana-2.1` | `aspect_ratio` | `"一只柯基在草地上奔跑"` |
| **4K 海报** | `nano-banana-pro` | `image_size: "4K"` | `"电影级科幻海报，太空站场景"` |
| **风格转换** | `nano-banana-2.1` | `image: [url]` | `"转换为油画风格"` |
| **图片编辑** | `nano-banana-pro` | `image: [url]`, `image_size` | `"移除背景，替换为海滩"` |
| **多图融合** | `nano-banana-pro` | `image: [url1, url2]` | `"将两张图合成一张"` |
| **联网生图** | `nano-banana-2.1` | `search: true` | `"2025年最新iPhone设计概念图"` |
| **手机壁纸** | `nano-banana-2.1` | `aspect_ratio: "9:16"` | `"极光星空壁纸，梦幻紫色调"` |
| **社交封面** | `nano-banana-2.1` | `aspect_ratio: "16:9"`, `image_size: "2K"` | `"简约科技风个人主页封面"` |

---

## 五、关键说明

1. **图片链接有效期**：返回的 URL 为临时链接，**2小时后过期**，请及时下载保存。
2. **失败不扣费**：生成失败时不会扣除额度。
3. **图生图 `image` 参数**：支持传入 **图片 URL** 或 **Base64 字符串**，可混合使用，支持一张或多张。
4. `image_size`：可选 `1K`、`2K`、`4K`（`K` 必须大写）。
5. `search`：设为 `true` 时，模型会结合实时联网信息生成图片。
6. **Prompt 建议**：描述越详细，生成效果越好；支持中英文混合 prompt。