Unode
User GuideAPI ReferenceHelp & SupportBusiness Cooperation

Google Gemini Image Generation

Introduction

Given a text prompt, the model will generate new images. Google Gemini provides powerful image generation models that can create images based on natural language descriptions.

Supported Models

Currently supported models include:

ModelDescription
gemini-2.5-flash-imageGoogle Gemini image generation model that supports generating high-quality images based on text prompts
gemini-3.1-flash-image-previewGoogle Gemini fast image generation model (preview)
gemini-3-pro-image-previewGoogle Gemini image generation and editing model that supports generating images based on text prompts, as well as editing images based on input images and text prompts
imagen-4.0-fast-generate-001Google Imagen 4.0 Fast image generation model

Request Examples

# Basic image generation
curl "https://www.unodetech.xyz/v1beta/models/gemini-2.5-flash-image:generateContent?key=$API_KEY" \
  -H 'Content-Type: application/json' \
  -X POST \
  -d '{
    "contents": [{
      "parts": [{"text": "Give me an image of a cat"}]
    }]
  }'

Response Example:

{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "text": "Here is an image for you: "
          },
          {
            "inlineData": {
              "mimeType": "image/png",
              "data": "..."
            }
          }
        ],
        "role": "model"
      },
      "finishReason": "STOP",
      "index": 0
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 9,
    "candidatesTokenCount": 1298,
    "totalTokenCount": 1307,
    "promptTokensDetails": [
      {
        "modality": "TEXT",
        "tokenCount": 9
      }
    ],
    "candidatesTokensDetails": [
      {
        "modality": "IMAGE",
        "tokenCount": 1290
      }
    ]
  },
  "modelVersion": "gemini-2.5-flash-image",
  "responseId": "..."
}
# Generate an image and return a temporary URL
curl "https://www.unodetech.xyz/v1beta/models/gemini-2.5-flash-image:generateContent?key=$API_KEY" \
  -H 'Content-Type: application/json' \
  -X POST \
  -d '{
    "contents": [{
      "parts": [{"text": "Give me an image of a cat"}]
    }],
    "generationConfig": {
      "responseFormat": {
        "image": {"delivery": "URI"}
      }
    }
  }'

Response Example:

{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "text": "Here is an image for you: "
          },
          {
            "fileData": {
              "mimeType": "image/png",
              "fileUri": "https://cdn.example.com/generated-images/...?..."
            }
          }
        ],
        "role": "model"
      },
      "finishReason": "STOP",
      "index": 0
    }
  ]
}
# Image editing
curl "https://www.unodetech.xyz/v1beta/models/gemini-3-pro-image-preview:generateContent?key=$API_KEY" \
  -H 'Content-Type: application/json' \
  -X POST \
  -d '{
    "contents": [{
      "parts": [
        {"text": "put the chips in the given image on a beach."},
        {
          "inline_data": {
            "mime_type": "image/jpeg",
            "data": "$IMG_BASE64"
          }
        }
      ]
    }],
    "generationConfig": {
      "responseModalities": ["TEXT", "IMAGE"],
      "imageConfig": {
        "aspectRatio": "1:1",
        "imageSize": "2K"
      }
    }
  }'

Response Example:

{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "text": "Here is the edited image for you: "
          },
          {
            "inlineData": {
              "mimeType": "image/png",
              "data": "..."
            }
          }
        ],
        "role": "model"
      },
      "finishReason": "STOP",
      "index": 0
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 15,
    "candidatesTokenCount": 1350,
    "totalTokenCount": 1365,
    "promptTokensDetails": [
      {
        "modality": "TEXT",
        "tokenCount": 15
      },
      {
        "modality": "IMAGE",
        "tokenCount": 1200
      }
    ],
    "candidatesTokensDetails": [
      {
        "modality": "IMAGE",
        "tokenCount": 1350
      }
    ]
  },
  "modelVersion": "gemini-3-pro-image-preview",
  "responseId": "..."
}

Request

Endpoint

Create Image

POST /v1beta/models/gemini-2.5-flash-image:generateContent

Create an image based on a text prompt.

Edit Image

POST /v1beta/models/gemini-3-pro-image-preview:generateContent

Edit or generate new images based on input images and text prompts. Supports gemini-2.5-flash-image and gemini-3-pro-image-preview models.

Authentication Method

Include the API key in the request URL parameters:

?key=$API_KEY

Where $API_KEY is your API key.

Request Body Parameters

Response

Success Response

Image Object Example

With "delivery": "URI":

{
  "fileData": {
    "mimeType": "image/png",
    "fileUri": "https://cdn.example.com/generated-images/...?..."
  }
}

Without URL delivery, the image may be returned as Base64:

{
  "inlineData": {
    "mimeType": "image/png",
    "data": "..."
  }
}

Image URL Delivery

URL delivery applies to non-streaming generateContent requests. Depending on the model, image data will either be returned in candidates.content.parts in an "inlineData" object or directly as Markdown in a "text" object.

Temporary URLs expire. Download or transfer the image soon after receiving it.

URL Delivery Errors

HTTP statusError codeRecommended action
400Invalid requestUse a non-streaming generateContent request and valid responseFormat JSON. Only uppercase "URI" activates URL delivery.
502image_url_delivery_failedStorage or URL signing failed in strict mode; retry later.
502image_response_too_largeReduce the image count or dimensions.
503image_url_delivery_unavailableURL delivery for this API key or traffic.
503Other image_delivery_* errorsRetry with backoff when capacity or runtime state recovers.

Best Practices

Prompt Writing Suggestions

  1. Use clear and specific descriptions
  2. Specify important visual details
  3. Describe the desired artistic style and atmosphere
  4. Pay attention to composition and perspective descriptions
  5. Can include details such as color, lighting, mood, etc.

Parameter Selection Suggestions

  1. Model Selection

    • gemini-2.5-flash-image: Suitable for quickly generating high-quality images
    • gemini-3-pro-image-preview: Supports image generation and editing, suitable for scenarios requiring editing based on existing images
  2. Prompt Optimization

    • Use detailed and descriptive text
    • Include specific visual elements and style requirements
    • Avoid vague or overly brief descriptions
    • When editing images, clearly describe how you want to modify the input image (add, remove, replace elements, etc.)
  3. Safety Settings

    • Adjust safety thresholds according to application scenarios

Common Issues

  1. Image Generation Failure

    • Check if the prompt complies with content policies
    • Verify API key permissions
    • Confirm the request format is correct
  2. Results Don't Match Expectations

    • Optimize prompt descriptions to be more specific and detailed
    • Add more visual details and style descriptions
    • Try different description approaches
  3. Safety Filter Issues

    • Modify the prompt to avoid triggering safety filters

Last updated on