# Gemini Protocol

Google Gemini native generateContent protocol. Currently used by the Nano image models (images are returned synchronously); Gemini text chat models are not offered yet.

## Image Generation (Nano · sync)

Synchronous image generation on the native Gemini generateContent endpoint is available: pass an image model with generationConfig.responseModalities including "IMAGE", and a single request returns the base64 image directly — no taskId, no polling. The public "Nano tier" is backed by these Gemini image models. Two request formats are supported: default JSON (native Gemini protocol) and multipart/form-data upload (upload reference images directly via the image file field for image-to-image — see the example below).

### Endpoints
| Usage | Method | Path |
| --- | --- | --- |
| Sync image generation | POST | /v1beta/models/{model}:generateContent |

### Supported models
gemini-2.5-flash-image (Nano, 1K) / gemini-3-pro-image-preview (Nano Pro, 1K/2K/4K) / gemini-3.1-flash-image-preview (Nano 2, 1K/2K/4K).

### Request body
| Param | Type | Required | Description |
| --- | --- | --- | --- |
| {model} | path | Yes | Model id in the URL path (tier mapping): gemini-2.5-flash-image (Nano, 1K) / gemini-3-pro-image-preview (Nano Pro, 1K/2K/4K) / gemini-3.1-flash-image-preview (Nano 2, 1K/2K/4K) |
| contents | array | Yes | Native Gemini contents array; each item has role and parts[]. Text-to-image usually needs a single role="user" item. |
| contents[].role | string | — | Role; use "user" for image generation |
| contents[].parts[].text | string | Yes | Text prompt describing the image to generate |
| contents[].parts[].inlineData | object | — | Reference image for image-to-image (inline base64): { "mimeType": "image/png", "data": "<base64>" }; omit for text-to-image. You can also pass a URL via fileData.fileUri below. |
| contents[].parts[].fileData.fileUri | string | — | https URL of a reference image for image-to-image (alternative to inline base64); the server downloads and inlines it. https only, up to 4 reference images; if any download fails the whole request errors and is not charged. |
| generationConfig | object | Yes | Generation config object; see the sub-fields below |
| generationConfig.responseModalities | string[] | Yes | Output modalities; must include "IMAGE" for image output (e.g. ["TEXT","IMAGE"]); omitting it returns text only |
| generationConfig.candidateCount | number | — | Number of images: 1-4, default 1. Charged per image (xN) and settled by the number of successful images (failures refunded) |
| generationConfig.imageConfig.imageSize | string | — | Resolution: 1K / 2K / 4K, default 1K (Nano is 1K only; Nano Pro / Nano 2 support 2K/4K) |
| generationConfig.imageConfig.aspectRatio | string | — | Output ratio: 1:1 / 16:9 / 9:16 / 4:3 / 3:4 / 3:2 / 2:3 / 5:4 / 4:5 / 21:9, default 1:1; follow the official Google imageConfig spec |

> Besides Authorization: Bearer <API_KEY>, x-goog-api-key is also accepted (your APIKey; either one, not both). Generating multiple images is charged per image (candidateCount) and settled by the number that succeed; a reference image can be inline base64 or an https URL via fileData.fileUri. multipart/form-data submission: fields prompt (required for text-to-image / edit instruction for image-to-image), image (reference file, repeatable up to 4), n (count, max 4, maps to candidateCount); model comes from the URL path only, image models + non-stream only, request body capped at 20MB (over → 413); request format (JSON / multipart) and response format (base64 / add ?respFormat=url for a CDN link) are orthogonal, and billing is identical to the JSON path.

### Text-to-image (sync)
```bash
curl -X POST "https://api.novapi.ai/v1beta/models/gemini-2.5-flash-image:generateContent" \
  -H "Authorization: Bearer <API_KEY>" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [{"role":"user","parts":[{"text":"a cute cat on a windowsill, soft morning light"}]}],
    "generationConfig": {
      "responseModalities": ["TEXT","IMAGE"],
      "imageConfig": {"imageSize": "1K", "aspectRatio": "1:1"}
    }
  }'
```

### Image-to-image (sync, URL reference + multiple)
```bash
curl -X POST "https://api.novapi.ai/v1beta/models/gemini-3-pro-image-preview:generateContent" \
  -H "Authorization: Bearer <API_KEY>" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [{"role":"user","parts":[
      {"text":"turn this photo into an oil painting"},
      {"fileData":{"mimeType":"image/png","fileUri":"https://example.com/input.png"}}
    ]}],
    "generationConfig": {
      "responseModalities": ["TEXT","IMAGE"],
      "candidateCount": 2,
      "imageConfig": {"imageSize": "2K", "aspectRatio": "16:9"}
    }
  }'
```

### Image-to-image (sync, multipart file upload)
```bash
# 同一端点按 Content-Type 分流：发 multipart/form-data 直接上传参考图文件
# 字段：prompt（文生图必填 / 图生图为编辑指令）、image（参考图文件，可重复最多 4 张）、n（张数，最多 4）
# model 只取自 URL 路径；仅图片模型 + 仅非流式支持；请求体上限 20MB
# 默认返回 base64；叠加 ?respFormat=url 则返回 CDN 直链
curl -X POST "https://api.novapi.ai/v1beta/models/gemini-2.5-flash-image:generateContent" \
  -H "Authorization: Bearer <API_KEY>" \
  -F "prompt=把这张图改成水彩风格" \
  -F "image=@input.png"
```

### Response example
```json
{
  "candidates": [
    {
      "content": {
        "parts": [
          { "inlineData": { "mimeType": "image/png", "data": "<base64 PNG，原样透传>" } }
        ]
      },
      "finishReason": "STOP"
    }
  ]
}
```
