Skip to content

Image Generation

Generate images using AI models via the chat completions endpoint.

Supported Models

  • google/gemini-2.5-flash-image-preview (aka Nano Banana) - faster, but lower quality
  • google/gemini-3-pro-image-preview (aka Nano Banana 3 Pro) - higher quality output, but also slower

Request Parameters

In addition to standard chat completion parameters, include:

ParameterTypeDescription
modalitiesarraySet to ["image", "text"] for image generation
image_configobjectOptional configuration for image output
image_config.aspect_ratiostringAspect ratio (see table below)

Supported Aspect Ratios

For Gemini image-generation models:

Aspect RatioDimensions
1:11024×1024 (default)
2:3832×1248
3:21248×832
3:4864×1184
4:31184×864
4:5896×1152
5:41152×896
9:16768×1344
16:91344×768
21:91536×672

Example Request

bash
curl https://ai.hackclub.com/proxy/v1/chat/completions \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "google/gemini-2.5-flash-image-preview",
    "messages": [
      {
        "role": "user",
        "content": "Make a picture of a sunset over mountains"
      }
    ],
    "modalities": ["image", "text"],
    "image_config": {
      "aspect_ratio": "16:9"
    },
    "stream": false
  }'
python
import base64
import requests

headers = {
    "Authorization": f"Bearer YOUR_API_KEY",
    "Content-Type": "application/json"
}

payload = {
    "model": "google/gemini-2.5-flash-image",
    "messages": [
        {
            "role": "user",
            "content": "Make a picture of a sunset over mountains"
        }
    ],
    "modalities": ["image", "text"],
    "image_config": {
        "aspect_ratio": "16:9"
    }
}

response = requests.post(
    "https://ai.hackclub.com/proxy/v1/chat/completions",
    headers=headers,
    json=payload
)
result = response.json()

if result.get("choices"):
    message = result["choices"][0]["message"]
    if message.get("images"):
        image_url = message["images"][0]["image_url"]["url"]
        
        # Handle data URI prefix
        if "," in image_url:
            base64_data = image_url.split(",")[1]
        else:
            base64_data = image_url

        image_bytes = base64.b64decode(base64_data)
        # Save or process image_bytes as needed

Response Format

The response includes an images array in the assistant message:

json
{
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": "Here is your image of a sunset over mountains.",
        "images": [
          {
            "type": "image_url",
            "image_url": {
              "url": "data:image/png;base64,..."
            }
          }
        ]
      },
      "finish_reason": "stop"
    }
  ]
}
  • Format: Images are returned as base64-encoded data URLs
  • Types: Typically PNG format (data:image/png;base64,)
  • Multiple Images: Some models can generate multiple images in a single response

Streaming

Image generation also works with streaming responses. Set stream: true to receive the response as server-sent events.

More Information

Refer to the OpenRouter image docs for more details.