Generate images
Make images from a text prompt through Stav with the OpenAI SDK — change the base URL and the key. Always base64, nothing stored, billed in the provider's own unit.
Stav serves image models on POST /v1/images/generations, in the same shape
as OpenAI's images API. Code that calls images.generate() today changes two
things: the base URL and the key.
Before you start
- A Stav API key with the
inference:imagesscope, or a key with no scopes. Create one under API keys. - An image model. In the catalogue, filter on Image. From code, an image
model lists
imagesinstav_surfacesonGET /v1/models.
Make an image
Python, OpenAI SDK
import base64, os
from openai import OpenAI
client = OpenAI(base_url="https://api.stav.ai/v1", api_key=os.environ["STAV_API_KEY"])
img = client.images.generate(
model="openai/gpt-image-2",
prompt="A lighthouse on a Norwegian skerry at dawn",
size="1024x1024",
quality="low",
)
open("image.png", "wb").write(base64.b64decode(img.data[0].b64_json))
curl
curl https://api.stav.ai/v1/images/generations \
-H "Authorization: Bearer $STAV_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model": "openai/gpt-image-2",
"prompt": "A lighthouse on a Norwegian skerry at dawn",
"size": "1024x1024",
"quality": "low"}' \
| jq -r '.data[0].b64_json' | base64 --decode > image.png
You can also try a model without code: open its page and click Try model.
The answer is always base64
Stav answers with the image itself in data[].b64_json. When a provider
answers with a link on its own host, Stav fetches the image and puts the bytes
in the answer, so you never fetch from the provider and no link expires.
Stav stores no image.
response_format: "url" is refused with 400 unsupported_parameter.
Size and quality
Send size as WIDTHxHEIGHT (1024x1024) or auto, and quality as the
provider's own word (low, medium, high, …). The sizes and qualities a
model is priced for are in stav_image_options on
GET /v1/models/{model_id}. A size the model does not make is the provider's
own 400, passed on.
quality changes the price on a model priced in tokens: high can cost
several times low. Send the one you want; with none, the provider picks.
Some models take no quality at all (Gemini image models): sending one is
refused with 400 unsupported_parameter before anything is sent or charged.
More than one image
Send n (up to 4) for several images in one request. Some models make one
image per request: they refuse n above 1 with 400 n_not_supported
before anything is sent or charged. stav_image_options.max_n on
GET /v1/models/{model_id} says which in advance; for those, repeat the
call.
What it costs
Each request is billed in the unit the serving Provider Endpoint is priced in:
| Unit | What you pay |
|---|---|
| Per image | A flat price, or a price by size and quality. Known before the image is made: a balance below it is refused with 402 and nothing is sent. |
| Per megapixel | The measured pixels of the image that came back. A larger image than you asked for is billed as returned — we bill what the provider bills. |
| Tokens | The text and image tokens the provider reports. |
| Per request | One price per request. |
usage.cost in the answer, and the X-Stav-Cost header, are what your wallet
is charged, in your workspace's currency. A failed request is not charged, and
neither is a content refusal. See Credits and billing.
Where it runs
The same policy as chat applies: the countries your workspace allows, zero
data retention, no training, the sovereignty floor and the providers you
block. A request that no Provider Endpoint of the model may serve is refused,
and the refusal says why. To narrow one request further, send
"provider": {"runs_in": "eu"}.
Content
Stav adds no content filter of its own. When a provider refuses a prompt under
its content policy, you get 400 content_policy_violation with the provider's
reason, and you are not charged.
Not in this version
- Variations. To change an image you already have, see Edit images.
autoand Routers: name the model.- Streaming.
The full reference, with every field and error: Images API.