N·00Image API

Generating images over the API

This page is for people who want to generate images in their own programs. There are two ways, one per tab: to render a picture from a description, or edit one you have, use the Images API, the simplest; to have a chat model produce images as you talk and revise one back and forth, use the Responses endpoint. The address is the same as for text, https://api.xylemnode.com/v1. Your key's group needs to include the image models (GPT Image).

Images API: generate and edit

If all you want is the picture, this is the simplest route: one request returns a finished image. The image doesn't come back as a link but as a long text encoding (Base64); the samples below turn it back into an image file on your computer.

  1. 01

    Generate from a description: /v1/images/generations

    Describe the picture you want in prompt; model is the image model, size the picture size and quality the level of detail. The command below is for macOS / Linux (it needs jq installed) and leaves a moon-cat.png in the current folder; on Windows, skip to the Python version further down.

    terminal
    curl https://api.xylemnode.com/v1/images/generations \
      -H "Authorization: Bearer sk-your-key" \
      -H "Content-Type: application/json" \
      -d '{
        "model": "gpt-image-2.5-flare",
        "prompt": "A ginger cat in a spacesuit standing on the moon, blue Earth in the distance, cinematic lighting, photorealistic.",
        "size": "1024x1536",
        "quality": "high"
      }' | jq -r '.data[0].b64_json' | base64 --decode > moon-cat.png

    You should seeA moon-cat.png appears in the current folder and opens normally.

    Why, and what to do if it goes wrong

    The GPT Image models only return b64_json (the image in Base64), never a link; the jq and base64 at the end take it out and turn it back into a file. Images take longer than text, so anything from ten seconds to a couple of minutes is normal.

  2. 02

    Edit an existing image: /v1/images/edits

    Upload the original picture and say in prompt how to change it. The example recolours the sofa in a living-room photo and saves living-room-edited.png. To change only part of the picture, also upload a mask with the area to change made transparent; if the whole picture may change, delete the mask line.

    terminal
    curl https://api.xylemnode.com/v1/images/edits \
      -H "Authorization: Bearer sk-your-key" \
      -F "model=gpt-image-2.5-sunburst" \
      -F "image[][email protected]" \
      -F "[email protected]" \
      -F "prompt=Replace the sofa with a dark green velvet one and keep everything else the same." \
      | jq -r '.data[0].b64_json' | base64 --decode > living-room-edited.png

    You should seeA living-room-edited.png appears in the current folder.

    Why, and what to do if it goes wrong

    Up to 16 originals (image[]), png, webp or jpg, each under 50MB; given several, the model treats them as references and composes one image. The mask must be a PNG with an alpha channel, the same size as the original and under 4MB. Fully transparent areas are the ones repainted. The model treats it as a guide and may not follow its outline exactly; with several originals it applies to the first. File names need the right extension (.png / .jpg / .webp). curl sets up the upload format itself, so don't add a Content-Type header by hand.

  3. 03

    In Python (works on Windows too)

    Both features in Python, which works on Windows and Mac alike. Run pip install requests, save the code as image.py and run it.

    python
    import base64
    import requests
    
    API = "https://api.xylemnode.com/v1"
    HEADERS = {"Authorization": "Bearer sk-your-key"}
    
    # Generate from a description
    result = requests.post(
        f"{API}/images/generations",
        headers=HEADERS,
        json={
            "model": "gpt-image-2.5-flare",
            "prompt": "A ginger cat in a spacesuit standing on the moon, blue Earth in the distance, cinematic lighting, photorealistic.",
            "size": "1024x1536",
            "quality": "high",
        },
        timeout=600,
    )
    result.raise_for_status()
    with open("moon-cat.png", "wb") as file:
        file.write(base64.b64decode(result.json()["data"][0]["b64_json"]))
    
    # Edit an existing image (the mask is optional)
    with open("living-room.png", "rb") as image, open("mask.png", "rb") as mask:
        result = requests.post(
            f"{API}/images/edits",
            headers=HEADERS,
            data={
                "model": "gpt-image-2.5-sunburst",
                "prompt": "Replace the sofa with a dark green velvet one and keep everything else the same.",
            },
            files=[
                ("image[]", ("living-room.png", image, "image/png")),
                ("mask", ("mask.png", mask, "image/png")),
            ],
            timeout=600,
        )
    result.raise_for_status()
    with open("living-room-edited.png", "wb") as file:
        file.write(base64.b64decode(result.json()["data"][0]["b64_json"]))

    You should seemoon-cat.png and living-room-edited.png appear in the current folder.

    Why, and what to do if it goes wrong

    Generation sends json=, edits upload with files=, and both take the image from data[0].b64_json in the result. timeout=600 gives image generation enough time; don't lower it much.

  4. 04

    Common parameters

    Change these to adjust the result. Choosing the image model: gpt-image-2.5-flare is fast and fine for everyday renders; gpt-image-2.5-sunburst is more detailed and suits edits. Go by the model list in your console.

    n
    Images per request, 1–10
    size
    1024x1024 · 1536x1024 · 1024x1536 · auto
    quality
    low · medium · high · auto (the 2.5 models add xhigh · max)
    background
    auto · opaque · transparent (transparent needs png or webp)
    output_format
    png · jpeg · webp
    output_compression
    0–100, jpeg / webp only
    Rules for custom sizes

    Besides the sizes in the table, size takes your own width and height, as long as both sides are multiples of 16, the aspect ratio is between 1:3 and 3:1, no side is longer than 3840, and the total is 655,360 to 8,294,400 pixels (1024x640 up to 3840x2160, for example). Sizes above 2560x1440 are still experimental.

Endpoints at a glance

Generate
POST https://api.xylemnode.com/v1/images/generations (JSON)
Edit / compose
POST https://api.xylemnode.com/v1/images/edits (multipart)
Image models
gpt-image-2.5-flare · gpt-image-2.5-sunburst
Result field
data[].b64_json (Base64)
Get API Key