Skip to content

Image Generation (Gemini native API)

1. Overview

OpenAI-compatible interface for the Nano Banana image model.

2. Request

  • Method:POST
  • Endpoint:

    https://gateway.serevixai.ai/v1/v1beta/models/{model}:generateContent
    

3. Parameters

3.1 Header Parameters

Parameter Type Required Description Example
Content-Type string Yes Sets the request content type. It must be application/json application/json
Authorization string Yes API key required for authentication, in the format Bearer $YOUR_API_KEY. Bearer $YOUR_API_KEY

3.2 Body Parameters (application/json)

Parameter Type Required Description Example
contents array Yes The content exchanged with the model. For a single request, provide one item. For multi-turn conversations, include the prior history together with the latest request. [{"role":"user","parts":[{"text":"A cute baby sea otter"}]}]
content.role string Yes Message role. Must be user or model. user
content.parts array No Ordered parts that make up a single message. Parts can use different MIME types. [{"text":"A cute baby sea otter"}]}]
content.parts.text string No Inline text content. A cute baby sea otter
content.parts.inlineData struct No Inline media bytes.
content.parts.inlineData.mimeType string Yes The IANA standard MIME type of the source data. image/png
content.parts.inlineData.data string Yes Raw media bytes encoded as a base64 string.
generationConfig struct No Configuration options for model generation and output.
generationConfig.imageConfig struct No Image generation configuration. If this field is used with a model that does not support these options, the API returns an error.
generationConfig.imageConfig.aspectRatio string No Optional image aspect ratio. Supported values: 1:1, 2:3, 3:2, 3:4, 4:3, 9:16, 16:9, and 21:9. 16:9
generationConfig.imageConfig.imageSize string No Generated image size. Supported values: 1K, 2K, and 4K. 1K

4. Request Examples

4.1 Text to Image

POST /v1/v1beta/models/gemini-2.5-flash-image:generateContent
Content-Type: application/json
Accept: application/json
Authorization: Bearer $YOUR_API_KEY

{
    "contents": [{
        "role": "user",
        "parts": [{
            "text": "A cute baby sea otter"
        }]
    }],
    "generationConfig": {
        "imageConfig": {
            "aspectRatio": "4:3",
            "imageSize": "1K"
        }
    }
}
curl https://gateway.serevixai.ai/v1/v1beta/models/gemini-2.5-flash-image:generateContent \
    -H "Content-Type: application/json" \
    -H "Accept: application/json" \
    -H "Authorization: Bearer $YOUR_API_KEY" \
    -d "{
    \"contents\": [{
        \"role\": \"user\",
        \"parts\": [{
            \"text\": \"A cute baby sea otter\"
        }]
    }],
    \"generationConfig\": {
        \"imageConfig\": {
            \"aspectRatio\": \"4:3\",
            \"imageSize\": \"1K\"
        }
    }
}"
package main

import (
    "context"
    "fmt"
    "os"

    "google.golang.org/genai"
)

func main() {

    apiKey := "sk-123456789012345678901234567890123456789012345678"
    exampleFilePath := "example.png"

    client, err := genai.NewClient(
        context.Background(),
        &genai.ClientConfig{
            APIKey:  apiKey,
            Backend: genai.BackendGeminiAPI,
            HTTPOptions: genai.HTTPOptions{
                BaseURL: "https://gateway.serevixai.ai",
            },
        })
    if err != nil {
        fmt.Println("error creating client:", err)
        return
    }

    resp, err := client.Models.GenerateContent(
        context.Background(),
        "gemini-2.5-flash-image",
        []*genai.Content{
            {
                Role: "user",
                Parts: []*genai.Part{
                    {Text: "A cute baby sea otter"},
                },
            },
        },
        &genai.GenerateContentConfig{
            ImageConfig: &genai.ImageConfig{
                AspectRatio: "4:3",
                ImageSize:   "1K",
            },
        },
    )
    if err != nil {
        fmt.Println("error:", err)
        return
    }

    var imageData []byte
    for _, candidate := range resp.Candidates {
        for _, part := range candidate.Content.Parts {
            if part.InlineData != nil && part.InlineData.MIMEType == "image/png" {
                imageData = part.InlineData.Data
                break
            }
        }
        if imageData != nil {
            break
        }
    }

    if imageData == nil {
        fmt.Println("error: no image data found")
        return
    }

    if err := os.WriteFile(exampleFilePath, imageData, 0644); err != nil {
        fmt.Println("error:", err)
        return
    }

    fmt.Println("success to write to file", exampleFilePath)
}
#!/usr/bin/env python3

from google import genai

def main():
    api_key = "sk-123456789012345678901234567890123456789012345678"
    example_file_path = "example.png"

    client = genai.Client(
        api_key=api_key,
        http_options={
            "base_url": "https://gateway.serevixai.ai"
        }
    )

    response = client.models.generate_content(
        model="gemini-2.5-flash-image",
        contents="A cute baby sea otter",
        config=genai.types.GenerateContentConfig(
            image_config=genai.types.ImageConfig(
                aspect_ratio="4:3",
                image_size="1K"
            )
        )
    )

    image_data = None
    for candidate in response.candidates:
        for part in candidate.content.parts:
            if part.inline_data and part.inline_data.mime_type == "image/png":
                image_data = part.inline_data.data
                break
        if image_data:
            break

    if not image_data:
        print("error: no image data found")
        return

    with open(example_file_path, "wb") as f:
        f.write(image_data)

    print(f"success to write to file {example_file_path}")

if __name__ == "__main__":
    main()

4.2 Image to Image

Supported Inline Image Types `image/png` `image/jpeg` `image/webp` `image/heic` `image/heif`
POST /v1/v1beta/models/gemini-2.5-flash-image:generateContent
Content-Type: application/json
Accept: application/json
Authorization: Bearer $YOUR_API_KEY

{
    "contents": [{
        "role": "user",
        "parts": [{
            "text": "add a rainbow"
        }, {
            "inlineData": {
                "mimeType": "image/png",
                "data": "..."
            }
        }]
    }],
    "generationConfig": {
        "imageConfig": {
            "aspectRatio": "4:3",
            "imageSize": "1K"
        }
    }
}
base64_image=$(base64 -i "Path/to/agi/image.png");
curl https://gateway.serevixai.ai/v1/v1beta/models/gemini-2.5-flash-image:generateContent \
    -H "Content-Type: application/json" \
    -H "Accept: application/json" \
    -H "Authorization: Bearer $YOUR_API_KEY" \
    -d "{
    \"contents\": [{
        \"role\": \"user\",
        \"parts\": [{
            \"text\": \"add a rainbow\"
        }, {
            \"inlineData\": {
                \"mimeType\": \"image/png\",
                \"data\": \"${base64_image}\"
            }
        }]
    }],
    \"generationConfig\": {
        \"imageConfig\": {
            \"aspectRatio\": \"4:3\",
            \"imageSize\": \"1K\"
        }
    }
}"
package main

import (
    "context"
    "fmt"
    "os"

    "google.golang.org/genai"
)

func main() {

    apiKey := "sk-123456789012345678901234567890123456789012345678"
    inputPath := "example.png"
    outputPath := "example2.png"

    inputData, err := os.ReadFile(inputPath)
    if err != nil {
        fmt.Println("error:", err)
        return
    }

    client, err := genai.NewClient(
        context.Background(),
        &genai.ClientConfig{
            APIKey:  apiKey,
            Backend: genai.BackendGeminiAPI,
            HTTPOptions: genai.HTTPOptions{
                BaseURL: "https://gateway.serevixai.ai",
            },
        })
    if err != nil {
        fmt.Println("error:", err)
        return
    }

    resp, err := client.Models.GenerateContent(
        context.Background(),
        "gemini-2.5-flash-image",
        []*genai.Content{
            {
                Role: "user",
                Parts: []*genai.Part{
                    {Text: "add a rainbow"},
                    {
                        InlineData: &genai.Blob{
                            MIMEType: "image/png",
                            Data:     inputData,
                        },
                    },
                },
            },
        },
        &genai.GenerateContentConfig{
            ImageConfig: &genai.ImageConfig{
                AspectRatio: "4:3",
                ImageSize:   "1K",
            },
        },
    )
    if err != nil {
        fmt.Println("error:", err)
        return
    }

    var imageData []byte
    for _, candidate := range resp.Candidates {
        for _, part := range candidate.Content.Parts {
            if part.InlineData != nil && part.InlineData.MIMEType == "image/png" {
                imageData = part.InlineData.Data
                break
            }
        }
        if imageData != nil {
            break
        }
    }

    if imageData == nil {
        fmt.Println("error: no image data found")
        return
    }

    if err := os.WriteFile(outputPath, imageData, 0644); err != nil {
        fmt.Println("error:", err)
        return
    }

    fmt.Println("success to write to file", outputPath)
}
#!/usr/bin/env python3

from google import genai

def main():
    api_key = "sk-123456789012345678901234567890123456789012345678"
    input_path = "example.png"
    output_path = "example2.png"

    with open(input_path, "rb") as f:
        input_data = f.read()

    client = genai.Client(
        api_key=api_key,
        http_options={
            "base_url": "https://gateway.serevixai.ai"
        }
    )

    response = client.models.generate_content(
        model="gemini-2.5-flash-image",
        contents=[
            "add a rainbow",
            genai.types.Part(
                inline_data=genai.types.Blob(
                    mime_type="image/png",
                    data=input_data
                )
            )
        ],
        config=genai.types.GenerateContentConfig(
            image_config=genai.types.ImageConfig(
                aspect_ratio="4:3",
                image_size="1K"
            )
        )
    )

    image_data = None
    for candidate in response.candidates:
        for part in candidate.content.parts:
            if part.inline_data and part.inline_data.mime_type == "image/png":
                image_data = part.inline_data.data
                break
        if image_data:
            break

    if not image_data:
        print("error: no image data found")
        return

    with open(output_path, "wb") as f:
        f.write(image_data)

    print(f"success to write to file {output_path}")

if __name__ == "__main__":
    main()

5. Response Example

{
  "candidates": [
    {
      "content": {
        "role": "model",
        "parts": [
          {
            "inlineData": {
              "mimeType": "image/png",
              "data": "..."
            }
          }
        ]
      },
      "finishReason": "STOP"
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 5,
    "candidatesTokenCount": 1290,
    "totalTokenCount": 1295,
    "trafficType": "ON_DEMAND",
    "promptTokensDetails": [
      {
        "modality": "TEXT",
        "tokenCount": 5
      }
    ],
    "candidatesTokensDetails": [
      {
        "modality": "IMAGE",
        "tokenCount": 1290
      }
    ]
  },
  "modelVersion": "gemini-2.5-flash-image",
  "createTime": "2025-12-02T05:44:30.324935Z",
  "responseId": "vmU2acfqE--ijeYP-9ykgAI"
}