Overview

Streaming Image API

Call any route below directly. The API returns an SSE stream.

MethodPOST
Path/v1/responses
AuthAuthorization: Bearer sk-your-key
Accepttext/event-stream

Direct Responses streaming image generation.

Text to Image

Text to Image: curl Example

Recommended cdn route
curl --location 'https://cdn.aiswing.fun/v1/responses' \
  --header 'Authorization: Bearer sk-your-key' \
  --header 'Content-Type: application/json' \
  --header 'Accept: text/event-stream' \
  --data '{
    "model": "gpt-image-2",
    "input": [{
      "role": "user",
      "content": [{
        "type": "input_text",
        "text": "A premium product poster, clean studio lighting, no text, no watermark"
      }]
    }],
    "tools": [{
      "type": "image_generation",
      "size": "3840x2160",
      "quality": "high",
      "output_format": "png"
    }],
    "stream": true
  }'

Use 3840x2160 for 4K landscape and 2160x3840 for 4K portrait.

Image to Image

Image to Image: With Reference Images

reference_images
curl --location 'https://cdn.aiswing.fun/v1/responses' \
  --header 'Authorization: Bearer sk-your-key' \
  --header 'Content-Type: application/json' \
  --header 'Accept: text/event-stream' \
  --data '{
    "model": "gpt-image-2",
    "input": [{
      "role": "user",
      "content": [
        {"type": "input_image", "image_url": "data:image/png;base64,iVBORw0KGgoAAA..."},
        {"type": "input_text", "text": "Keep the same subject, turn it into a cinematic product poster"}
      ]
    }],
    "tools": [{
      "type": "image_generation",
      "size": "3840x2160",
      "quality": "high",
      "output_format": "png"
    }],
    "stream": true
  }'

Use data:image/png;base64,... for reference images.

Three Routes

cdnhttps://cdn.aiswing.fun/v1/responsesDefault recommended route
gpthttps://gpt.aiswing.fun/v1/responsesBackup route
codexhttps://codex.aiswing.fun/v1/responsesBackup route

Request Parameters

modelModel

Recommended: gpt-image-2.

promptPrompt

Required. Describe subject, style, composition, lighting, colors, and unwanted text or watermark.

sizeResolution

Supports common sizes, 4K sizes, and auto.

qualityQuality

Optional: auto, low, medium, high.

formatOutput Format

Optional: png, jpeg, webp.

reference_imagesReference Images

Optional array for image-to-image.

SSE Streaming Events

response.image_generation_call.partial_imagePreview image

Read partial_image_b64.

response.completedFinal image

Usually in response.output[].result.

[DONE]Done

SSE stream termination marker.

JavaScript Streaming Example

const endpoint = "https://cdn.aiswing.fun/v1/responses";
const response = await fetch(endpoint, {
  method: "POST",
  headers: {
    "Authorization": "Bearer sk-your-key",
    "Content-Type": "application/json",
    "Accept": "text/event-stream"
  },
  body: JSON.stringify({
    model: "gpt-image-2",
    input: [{
      role: "user",
      content: [{ type: "input_text", text: "4K product poster, clean studio lighting" }]
    }],
    tools: [{
      type: "image_generation",
      size: "3840x2160",
      quality: "high",
      output_format: "png"
    }],
    stream: true
  })
});

const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = "";
while (true) {
  const { value, done } = await reader.read();
  if (done) break;
  buffer += decoder.decode(value, { stream: true });
  const chunks = buffer.split("\n\n");
  buffer = chunks.pop() || "";
  for (const chunk of chunks) {
    const line = chunk.split("\n").find((x) => x.startsWith("data:"));
    if (!line) continue;
    const raw = line.slice(5).trim();
    if (raw === "[DONE]") break;
    const event = JSON.parse(raw);
    const b64 = event.b64_json || event.partial_image_b64 || event.response?.output?.find?.((x) => x.result)?.result;
    if (b64) document.querySelector("img").src = `data:image/png;base64,${b64}`;
  }
}