Image generation
/v1/images/generations creates images from a text prompt in the OpenAI images format. The gateway translates the request for each image provider, returns images as URLs or base64 data, and records the cost per image from the price map, so image spend shows up in the same budgets and reports as text spend.
Scenario
A marketing team wants to generate draft illustrations from a content tool without handing out provider keys. The admin registers an image model, gives the team a key with a monthly budget, and the tool calls the gateway. Every image costs a known amount, the Usage page shows the team's image spend next to its text spend, and prompts go through the same guardrails as chat requests.
Steps
-
Register the image model. In the Admin UI open Models + Endpoints (
/ui/models-and-endpoints), click Add Model, pick the Provider, choose the model, set the Public Model Name togpt-image-2and set Mode to Image Generation - /images/generations, then click Add Model.model_list:- model_name: gpt-image-2spanway_params:model: openai/gpt-image-2api_key: os.environ/OPENAI_API_KEYmodel_info:mode: image_generation -
Create a key in Virtual Keys (
/ui/api-keys), pick the model under Models and set Max Budget (USD) under Optional Settings. -
Generate an image.
curl -X POST http://localhost:4000/v1/images/generations \-H "Authorization: Bearer $SPANWAY_API_KEY" \-H "Content-Type: application/json" \-d '{"model": "gpt-image-2","prompt": "A lighthouse on a cliff at dawn, watercolor","n": 1,"size": "1024x1024"}' -
Try it in the browser. Open Playground (
/ui/playground), set Endpoint Type to/v1/images/generations, pick the model, describe the image and send. The image appears in the conversation area. -
Check the cost. Open Logs (
/ui/logs) and find the request. The row shows the per-image cost.
Expected result
{
"created": 1760441234,
"data": [{"b64_json": "iVBORw0KGgo...", "revised_prompt": "A lighthouse on a cliff at dawn"}]
}
Providers that return URLs give url instead of b64_json. To save the image from the command line, pipe .data[0].b64_json through a base64 decoder.
Request fields
prompt, n, size, quality, style, response_format, user and output_format are accepted, and provider specific options such as aspect ratio, seed or negative prompts are forwarded as described in provider specific params.
Providers and pricing
mode: image_generation lets health checks send an image request and marks the model in the hub. Image models exist for OpenAI, Azure OpenAI, Vertex AI and Gemini, Bedrock, Stability AI, Recraft, fal, Black Forest Labs and several OpenAI compatible providers. Their per-image prices are listed on the provider pages, and a deployment can set output_cost_per_image under model_info when the price map has none.
Errors
| Status | What it means | Fix |
|---|---|---|
400 BadRequestError | prompt is missing, or size, quality or n is not valid for the model. | Use a size and quality the model supports. |
400 ContentPolicyViolationError | The provider's safety filter refused the prompt. | Change the prompt, or add a fallback deployment. |
401 AuthenticationError | The key is missing, wrong or expired. | Use a live key from Virtual Keys. |
403 PermissionDeniedError | The key may not use the model, or a pre_call guardrail blocked the prompt. | Add the model to the key, or read the guardrail result in Logs. |
429 RateLimitError | Budget or rate limit reached. | Raise the limit on the key. |
Cost shows 0 | The model has no per-image price. | Set output_cost_per_image on the deployment, see custom pricing. |
Related
- Image edits and image variations.
- Guardrails for screening prompts.
- Custom pricing for per-image prices.
Providers
The gateway's endpoint support matrix marks 16 provider integrations as supporting this endpoint:
AIHubMix (aihubmix), AI/ML API, Azure, Azure AI, Black Forest Labs (black_forest_labs), Eden AI, Fal AI, ModelScope, OpenAI, Qianwen AI Platform, QwenCloud, Recraft, RunwayML, Spanway Proxy, Stability AI, Google - Vertex AI.