> ## Documentation Index
> Fetch the complete documentation index at: https://docs.hairoute.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Gemini native image generation

> Generate or edit images with the native Gemini generateContent endpoint, for Google/Gemini image models only

Generate images with the native Gemini `generateContent` request and response format. **For image models, this endpoint is supported only for Google/Gemini image models.** Use the [OpenAI image generation endpoint](/docs/en/api-reference/images/openai/generation) or a model-specific endpoint for Seedream, GPT Image, and other image models. Native calls for Gemini chat models are outside the scope of this image reference.

## Features

* Generate images using native Gemini `generateContent` request and response formats, not OpenAI Images formats
* Generate from text, edit one image, combine multiple references, or continue editing across turns
* Set model-supported aspect ratios and output tiers with `generationConfig.imageConfig`
* Receive text and Base64 images in `candidates[].content.parts[]`; use the [streaming endpoint](/docs/en/api-reference/images/gemini/stream-generation) for SSE

## Authentication

`POST https://api.hairoute.ai/v1/models/{model}:generateContent`

Replace `{model}` with an available Google/Gemini image model from the [model list](https://portal.hairoute.ai/en/models). **Do not put `model` in the request body.** Pass your HaiRoute API key in `x-goog-api-key: YOUR_API_KEY`, or use `Authorization: Bearer YOUR_API_KEY`. Do not substitute a Google API key for your HaiRoute key.

## Supported image models

This endpoint is only for Google/Gemini image models.

| Model identifier              | Model type                   | Description                                                                            |
| ----------------------------- | ---------------------------- | -------------------------------------------------------------------------------------- |
| `gemini-3.1-flash-lite-image` | Image generation and editing | Gemini 3.1 Flash Lite Image; configured output tier: `1K`.                             |
| `gemini-3.1-flash-image`      | Image generation and editing | Gemini 3.1 Flash Image; configured output tiers: `512` (about 0.5K), `1K`, `2K`, `4K`. |
| `gemini-3-pro-image`          | Image generation and editing | Gemini 3 Pro Image; configured output tiers: `1K`, `2K`, `4K`.                         |

## Quick example

```bash theme={null}
curl -X POST 'https://api.hairoute.ai/v1/models/YOUR_GEMINI_IMAGE_MODEL:generateContent' \
  -H 'x-goog-api-key: YOUR_API_KEY' \
  -H 'Content-Type: application/json' \
  -d '{
    "contents": [{"role": "user", "parts": [{"text": "An orange cat wearing an astronaut helmet floating in space"}]}],
    "generationConfig": {
      "responseModalities": ["TEXT", "IMAGE"],
      "imageConfig": {"aspectRatio": "1:1", "imageSize": "1K"}
    }
  }'
```

On success, the image is returned in `candidates[].content.parts[].inlineData`, with a `mimeType` and Base64-encoded `data`. Any accompanying text appears as a `text` part in the same `parts` array. The Base64 value below is a placeholder, not an actual image:

```json theme={null}
{
  "candidates": [{
    "content": {"role": "model", "parts": [
      {"text": "Here is your image:"},
      {"inlineData": {"mimeType": "image/png", "data": "<BASE64_IMAGE_DATA>"}}
    ]},
    "finishReason": "STOP"
  }],
  "usageMetadata": {"promptTokenCount": 12, "candidatesTokenCount": 1290, "totalTokenCount": 1302}
}
```

Decode `inlineData.data` to bytes and save them using the supplied `mimeType`. The response is **not** an OpenAI Images `data[].url` response. Usage, candidates, and optional fields vary by model and request.

## Key parameters

| Field                                      | Description                                                                                                                                                             |
| ------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `contents`                                 | Required. An array of messages. For text-to-image, use `role: "user"` and `parts: [{"text": "..."}]`.                                                                   |
| `contents[].parts[].inlineData`            | Optional image input for editing or multiple-reference requests. Provide the image MIME type in `mimeType` and raw Base64 data **without a data URI prefix** in `data`. |
| `generationConfig.responseModalities`      | Use `["TEXT", "IMAGE"]` for image output; text may accompany the image.                                                                                                 |
| `generationConfig.imageConfig.aspectRatio` | Optional aspect ratio, such as `1:1` or `16:9`; available values depend on the selected model.                                                                          |
| `generationConfig.imageConfig.imageSize`   | Optional size tier, such as `1K`, `2K`, or `4K`; availability depends on the selected model.                                                                            |
| `systemInstruction`                        | Optional native Gemini instruction with the same structure as one `contents` message.                                                                                   |

This endpoint uses native `generationConfig.imageConfig`, **not** OpenAI Images parameters such as `size`, `quality`, or `response_format`. Supported aspect ratios and sizes depend on model configuration.

## Generation modes

These examples show request bodies only. All four modes use this page's endpoint and authentication; no separate `mode` parameter is needed. Replace every Base64 placeholder with actual raw Base64 image data.

### Text-to-image

Send a text prompt without an input image; the **Quick example** above is ready to adapt. Local changes and style adjustments are prompt instructions, not separate API parameters.

### Single-image editing

Put one image and an editing instruction in the same `user` message. This example replaces the background; you can also ask to preserve the subject, change an element, or adjust the style.

```json theme={null}
{
  "contents": [{"role": "user", "parts": [
    {"inlineData": {"mimeType": "image/png", "data": "<BASE64_INPUT_IMAGE>"}},
    {"text": "Replace the background with a sunset beach and keep the subject"}
  ]}],
  "generationConfig": {"responseModalities": ["TEXT", "IMAGE"]}
}
```

### Multiple-reference composition

Put multiple reference images in one `user` message's `parts` and describe the role of each image in the text. Input count, size limits, and results depend on the selected model and channel.

```json theme={null}
{
  "contents": [{"role": "user", "parts": [
    {"inlineData": {"mimeType": "image/png", "data": "<BASE64_SUBJECT_IMAGE>"}},
    {"inlineData": {"mimeType": "image/jpeg", "data": "<BASE64_BACKGROUND_IMAGE>"}},
    {"text": "Use the person in the first image as the subject and the scene in the second as the background; make a natural composite"}
  ]}],
  "generationConfig": {"responseModalities": ["TEXT", "IMAGE"]}
}
```

### Multi-turn editing

Send the previous `user` request, the actual previous `candidates[0].content` as a `model` message, and your new instruction in that order within `contents`. This body illustrates the structure only:

```json theme={null}
{
  "contents": [
    {"role": "user", "parts": [{"text": "Draw an orange cat wearing a scarf"}]},
    {"role": "model", "parts": [
      {"inlineData": {"mimeType": "image/png", "data": "<BASE64_FROM_PREVIOUS_RESPONSE>"}}
    ]},
    {"role": "user", "parts": [{"text": "Keep the cat and scarf, but replace the background with a snowy landscape"}]}
  ],
  "generationConfig": {"responseModalities": ["TEXT", "IMAGE"]}
}
```

For a real request, do not reconstruct or trim the prior `model` content: replay the complete previous `candidates[0].content`, including any image, text, and `thoughtSignature` parts if returned. Multiple references and multi-turn results depend on the selected image model.

## Troubleshooting

Check the HTTP status and the `error` in the response body first. Never paste API keys or complete image Base64 in logs or support requests.

| Symptom                                         | What to check                                                                                                                                                                                                                                                                           |
| ----------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Authentication fails (for example, 401/403)     | Use a **HaiRoute API key** in either `x-goog-api-key` or `Authorization: Bearer`; do not use a Google API key here. Check that the key is valid and allowed to access the model.                                                                                                        |
| Model not found or unavailable                  | Check that `{model}` in the URL is an available Google/Gemini **image model** in your account’s [model list](https://portal.hairoute.ai/en/models). Do not put the model name in the request body or use a different image model on this endpoint.                                      |
| Invalid request or input image                  | Check `contents[].parts`, `generationConfig.responseModalities` (`["TEXT", "IMAGE"]` for image output), and the selected model’s supported `imageConfig`. `inlineData.data` must be raw Base64 without a `data:image/...;base64,` prefix; `mimeType` must match the input image format. |
| Request succeeds but no image appears           | Inspect all `candidates[].content.parts[].inlineData`; the image is not in `data[].url`. If absent, inspect `finishReason`, `promptFeedback`, and returned text before adjusting the prompt or request. HTTP 200 alone does not prove an image was generated.                           |
| Multi-turn edit fails or ignores earlier output | Replay the prior `user` message, the complete previous `candidates[0].content` (including `thoughtSignature` if returned), and the new `user` instruction in order; sending only the image Base64 is insufficient.                                                                      |

## Next steps

* See [OpenAI image generation](/docs/en/api-reference/images/openai/generation) for other image models


## OpenAPI

````yaml en/api-reference/images/gemini/generation/openapi.json POST /v1/models/{model}:generateContent
openapi: 3.0.1
info:
  title: Gemini native image API
  version: 1.0.0
servers:
  - url: https://api.hairoute.ai
security: []
paths:
  /v1/models/{model}:generateContent:
    post:
      summary: Generate an image (native Gemini, non-streaming)
      description: >-
        For image models, only Google/Gemini image models are supported; use
        other image endpoints for other models. HaiRoute API key. Use this
        header or Authorization: Bearer YOUR_API_KEY (not both).
      parameters:
        - name: model
          in: path
          required: true
          description: An available Google/Gemini image model in HaiRoute
          schema:
            type: string
          example: YOUR_GEMINI_IMAGE_MODEL
        - name: x-goog-api-key
          in: header
          required: true
          description: >-
            HaiRoute API key for the native request. Alternatively, use
            Authorization: Bearer YOUR_API_KEY instead (choose one).
          schema:
            type: string
          example: YOUR_API_KEY
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/GenerateRequest'
            example:
              contents:
                - role: user
                  parts:
                    - text: Draw an orange cat
              generationConfig:
                responseModalities:
                  - TEXT
                  - IMAGE
                imageConfig:
                  aspectRatio: '1:1'
                  imageSize: 1K
      responses:
        '200':
          description: >-
            Native Gemini GenerateContentResponse with images in
            candidates[].content.parts[].inlineData.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/GenerateResponse'
              example:
                candidates:
                  - content:
                      role: model
                      parts:
                        - inlineData:
                            mimeType: image/png
                            data: <BASE64_IMAGE_DATA>
                    finishReason: STOP
                usageMetadata:
                  totalTokenCount: 1302
        '400':
          description: Gemini-style error response.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/GeminiError'
components:
  schemas:
    GenerateRequest:
      type: object
      properties:
        contents:
          type: array
          minItems: 1
          items:
            $ref: '#/components/schemas/Content'
          description: >-
            Required native Gemini messages. Use a user text part for
            text-to-image; add inlineData for image-to-image.
        systemInstruction:
          $ref: '#/components/schemas/Content'
        generationConfig:
          $ref: '#/components/schemas/GenerationConfig'
      required:
        - contents
      additionalProperties: true
    GenerateResponse:
      type: object
      description: >-
        Native Gemini GenerateContentResponse with images in
        candidates[].content.parts[].inlineData.
      properties:
        candidates:
          type: array
          items:
            type: object
            properties:
              index:
                type: integer
              content:
                $ref: '#/components/schemas/Content'
              finishReason:
                type: string
            additionalProperties: true
        usageMetadata:
          type: object
          properties:
            promptTokenCount:
              type: integer
            candidatesTokenCount:
              type: integer
            totalTokenCount:
              type: integer
          additionalProperties: true
        modelVersion:
          type: string
        responseId:
          type: string
      additionalProperties: true
    GeminiError:
      type: object
      properties:
        error:
          type: object
          properties:
            code:
              type: integer
            message:
              type: string
            status:
              type: string
      description: Gemini-style error response.
    Content:
      type: object
      properties:
        role:
          type: string
          example: user
        parts:
          type: array
          items:
            $ref: '#/components/schemas/Part'
      required:
        - parts
      additionalProperties: true
    GenerationConfig:
      type: object
      description: >-
        Native Gemini generation settings; availability depends on the image
        model.
      properties:
        responseModalities:
          type: array
          items:
            type: string
            enum:
              - TEXT
              - IMAGE
          example:
            - TEXT
            - IMAGE
          description: Use ["TEXT", "IMAGE"] for image output; text may also be returned.
        imageConfig:
          $ref: '#/components/schemas/ImageConfig'
        candidateCount:
          type: integer
          minimum: 1
      additionalProperties: true
    Part:
      type: object
      properties:
        text:
          type: string
        inlineData:
          $ref: '#/components/schemas/InlineData'
      additionalProperties: true
      description: Text or image content parts.
    ImageConfig:
      type: object
      description: >-
        Image aspect ratio and output size tier; supported values depend on the
        model.
      properties:
        aspectRatio:
          type: string
          example: '1:1'
        imageSize:
          type: string
          example: 1K
      additionalProperties: true
    InlineData:
      type: object
      description: Image MIME type and raw Base64 data without a data URI prefix.
      properties:
        mimeType:
          type: string
          example: image/png
        data:
          type: string
          format: byte
          example: <BASE64_IMAGE_DATA>
      required:
        - mimeType
        - data

````