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

# Nano Banana 图片生成 API

> 使用 nano-banana-2 模型的原生接口生成高质量图片

## 支持的模型

<Card title="nano-banana-2" icon="image">
  Nano Banana 第二代图片生成模型，使用原生 API 接口
</Card>

## 接口特点

<Warning>
  **重要说明：**

  * 使用原生接口格式，与标准 OpenAI 格式不同
  * **不支持 URL 上传图片**，仅支持 Base64 编码
  * 图片统一以 Base64 格式返回
  * API Key 需要在 URL 查询参数中传递
</Warning>

## 文生图

通过文本描述生成图片。

<Note>
  **配置说明：**

  * 将 `api.xxx.com` 替换为您的实际 API 域名
  * 将 `sk-xxx` 替换为您的 API 密钥（在 URL 的 `key` 参数中传递）
</Note>

<CodeGroup>
  ```bash cURL theme={null}
  curl -X POST "https://api.xxx.com/v1beta/models/nano-banana-2:generateContent?key=sk-xxx" \
    -H "Content-Type: application/json" \
    -d '{
      "contents": [
        {
          "parts": [
            {
              "text": "生成狗狗趴在草地上的近景画面"
            }
          ]
        }
      ],
      "generationConfig": {
        "imageConfig": {
          "aspectRatio": "16:9",
          "imageSize": "2K"
        }
      }
    }'
  ```

  ```json Response theme={null}
  {
    "candidates": [
      {
        "content": {
          "parts": [
            {
              "inline_data": {
                "mime_type": "image/jpeg",
                "data": "/9j/4AAQSkZJRg..."
              }
            }
          ]
        }
      }
    ]
  }
  ```
</CodeGroup>

## 参数说明

### 请求参数

| 参数名                              | 类型     | 必填  | 说明     | 示例值              |
| -------------------------------- | ------ | --- | ------ | ---------------- |
| `contents`                       | array  | ✅   | 内容数组   | 包含 parts 数组      |
| `contents[].parts`               | array  | ✅   | 部分内容数组 | 包含文本或图片数据        |
| `contents[].parts[].text`        | string | ✅\* | 文本提示词  | "生成狗狗趴在草地上的近景画面" |
| `contents[].parts[].inline_data` | object | ✅\* | 内联图片数据 | 用于图生图/多图融合       |
| `generationConfig`               | object | ❌   | 生成配置   | 包含图片配置           |
| `generationConfig.imageConfig`   | object | ❌   | 图片配置   | 包含分辨率和比例         |

\*注：`text` 和 `inline_data` 至少需要一个

### 图片配置参数

| 参数名           | 类型     | 说明    | 可选值                                   |
| ------------- | ------ | ----- | ------------------------------------- |
| `aspectRatio` | string | 图片宽高比 | `1:1`, `16:9`, `9:16`, `4:3`, `3:4` 等 |
| `imageSize`   | string | 图片尺寸  | `1K`, `2K`, `4K`                      |

## 多图融合

融合多张图片的特征生成新图片，仅支持 Base64 格式。

<CodeGroup>
  ```bash cURL theme={null}
  curl -X POST "https://api.xxx.com/v1beta/models/nano-banana-2:generateContent?key=sk-xxx" \
    -H "Content-Type: application/json" \
    -d '{
      "contents": [
        {
          "parts": [
            {
              "text": "融合这两张图片的风格"
            },
            {
              "inline_data": {
                "mime_type": "image/jpeg",
                "data": "<base64_image_1>"
              }
            },
            {
              "inline_data": {
                "mime_type": "image/jpeg",
                "data": "<base64_image_2>"
              }
            }
          ]
        }
      ],
      "generationConfig": {
        "imageConfig": {
          "aspectRatio": "1:1"
        }
      }
    }'
  ```

  ```json Response theme={null}
  {
    "candidates": [
      {
        "content": {
          "parts": [
            {
              "inline_data": {
                "mime_type": "image/jpeg",
                "data": "/9j/4AAQSkZJRg..."
              }
            }
          ]
        }
      }
    ]
  }
  ```
</CodeGroup>

<Info>
  **多图输入：** 在 `parts` 数组中添加多个 `inline_data` 对象，每个包含一张图片的 Base64 数据
</Info>

## inline\_data 格式

图片数据需要以内联数据格式提供：

```json theme={null}
{
  "inline_data": {
    "mime_type": "image/jpeg",
    "data": "<base64_encoded_image_data>"
  }
}
```

### 支持的 MIME 类型

* `image/jpeg`
* `image/png`
* `image/webp`

## 响应格式

返回的图片数据位于响应的 `candidates[0].content.parts[0].inline_data.data` 字段中，为 Base64 编码字符串。

<CodeGroup>
  ```python Python 示例 theme={null}
  import base64
  import requests
  import json

  # 发送请求
  response = requests.post(
      "https://api.xxx.com/v1beta/models/nano-banana-2:generateContent?key=sk-xxx",
      json={
          "contents": [{
              "parts": [{"text": "生成狗狗趴在草地上的近景画面"}]
          }],
          "generationConfig": {
              "imageConfig": {
                  "aspectRatio": "16:9",
                  "imageSize": "2K"
              }
          }
      }
  )

  # 解析响应
  data = response.json()
  image_base64 = data["candidates"][0]["content"]["parts"][0]["inline_data"]["data"]

  # 保存图片
  with open("output.jpg", "wb") as f:
      f.write(base64.b64decode(image_base64))
  ```
</CodeGroup>

## 与标准接口的区别

| 特性   | Nano Banana 原生接口                               | 标准 OpenAI 格式接口                        |
| ---- | ---------------------------------------------- | ------------------------------------- |
| 接口路径 | `/v1beta/models/nano-banana-2:generateContent` | `/v1/images/generations`              |
| 认证方式 | URL 参数 `?key=sk-xxx`                           | Header `Authorization: Bearer sk-xxx` |
| 请求格式 | `contents` + `parts` 数组                        | `model` + `prompt`                    |
| 图片上传 | 仅 Base64 (`inline_data`)                       | URL 或 Base64 (`image`)                |
| 返回格式 | 仅 Base64 (`inline_data`)                       | URL 或 Base64 (`response_format`)      |
| 尺寸参数 | `aspectRatio` + `imageSize`                    | `size`                                |

<Note>
  Nano Banana 使用的是类似 Google Gemini 的原生 API 格式，与其他模型的 OpenAI 兼容格式不同。
</Note>
