Skip to main content

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​

  1. 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 to gpt-image-2 and set Mode to Image Generation - /images/generations, then click Add Model.

    model_list:
    - model_name: gpt-image-2
    spanway_params:
    model: openai/gpt-image-2
    api_key: os.environ/OPENAI_API_KEY
    model_info:
    mode: image_generation
  2. Create a key in Virtual Keys (/ui/api-keys), pick the model under Models and set Max Budget (USD) under Optional Settings.

  3. 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"
    }'
  4. 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.

  5. 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​

StatusWhat it meansFix
400 BadRequestErrorprompt is missing, or size, quality or n is not valid for the model.Use a size and quality the model supports.
400 ContentPolicyViolationErrorThe provider's safety filter refused the prompt.Change the prompt, or add a fallback deployment.
401 AuthenticationErrorThe key is missing, wrong or expired.Use a live key from Virtual Keys.
403 PermissionDeniedErrorThe 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 RateLimitErrorBudget or rate limit reached.Raise the limit on the key.
Cost shows 0The model has no per-image price.Set output_cost_per_image on the deployment, see custom pricing.

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.