# Gemini Image Generation

OpenAI compatible: `POST /v1/images/generations`. Official native path: `POST /v1/models/{model}:generateContent` (compatible with `/v1beta/models/{model}:generateContent`). [Image Generation API](https://ai.google.dev/gemini-api/docs/image-generation?hl=zh-cn)

---

## 1. Submit Image Generation Task Examples (POST)

* **HTTP Method**: `POST`
* **Request Path**: `https://www.tokenpad.ai/v1/images/generations`
* **Auth Header**: `Authorization: Bearer sk-your_token`

### A. Classic Text-to-Image (with Aspect Ratio & Resolution Control)

```bash
curl -X POST https://www.tokenpad.ai/v1/images/generations \
  -H "Authorization: Bearer sk-your_token_here" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemini-3.1-flash-image-preview",
    "prompt": "Sunlit Japanese garden with falling cherry blossoms, crystal-clear pond, ultra-detailed lighting and shadows, Makoto Shinkai anime style",
    "ratio": "16:9",
    "resolution": "1k",
    "n": 1,
    "response_format": "url"
  }'
```

### B. Image-to-Image / Reference Image Generation

The Gemini image engine supports passing a direct web image URL or `data:image/png;base64,...` data to perform image-to-image or multi-image reference generation.

```bash
curl -X POST https://www.tokenpad.ai/v1/images/generations \
  -H "Authorization: Bearer sk-your_token_here" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemini-3.1-flash-image-preview",
    "prompt": "Make this cat look more sci-fi in the scene, add glowing mechanical eyes, realistic cyberpunk style",
    "image": "https://example.com/assets/my_cat.png",
    "ratio": "1:1",
    "resolution": "1k",
    "response_format": "url"
  }'
```

### C. Multi-Image Reference Generation

When passing multiple reference images, use the `image_urls` array (or `image` array). Ideal for combining character + scene, style + subject, and other multi-asset compositions.

```bash
curl -X POST https://www.tokenpad.ai/v1/images/generations \
  -H "Authorization: Bearer sk-your_token_here" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemini-3.1-flash-image-preview",
    "prompt": "Use the character appearance from image 1 and the cafe scene from image 2, unified illustration style, warm natural lighting",
    "image_urls": [
      "https://example.com/character.png",
      "https://example.com/cafe_scene.png"
    ],
    "ratio": "16:9",
    "resolution": "1k",
    "n": 1,
    "response_format": "url"
  }'
```

Also supports mixed Base64 inputs (combinable with URLs in an array):

```bash
curl -X POST https://www.tokenpad.ai/v1/images/generations \
  -H "Authorization: Bearer sk-your_token_here" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemini-3.1-flash-image-preview",
    "prompt": "Place the person from image 1 into the background of image 2, keeping lighting and shadows consistent",
    "image": [
      "https://example.com/person.png",
      "data:image/png;base64,iVBORw0KGgoAAAANSUhEUg..."
    ],
    "ratio": "1:1",
    "resolution": "1k"
  }'
```

### D. Search-Grounded Image Generation (Search Grounding)

Enable web search to assist image generation with up-to-date visual information.

```bash
curl -X POST https://www.tokenpad.ai/v1/images/generations \
  -H "Authorization: Bearer sk-your_token_here" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemini-3.1-flash-image-preview",
    "prompt": "Create a concept design rendering of a flagship 2026 smartphone featuring a transparent display",
    "google_search": true,
    "google_image_search": true,
    "ratio": "16:9",
    "resolution": "1k"
  }'
```

### E. Official Native Gemini Format Image Generation (POST /v1/models/{model}:generateContent)

If your client application (such as Google GenAI SDK, ComfyUI, workflow plugins, etc.) uses Google's official native request structure, simply call the official endpoint with your platform API key:

```bash
curl -X POST https://www.tokenpad.ai/v1/models/gemini-3-pro-image:generateContent \
  -H "Authorization: Bearer sk-your_token_here" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [
      {
        "role": "user",
        "parts": [
          { "text": "Sunlit Japanese garden with falling cherry blossoms, ultra-detailed lighting and shadows, Makoto Shinkai anime style" }
        ]
      }
    ],
    "generationConfig": {
      "imageConfig": {
        "aspectRatio": "16:9",
        "imageSize": "1K"
      },
      "responseModalities": [
        "IMAGE"
      ]
    }
  }'
```

---

## 2. Complete Parameter Dictionary

| Parameter | Type | Required | Default | Description & Constraints |
| :--- | :--- | :--- | :--- | :--- |
| `model` | `string` | **Yes** | - | e.g., `gemini-3.1-flash-image-preview`. |
| `prompt` | `string` | **Yes** | - | Image prompt description. |
| `image` | `string / array` | No | - | Reference image URL or Base64 (single or array). |
| `image_urls` | `array` | No | - | Array of reference image URLs; alternative to `image`, recommended for multiple images. |
| `ratio` | `string` | No | `"1:1"` | Aspect ratio: `"1:1"` / `"3:4"` / `"4:3"` / `"9:16"` / `"16:9"`. Takes precedence over `size`. |
| `resolution` | `string` | No | `"1k"` | e.g., `"1k"`, `"2k"`. Takes precedence over `size`. |
| `size` | `string` | No | - | Compatibility parameter: with a colon (e.g., `"16:9"`) specifies aspect ratio; without a colon (e.g., `"1k"`) specifies resolution. **Do not pass pixel values like `"1024x1024"`**. |
| `response_format` | `string` | No | `"url"` | `"url"` or `"b64_json"`. |
| `n` | `integer` | No | `1` | Number of images to generate. |
| `google_search` | `boolean` | No | `false` | Whether to enable Google Search. |
| `google_image_search` | `boolean` | No | `false` | Whether to enable Google Image Search grounding. |

---

## 3. Response Example (200 OK)

```json
{
  "created": 1719441600,
  "data": [
    {
      "url": "https://example.com/output/img_gemini_garden.png"
    }
  ]
}
```



### 5. Common Error Codes & Troubleshooting

HTTP codes, error body format, and troubleshooting are **maintained in one dedicated page** (not duplicated under each example).

👉 **[Gateway Error Codes & Troubleshooting](/docs/errors/error-codes)**

**Quick reference**:

| Code | Meaning |
| :---: | :--- |
| **400** | Invalid parameters / content moderation or copyright block |
| **402** | Insufficient account balance (**not** 429) |
| **429** | RPS/RPM limits or in-flight task cap (low balance) |
| **403** | Token / IP / model permission (balance → 402; content filter → 400) |
| **200 + `status: failed`** | Async video/long-task business failure on poll |

HTTP status matches call log `status_code` for traceability by `log_id`.

> **Maintenance**: update the `error-codes` article only; this appendix links to it.
