> ## 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 文生图。在在线调试面板选择模型，设置提示词、分辨率和比例，并读取生成结果。

使用 Nano Banana Pro、2 或 2.1，根据文字描述生成图片。

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

文生图和图片编辑使用同一个接口；图片编辑的请求中需要增加参考图。

## 在线调试

在 **x-goog-api-key** 中填写 BetterToken API Key，选择 **model**，并修改 **contents** 中的提示词。先使用 `1K`、`1:1`，然后点击 **Send**。

<Note>
  点击 **Send** 会发起真实生成请求，可能产生费用。接口同步等待生成完成；超时或断连后，请先核对消费记录和请求结果，再决定是否重新发送。
</Note>

三款均提供 `1K`、`2K`、`4K`。Nano Banana 2 和 2.1 支持 14 种比例，Pro 支持 10 种。 `X-BetterToken-Max-Images` 是允许返回并计费的图片数量上限，不保证生成对应张数。 [比例列表](/zh/api-reference/nano-banana#aspect-ratios)

<Warning>
  BetterToken API 不支持搜索生图，包括 Google 网页搜索和图片搜索增强。不要添加搜索工具。
</Warning>

文生图只需提交文字描述，无需参考图。接口使用非流式 `generateContent`；不支持 Files、Batch、Interactions 或流式调用。

## 读取响应

图片位于 `candidates[].content.parts[].inlineData`。遍历所有 part，跳过 `thought: true`，解码 `data`，并根据 `mimeType` 选择文件后缀。不要假定图片总在第一个 part，也不要一律保存成 PNG。

## 相关文档

* [图片编辑](/zh/api-reference/gemini-images-edits)
* [SDK、图片保存与连续编辑](/zh/api-reference/nano-banana)
* [模型介绍](/zh/model-updates/nano-banana)
* <a href={"https://bettertoken.ai/pricing"}>模型广场</a>


## OpenAPI

````yaml api-reference/gemini-images-generations.zh.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: generateGeminiImage_zh
      parameters:
        - name: model
          in: path
          required: true
          description: BetterToken 模型 ID，选择三款 Nano Banana 模型之一。
          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: 原始图片字节的 base64，不包含 Data URL 前缀。
                                      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: 白色桌面上蓝色陶瓷杯的影棚产品照片。
              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.