Skip to content
Browse the docs

Guides · curl · Python · 5 min

Generate an image

Choose a model, set size and shape, and receive base64 data or signed links.

POST /v1/images/generations creates images from a prompt and returns them in the same response.

Pick a model

Image models appear in GET /v1/models with "type": "image". Each model declares which parameters it accepts; a parameter it does not declare is rejected with 400, so you always know whether an option was applied.

Generate

bash
curl https://api.useunbound.ai/v1/images/generations \
  -H "Authorization: Bearer $UNBOUND_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "<image-model-id>",
    "prompt": "A lighthouse at dusk, long exposure",
    "aspect_ratio": "16:9",
    "response_format": "url"
  }'
json
{
  "created": 1795500000,
  "data": [{ "url": "https://api.useunbound.ai/api/assets/<asset-id>?expires=…&signature=…" }]
}

Size and shape

There are two ways to say how large the image should be:

  • size, in the OpenAI style ("1024x1024"). It is mapped to the nearest aspect ratio and resolution the model offers.
  • aspect_ratio (for example "16:9") and resolution (for example "2k"). These are Unbound fields and take precedence over size.

Getting the bytes

By default the response contains base64 data in b64_json, with the image type in mime_type:

python
import base64
from openai import OpenAI

client = OpenAI(base_url="https://api.useunbound.ai/v1", api_key="<your Unbound key>")

result = client.images.generate(model="<image-model-id>", prompt="A lighthouse at dusk, long exposure")
with open("lighthouse.png", "wb") as f:
    f.write(base64.b64decode(result.data[0].b64_json))

With "response_format": "url" you get signed links instead. A link works without a key and expires after one hour, so download the file rather than storing the link. Images generated through the API are also kept in your Library.

What you pay

The exact price for the request is held before generation starts. Then:

  • If every image is delivered, the hold is charged.
  • If fewer images are delivered than you asked for, only the delivered ones are charged.
  • If generation fails or is refused, nothing is charged.

Retrying safely

Image generation can take a while, and connections drop. Send an Idempotency-Key header so that a retry does not generate, or charge, twice. See Retry without paying twice.

Not available yet

Editing an existing image, masks, reference images and upscaling are in development. They are not part of the API today.