Skip to content

Image Generation

POST /v1/images/generations

OpenAI-compatible image generation. The gateway fans out across every configured image provider and picks a healthy one based on live success rate and daily-limit headroom.

FieldTypeDefaultDescription
modelstring"auto"Model identifier (see Providers). Use "auto" for health-aware routing across all enabled image models.
promptstringrequiredText description of the image. 1–8000 chars.
nnumber1Number of images to generate (1–4). Not all providers honour values greater than 1 — excess images may be ignored.
sizestringprovider defaultOne of "256x256", "512x512", "1024x1024", "1024x1792", "1792x1024".
response_formatstring"url""url" returns a hosted URL; "b64_json" returns a base64-encoded PNG/JPEG.
qualitystringPassed through when the provider supports it (currently a no-op for most).
stylestringPassed through when the provider supports it.
project_idstringOptional body-level project tag. Same semantics as the x-gateway-project-id header.
HeaderRequiredDescription
Authorization: Bearer <GATEWAY_API_KEY>yesGateway API key.
x-gateway-project-idyes (or in body)1–64 chars [a-zA-Z0-9._:-]. Required for analytics + rate accounting. Either send the header or include project_id in the JSON body.
x-gateway-force-providernoPin a specific provider (together, workers_ai, gemini, nvidia, pollinations). Bypasses health-aware routing.
ProviderModelsNotes
Together FLUXblack-forest-labs/FLUX.1-schnell, FLUX.1.1-pro, FLUX.1-kontext-pro, FLUX.2-dev, FLUX.2-flex, FLUX.2-pro, FLUX.2-maxRequires TOGETHER_API_KEY. Highest priority in auto.
Gemini Imagenimagen-4.0-generate-001, gemini-2.5-flash-imageRequires GEMINI_API_KEY.
Cloudflare Workers AI@cf/black-forest-labs/flux-1-schnell, @cf/stabilityai/stable-diffusion-xl-base-1.0, @cf/lykon/dreamshaper-8-lcmNo external key — uses Workers AI binding.
NVIDIA NIMblack-forest-labs/flux.1-schnellRequires NVIDIA_API_KEY.
Pollinationsflux, flux-realism, turboNo key — public free tier. Lowest priority fallback.

Call GET /v1/models for the live enabled set with health state.

{
"created": 1714000000,
"data": [
{
"url": "https://cdn.example/generated-image.png",
"revised_prompt": "A cinematic close-up of a red fox in an autumn forest"
}
],
"x_gateway": {
"provider": "together",
"model": "black-forest-labs/FLUX.1-schnell",
"attempts": 1,
"request_id": "req_abc123",
"project_id": "my_project"
}
}

When response_format: "b64_json", each data[i] contains b64_json instead of url.

The gateway tries up to 3 candidates from the health-sorted pool before giving up with 502 provider_error.

Terminal window
curl https://free-ai-gateway.sarthakagrawal927.workers.dev/v1/images/generations \
-H "Authorization: Bearer <GATEWAY_API_KEY>" \
-H "x-gateway-project-id: my_project" \
-H "Content-Type: application/json" \
-d '{
"model": "auto",
"prompt": "A cinematic close-up of a red fox in an autumn forest",
"size": "1024x1024"
}'
Terminal window
curl https://free-ai-gateway.sarthakagrawal927.workers.dev/v1/images/generations \
-H "Authorization: Bearer <GATEWAY_API_KEY>" \
-H "x-gateway-project-id: my_project" \
-H "Content-Type: application/json" \
-d '{
"model": "black-forest-labs/FLUX.1-schnell",
"prompt": "A watercolor painting of Mount Fuji at sunrise",
"response_format": "b64_json"
}'
Terminal window
curl https://free-ai-gateway.sarthakagrawal927.workers.dev/v1/images/generations \
-H "Authorization: Bearer <GATEWAY_API_KEY>" \
-H "x-gateway-project-id: my_project" \
-H "x-gateway-force-provider: workers_ai" \
-H "Content-Type: application/json" \
-d '{
"model": "@cf/black-forest-labs/flux-1-schnell",
"prompt": "A neon cyberpunk alley at night"
}'
StatuscodeMeaning
400invalid_project_idMissing/invalid project_id.
502provider_errorAll attempted providers failed in sequence. Response message includes the last upstream error.
503no_image_providerNo image provider key/binding is configured.