Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.anyfast.ai/llms.txt

Use this file to discover all available pages before exploring further.

Gemini 3.1 Flash Image Preview (Stream) generates images using Google’s Gemini 3.1 Flash Image Preview model through the native Gemini API via Anyfast. Responses are delivered in real-time using Server-Sent Events (SSE), with thinking chunks arriving first followed by the image data chunk.
Streaming requires a Direct group token. Select the Direct group when creating your token in the Anyfast console.

Key capabilities

  • SSE Streaming — Real-time delivery of thinking and image chunks
  • Thinking mode — Internal reasoning chunks (thought: true) stream before the final image
  • Text-to-image — Generate images from text descriptions
  • Image editing — Pass a reference image in inline_data alongside your text instruction
  • Aspect ratio control1:1, 4:3, 3:4, 16:9, 9:16
  • Resolution control1K (~1024px), 2K (~2048px), 4K (~4096px) on the long edge

SSE response format

The streaming endpoint returns newline-delimited SSE lines. Each line starts with data: followed by a JSON object. There are three chunk types:
  1. Thinking chunks — Arrive first; parts[0].thought is true
  2. Image chunk — Contains parts[0].inlineData with mimeType and base64 data (note: camelCase in stream response)
  3. Final usage chunk — Contains top-level usageMetadata with thoughtsTokenCount and token details
data: {"candidates":[{"content":{"role":"model","parts":[{"text":"...","thought":true}]}}],"usageMetadata":{"trafficType":"ON_DEMAND"},"modelVersion":"gemini-3.1-flash-image-preview","createTime":"...","responseId":"..."}

data: {"candidates":[{"content":{"role":"model","parts":[{"inlineData":{"mimeType":"image/png","data":"<base64>"}}]}}],...}

data: {"usageMetadata":{"promptTokenCount":8,"candidatesTokenCount":1120,"totalTokenCount":1392,"trafficType":"ON_DEMAND","promptTokensDetails":[{"modality":"TEXT","tokenCount":8}],"candidatesTokensDetails":[{"modality":"IMAGE","tokenCount":1120}],"thoughtsTokenCount":264}}
In the streaming response, the image field is named inlineData (camelCase). In the non-streaming request body, the field is inline_data (snake_case). This is a native Gemini API behaviour.

Text-to-image example

curl "https://www.anyfast.ai/v1beta/models/gemini-3.1-flash-image-preview:streamGenerateContent?key=YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [
      {
        "role": "user",
        "parts": [
          { "text": "Generate an image of a sunset over mountains" }
        ]
      }
    ],
    "generationConfig": {
      "responseModalities": ["TEXT", "IMAGE"],
      "imageConfig": {
        "aspectRatio": "16:9",
        "imageSize": "1K"
      }
    }
  }'

Image editing example (with reference image)

Include both a text instruction and an inline_data reference image in the same parts array.
# First encode your image to base64:
# BASE64=$(base64 -i your_photo.jpg)
#
# Then send the request:
curl "https://www.anyfast.ai/v1beta/models/gemini-3.1-flash-image-preview:streamGenerateContent?key=YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [
      {
        "role": "user",
        "parts": [
          {
            "text": "Hi, this is a picture of me. Can you add a llama next to me?"
          },
          {
            "inline_data": {
              "mime_type": "image/jpeg",
              "data": "<YOUR_BASE64_ENCODED_IMAGE>"
            }
          }
        ]
      }
    ],
    "generationConfig": {
      "responseModalities": ["TEXT", "IMAGE"],
      "imageConfig": {
        "aspectRatio": "1:1",
        "imageSize": "1K"
      }
    }
  }'

Parameters

ParameterTypeRequiredDescription
keystringYesAPI key (query parameter)
altstringNoSet to sse for explicit SSE mode (optional, streaming is default)
contents[].parts[].textstringYesText prompt or instruction
contents[].parts[].inline_data.mime_typestringNoReference image type: image/jpeg, image/png, image/webp
contents[].parts[].inline_data.datastringNoBase64-encoded reference image
generationConfig.responseModalitiesarrayYes["IMAGE"] or ["TEXT", "IMAGE"]
generationConfig.imageConfig.aspectRatiostringNo1:1 / 4:3 / 3:4 / 16:9 / 9:16
generationConfig.imageConfig.imageSizestringNo1K / 2K / 4K (default: 1K)

API Reference

View the interactive API playground for Gemini 3.1 Flash Image Preview (Stream).