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.
- 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.pngYou 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.
- 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.pngYou 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.
- 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.
- 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)
Responses endpoint: images inside a conversation
Use this when you want the model to understand your request before it renders, or to revise the same image back and forth: you talk to a chat model, it calls the image tool when needed and hands the image back to you.
- 01
Build the request body
First confirm the request goes through with curl. This command prints a long stream (the image's encoding included) straight into the terminal and is only a test; to save the image as a file, use the Python in the next step. On Windows, go straight to the next step.
terminal curl https://api.xylemnode.com/v1/responses \ -H "Authorization: Bearer sk-your-key" \ -H "Content-Type: application/json" \ -d '{ "model": "gpt-6.2-astra", "instructions": "Generate the requested image.", "input": [ { "role": "user", "content": "A ginger cat in a spacesuit standing on the moon, blue Earth in the distance, cinematic lighting, photorealistic." } ], "tools": [{ "type": "image_generation", "model": "gpt-image-2.5-flare" }], "tool_choice": { "type": "image_generation" }, "stream": true, "store": false }'You should seeThe terminal fills with lines starting with data:, and no error.
Why, and what to do if it goes wrong
The four settings that matter: put image_generation in tools and name the image model; set tool_choice to the same type so it always renders instead of replying in text; turn stream on, since images are slow and receiving them as they're made is less likely to break midway; turn store off so the turn isn't kept on the server. The image description goes into input as a user message, and instructions only states the job.
- 02
Pull the image out of the stream
Run pip install requests, save the code below as chat-image.py and run it. It reads the result as it arrives, picks out the image and saves it as moon-cat.png.
python import base64 import json import requests PROMPT = "A ginger cat in a spacesuit standing on the moon, blue Earth in the distance, cinematic lighting, photorealistic." payload = { "model": "gpt-6.2-astra", "instructions": "Generate the requested image.", "input": [{"role": "user", "content": PROMPT}], "tools": [{"type": "image_generation", "model": "gpt-image-2.5-flare"}], "tool_choice": {"type": "image_generation"}, "stream": True, "store": False, } with requests.post( "https://api.xylemnode.com/v1/responses", headers={"Authorization": "Bearer sk-your-key"}, json=payload, stream=True, timeout=600, ) as response: response.raise_for_status() for raw in response.iter_lines(): if not raw: continue line = raw.decode("utf-8") if not line.startswith("data:"): continue chunk = line[5:].strip() if chunk == "[DONE]": break event = json.loads(chunk) if event.get("type") != "response.output_item.done": continue item = event.get("item") or {} if item.get("type") == "image_generation_call" and item.get("result"): with open("moon-cat.png", "wb") as file: file.write(base64.b64decode(item["result"])) print("saved moon-cat.png")You should seeThe terminal prints that moon-cat.png was saved, and the file opens normally.
Why, and what to do if it goes wrong
The result is a series of lines starting with data:. The code only looks at events whose type is response.output_item.done, and among them the one whose item.type is image_generation_call; its item.result is the image in Base64, decoded into the file. result is a long string, not an image link, so don't use it as an address or print it to the terminal.
- 03
Swap models, tune the output
To change the image model or the result, edit the tools entry in the request, as below: size is the size, quality the detail, background set to transparent gives a transparent background (only png and webp support it), and output_format the file type. Image models: gpt-image-2.5-flare is fast, gpt-image-2.5-sunburst more detailed but slower; go by the model list in your console.
json { "tools": [{ "type": "image_generation", "model": "gpt-image-2.5-flare", "size": "1024x1536", "quality": "high", "background": "transparent", "output_format": "png", "partial_images": 2 }] }Why, and what to do if it goes wrong
Add partial_images (1–3) and you'll receive a few preview drafts while it renders, as response.image_generation_call.partial_image events.
Parameters
- Endpoint
https://api.xylemnode.com/v1/responses- Method
POST (Bearer auth)- Mainline model
gpt-6.2-astra- Tool
image_generation- Image model
gpt-image-2.5-flare · gpt-image-2.5-sunburst- Result field
image_generation_call.result (Base64)