Skip to main content
POST
/
v1beta
/
models
/
gemini-2.5-flash-image:streamGenerateContent
Image Generation (Streaming)
curl --request POST \
  --url https://www.anyfast.ai/v1beta/models/gemini-2.5-flash-image:streamGenerateContent \
  --header 'Content-Type: application/json' \
  --data '
{
  "contents": [
    {
      "role": "user",
      "parts": [
        {
          "text": "Generate an image of a sunset over mountains"
        }
      ]
    }
  ],
  "generationConfig": {
    "responseModalities": [
      "TEXT",
      "IMAGE"
    ],
    "imageConfig": {
      "aspectRatio": "16:9",
      "imageSize": "1K"
    }
  }
}
'
import requests

url = "https://www.anyfast.ai/v1beta/models/gemini-2.5-flash-image:streamGenerateContent"

payload = {
"contents": [
{
"role": "user",
"parts": [{ "text": "Generate an image of a sunset over mountains" }]
}
],
"generationConfig": {
"responseModalities": ["TEXT", "IMAGE"],
"imageConfig": {
"aspectRatio": "16:9",
"imageSize": "1K"
}
}
}
headers = {"Content-Type": "application/json"}

response = requests.post(url, json=payload, headers=headers)

print(response.text)
const options = {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
contents: [
{role: 'user', parts: [{text: 'Generate an image of a sunset over mountains'}]}
],
generationConfig: {
responseModalities: ['TEXT', 'IMAGE'],
imageConfig: {aspectRatio: '16:9', imageSize: '1K'}
}
})
};

fetch('https://www.anyfast.ai/v1beta/models/gemini-2.5-flash-image:streamGenerateContent', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));
<?php

$curl = curl_init();

curl_setopt_array($curl, [
CURLOPT_URL => "https://www.anyfast.ai/v1beta/models/gemini-2.5-flash-image:streamGenerateContent",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'contents' => [
[
'role' => 'user',
'parts' => [
[
'text' => 'Generate an image of a sunset over mountains'
]
]
]
],
'generationConfig' => [
'responseModalities' => [
'TEXT',
'IMAGE'
],
'imageConfig' => [
'aspectRatio' => '16:9',
'imageSize' => '1K'
]
]
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json"
],
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
package main

import (
"fmt"
"strings"
"net/http"
"io"
)

func main() {

url := "https://www.anyfast.ai/v1beta/models/gemini-2.5-flash-image:streamGenerateContent"

payload := strings.NewReader("{\n \"contents\": [\n {\n \"role\": \"user\",\n \"parts\": [\n {\n \"text\": \"Generate an image of a sunset over mountains\"\n }\n ]\n }\n ],\n \"generationConfig\": {\n \"responseModalities\": [\n \"TEXT\",\n \"IMAGE\"\n ],\n \"imageConfig\": {\n \"aspectRatio\": \"16:9\",\n \"imageSize\": \"1K\"\n }\n }\n}")

req, _ := http.NewRequest("POST", url, payload)

req.Header.Add("Content-Type", "application/json")

res, _ := http.DefaultClient.Do(req)

defer res.Body.Close()
body, _ := io.ReadAll(res.Body)

fmt.Println(string(body))

}
HttpResponse<String> response = Unirest.post("https://www.anyfast.ai/v1beta/models/gemini-2.5-flash-image:streamGenerateContent")
.header("Content-Type", "application/json")
.body("{\n \"contents\": [\n {\n \"role\": \"user\",\n \"parts\": [\n {\n \"text\": \"Generate an image of a sunset over mountains\"\n }\n ]\n }\n ],\n \"generationConfig\": {\n \"responseModalities\": [\n \"TEXT\",\n \"IMAGE\"\n ],\n \"imageConfig\": {\n \"aspectRatio\": \"16:9\",\n \"imageSize\": \"1K\"\n }\n }\n}")
.asString();
require 'uri'
require 'net/http'

url = URI("https://www.anyfast.ai/v1beta/models/gemini-2.5-flash-image:streamGenerateContent")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["Content-Type"] = 'application/json'
request.body = "{\n \"contents\": [\n {\n \"role\": \"user\",\n \"parts\": [\n {\n \"text\": \"Generate an image of a sunset over mountains\"\n }\n ]\n }\n ],\n \"generationConfig\": {\n \"responseModalities\": [\n \"TEXT\",\n \"IMAGE\"\n ],\n \"imageConfig\": {\n \"aspectRatio\": \"16:9\",\n \"imageSize\": \"1K\"\n }\n }\n}"

response = http.request(request)
puts response.read_body
{
  "candidates": [
    {
      "content": {
        "role": "model",
        "parts": [
          {
            "inlineData": {
              "mimeType": "image/png",
              "data": "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
            }
          }
        ]
      }
    }
  ],
  "usageMetadata": {
    "trafficType": "ON_DEMAND"
  },
  "modelVersion": "gemini-2.5-flash-image"
}
{
"error": {
"code": 123,
"message": "<string>",
"status": "<string>"
}
}
{
"error": {
"code": 123,
"message": "<string>",
"status": "<string>"
}
}
{
"error": {
"code": 123,
"message": "<string>",
"status": "<string>"
}
}

Query Parameters

key
string
required

API Key

alt
enum<string>

Set to sse for explicit Server-Sent Events mode (optional, streaming is the default behaviour of this endpoint)

Available options:
sse

Body

application/json
contents
object[]
required

Array of conversation turns. Each turn has a role and parts. A part can be a text prompt, or an inline_data image (base64). To use a reference image, include both a text part and an inline_data part in the same parts array.

Example:
[
{
"role": "user",
"parts": [
{
"text": "Generate an image of a sunset over mountains"
}
]
}
]
generationConfig
object
required

Response

Streaming SSE response. Each line starts with "data:" followed by a JSON chunk. Three chunk types are delivered in order: (1) Thinking chunks — parts[0].thought is true; (2) Image chunk — parts[0].inlineData contains mimeType and base64 data (camelCase); (3) Final usage chunk — top-level usageMetadata with thoughtsTokenCount.

A single SSE chunk. Three variants are possible: thinking chunk (parts[].thought=true), image chunk (parts[].inlineData), or usage chunk (no candidates).

candidates
object[]

Present in thinking and image chunks; absent in the final usage chunk.

usageMetadata
object

Token usage. Final chunk contains full details including thoughtsTokenCount.

modelVersion
string
Example:

"gemini-2.5-flash-image"

createTime
string
Example:

"2025-01-01T00:00:00Z"

responseId
string
Example:

"abc123"