Skip to content

gpt-image generation and editing

Call the gpt-image models in the OpenAI Images format. Generate images from a text description (the 2.6 models also output animated GIFs and SVG vector images), or upload reference images and edit them.

These two endpoints follow the OpenAI Images API format and serve the gpt-image models: /images/generations generates an image from a text description, and /images/edits changes the reference images you upload. Which models you can use depends on your key's group (see Groups), and each model's price is in Model Square.

POST https://codaapi.com/v1/images/generations
POST https://codaapi.com/v1/images/edits

Requests are synchronous: the response comes back once the image is ready. The low quality level usually takes ten to a few dozen seconds, while high quality, large sizes or SVG output can take a minute or two, so set your client's read timeout to 180 seconds or more.

Models

These are the gpt-image models on the live site when this page was written. Models come and go, and Model Square is authoritative:

Model Billing Groups
gpt-image-1, gpt-image-1.5, gpt-image-2.6-flare, gpt-image-2.6-sunburst Token-based OpenAI-官转
gpt-image-2, gpt-image-2.5-flare, gpt-image-2.5-sunburst Token-based OpenAI-官转, OpenAI-AZ
gpt-image-2-client, gpt-image-2.5-flare-client, gpt-image-2.5-sunburst-client, gpt-image-2.6-flare-client, gpt-image-2.6-sunburst-client Per Request GPT-image特价

The 2.5 and 2.6 series each come in two versions: the flare ones are fast, good for everyday use and for trying out prompts; the sunburst ones render finer pictures, suited to images with lots of detail or text, and take noticeably longer.

The -client models are billed per request and are only in the GPT-image特价 group. Their requests take the same format as the model of the same name without the suffix, with a few differences:

Model Differences from the model of the same name
gpt-image-2-client Outputs png only; an edit returns only 1 image whatever n says; the generation endpoint takes reference images in the image field (see Generate an image)
gpt-image-2.5-flare-client, gpt-image-2.5-sunburst-client Outputs jpeg only; returns only 1 image whatever n says; the generation endpoint takes reference images in the image field
gpt-image-2.6-flare-client, gpt-image-2.6-sunburst-client n can only be 1, and a larger value returns 400; no svg output; edits don't support mask or markup, so describe in the prompt where to change and say that the rest stays as it is; for streamed edits, see Streaming

To use a model, create your key in a group that includes it, or the request fails.

Gemini and Grok image models

  • Gemini image models (Nano Banana) can't use these two endpoints: on Gemini channels the gateway turns only the Imagen models into image generation requests. Use the native Gemini generateContent or the chat endpoint instead, see Nano Banana.
  • xAI's Grok Imagine also uses these two endpoints, but with different parameters: aspect_ratio and resolution for the aspect ratio and resolution, and a JSON body for edits. See Grok Imagine.

Request headers

Name Required Description
Authorization Yes Bearer sk-…, your API key
Content-Type Yes application/json to generate; multipart/form-data to upload files for an edit. curl and the SDKs add the boundary themselves, so don't write a value without a boundary by hand

Request body

The gateway passes the fields below to the upstream in the OpenAI Images format. Whether a field takes effect, and which values it accepts, depends on the model; the table notes how the series differ. Fields not listed here aren't guaranteed to be passed on.

Generate an image

POST /v1/images/generations, with a JSON body:

Parameter Type Required Description
model string Yes Model name, see Models
prompt string Yes A text description of the image, up to 32,000 characters. Put the style, composition and any text in the picture here; for clean Chinese text, ask for it explicitly
n integer No How many images to generate, 1 to 10, 1 by default. For several images, use n rather than sending requests in parallel. For the -client models, see the table above
size string No auto (default) or WIDTHxHEIGHT. From gpt-image-2 on you can set a custom size: width and height both multiples of 16, the long side at most 3 times the short side, the longest side at most 3840, and between 655,360 and 8,294,400 pixels in total. Common sizes are 1024x1024, 1536x1024, 1024x1536, 2048x2048, 2048x1152 and 3072x1024, and the 2.5 and 2.6 series go up to 3840x2160. With auto the model picks the size, which may not be a common one; the size in the response tells you. Use the letter x; the gateway rejects the multiplication sign ×
quality string No auto (default), low, medium or high; the 2.5 and 2.6 series also have xhigh and max. A higher level is slower and produces more output tokens. On the 2.6 series, auto renders at low
complexity string No 2.6 series only: how much planning happens before rendering, low, medium or high (high when omitted), independent of quality. low is faster for simple pictures; use high for dense text and complex layouts
background string No auto, opaque or transparent. A transparent background needs png or webp output; gpt-image-2 and the 2.5 series don't support a transparent background when generating and return an error
output_format string No png (default), jpeg or webp; the 2.6 series also has gif (animated) and svg (vector), see Animated GIFs and SVG vector images
output_compression integer No Compression level from 0 to 100, for jpeg and webp only. With png output, the 2.6 series accepts only 100
moderation string No How strict content moderation is: auto (default) or low (more lenient)
stream boolean No Set it to true for an SSE stream, see Streaming
partial_images integer No How many in-progress previews to send first when streaming, 0 to 3, 0 by default. You may get fewer than you ask for
user string No An identifier for your end user, which the upstream uses to detect abuse; a hash is recommended
image string or string[] No Only gpt-image-2-client and the 2.5 -client versions: the URL or base64 of a reference image, as an array for several

The 2.6 series doesn't support response_format or style and returns 400 if you send them. All gpt-image models return base64 only, so put style requirements in the prompt.

Edit an image

POST /v1/images/edits, with a multipart/form-data body. Besides the fields below, the text fields n, size, quality, complexity, background, output_format, output_compression, moderation, stream, partial_images, frame_count, frame_duration_ms, user and so on are the same as for generation, and the gateway passes them on as they are. Edits can't output svg.

Parameter Type Required Description
model string Yes Model name
prompt string Yes What to change. With several reference images, say what each one is for, for example "put the cup from the first image on the desk in the second"; to keep text already in the image, ask for it to stay unchanged
image file Yes The reference image, png, jpeg or webp, up to 25 MB each. For several, repeat this field or upload each one under the field name image[], up to 16 (no more than 6 is recommended). Give files the right extension (.png, .jpg, .webp), because the gateway labels the image type by extension
mask file No A mask: a PNG with an alpha channel, the same size as the first reference image. Fully transparent areas are redrawn and opaque areas are kept. On the 2.6 series, if you use a mask without background, the result may have an alpha channel; send background=opaque for an opaque image
markup file No 2.6 series only (not -client): a markup image laid over the first reference image, for example a circle around what to change. A transparent PNG the same size as the first reference image; it can't be combined with mask
input_fidelity string No How closely to keep the details of the reference images: low (default, allows big changes, good for changing the style) or high (keeps faces, products and other details as far as possible, good for changing the background or clothes). The 2.6 series doesn't support it and returns 400; say in the prompt how much of the original to keep

The gateway returns an error straight away when no image is uploaded. You can also send a JSON body with the reference images in an images array (each item is {"image_url": "an image URL or a base64 data URL"}), in the format of the official OpenAI documentation; the gateway passes it on as it is.

Animated GIFs and SVG vector images

Only the 2.6 series supports them, and the result comes back in data[].b64_json.

Animated GIFs: both generation and edits can output them. Set output_format to gif, and set the pace with frame_count (the number of frames, 2 to 16, 9 by default) and frame_duration_ms (how long each frame shows in milliseconds, at least 20, 200 by default, rounded to 10 ms). size is then the size of one frame, and all frames share the pixel budget of one request: at 1024x1024, three or more frames go over it and return 400, so use a smaller size such as 512x512, or auto. GIFs don't support stream, partial_images or output_compression, and edits can't use mask with them.

SVG vector images: only generation can output them, and the -client models don't support them. Set output_format to svg, and you must also pass an svg object naming the model that writes the SVG, for example {"model": "gpt-5", "reasoning": {"effort": "low"}} (reasoning is optional); without it you get a 400. Don't pass svg with other formats. n can only be 1, streaming isn't supported, and output_compression doesn't apply. Upstream first generates a reference image and then has that model write it as SVG, which usually takes a minute or two. b64_json decodes to UTF-8 SVG text, and data[] has no generation_id; the response also has svg_usage, the tokens used by the model that wrote the SVG.

Request examples

Generation examples

curl

curl https://codaapi.com/v1/images/generations \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-image-2.6-flare",
    "prompt": "A canal town at dawn with a stone bridge and small boats, watercolor style, the words \"Good morning\" in the top right corner",
    "size": "1536x1024",
    "quality": "medium",
    "n": 2,
    "output_format": "jpeg",
    "output_compression": 80
  }'

Python (pip install openai)

import base64
from openai import OpenAI

client = OpenAI(base_url="https://codaapi.com/v1", timeout=180)  # the key is read from the OPENAI_API_KEY environment variable

result = client.images.generate(
    model="gpt-image-2.6-flare",
    prompt='A canal town at dawn with a stone bridge and small boats, watercolor style, the words "Good morning" in the top right corner',
    size="1536x1024",
    quality="medium",
    n=2,
    output_format="jpeg",
    output_compression=80,
)
for i, image in enumerate(result.data):
    with open(f"morning-{i}.jpg", "wb") as f:
        f.write(base64.b64decode(image.b64_json))

Node.js (npm install openai)

import fs from 'node:fs';
import OpenAI from 'openai';

const client = new OpenAI({ baseURL: 'https://codaapi.com/v1', timeout: 180_000 }); // the key is read from the OPENAI_API_KEY environment variable

const result = await client.images.generate({
  model: 'gpt-image-2.6-flare',
  prompt: 'A canal town at dawn with a stone bridge and small boats, watercolor style, the words "Good morning" in the top right corner',
  size: '1536x1024',
  quality: 'medium',
  n: 2,
  output_format: 'jpeg',
  output_compression: 80,
});
result.data.forEach((image, i) => {
  fs.writeFileSync(`morning-${i}.jpg`, Buffer.from(image.b64_json, 'base64'));
});

A transparent background (2.6 series): set background to transparent and output_format to png or webp:

curl https://codaapi.com/v1/images/generations \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-image-2.6-flare",
    "prompt": "A paper plane icon in a flat style",
    "size": "1024x1024",
    "quality": "low",
    "background": "transparent",
    "output_format": "png"
  }'

A reference image when generating with a -client model (gpt-image-2-client):

curl https://codaapi.com/v1/images/generations \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-image-2-client",
    "prompt": "Draw the person in this photo, in the same pose, as a watercolor illustration",
    "size": "1024x1536",
    "quality": "medium",
    "image": ["https://example.com/pose.jpg"]
  }'

Animated GIF and SVG examples

An animated GIF:

curl https://codaapi.com/v1/images/generations \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-image-2.6-flare",
    "prompt": "A desk lamp slowly turning on and off, looping animation",
    "size": "512x512",
    "quality": "low",
    "output_format": "gif",
    "frame_count": 4,
    "frame_duration_ms": 300
  }'

An SVG vector image, decoded and saved as an .svg file:

curl https://codaapi.com/v1/images/generations   -H "Authorization: Bearer $OPENAI_API_KEY"   -H "Content-Type: application/json"   -d '{
    "model": "gpt-image-2.6-flare",
    "prompt": "A minimalist green leaf icon in flat colours",
    "size": "1024x1024",
    "output_format": "svg",
    "svg": {"model": "gpt-5", "reasoning": {"effort": "low"}}
  }'
import base64
from openai import OpenAI

client = OpenAI(base_url="https://codaapi.com/v1", timeout=300)  # the key is read from OPENAI_API_KEY

result = client.images.generate(
    model="gpt-image-2.6-flare",
    prompt="A minimalist green leaf icon in flat colours",
    size="1024x1024",
    output_format="svg",
    extra_body={"svg": {"model": "gpt-5", "reasoning": {"effort": "low"}}},
)
with open("leaf.svg", "wb") as f:
    f.write(base64.b64decode(result.data[0].b64_json))

Edit examples

curl (combine two reference images into one)

curl https://codaapi.com/v1/images/edits \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -F model="gpt-image-2" \
  -F "image[][email protected]" \
  -F "image[][email protected]" \
  -F prompt="Put the cup from the first image on the desk in the second, matching the desk's lighting" \
  -F size="1536x1024" \
  -F quality="medium"

Change only part of an image with a mask (transparent areas are redrawn), keeping the original details as far as possible:

curl https://codaapi.com/v1/images/edits \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -F model="gpt-image-2" \
  -F image="@photo.png" \
  -F mask="@mask.png" \
  -F input_fidelity="high" \
  -F prompt="Change the background to a sunset at the seaside and keep the person unchanged"

Python

result = client.images.edit(
    model="gpt-image-2",
    image=[open("cup.png", "rb"), open("desk.png", "rb")],  # one reference image can be a single file
    prompt="Put the cup from the first image on the desk in the second, matching the desk's lighting",
    size="1536x1024",
    quality="medium",
)
with open("edited.png", "wb") as f:
    f.write(base64.b64decode(result.data[0].b64_json))

For a mask, add mask=open("mask.png", "rb").

Node.js

const edited = await client.images.edit({
  model: 'gpt-image-2',
  image: [fs.createReadStream('cup.png'), fs.createReadStream('desk.png')],
  prompt: "Put the cup from the first image on the desk in the second, matching the desk's lighting",
  size: '1536x1024',
  quality: 'medium',
});
fs.writeFileSync('edited.png', Buffer.from(edited.data[0].b64_json, 'base64'));

Response

The gateway returns the upstream response as it is. The gpt-image models return base64-encoded images only, not image URLs:

{
  "created": 1764547200,
  "background": "opaque",
  "data": [
    { "b64_json": "iVBORw0KGgoAAAANSUhEUgAA…", "generation_id": "7d1c0f52-…" }
  ],
  "output_format": "png",
  "quality": "low",
  "size": "1024x1024",
  "usage": {
    "input_tokens": 21,
    "input_tokens_details": { "image_tokens": 0, "text_tokens": 21 },
    "output_tokens": 824,
    "output_tokens_details": { "image_tokens": 824, "text_tokens": 0 },
    "total_tokens": 845
  }
}
Field Description
created When the image was generated (Unix timestamp, seconds)
data[].b64_json The base64-encoded image, in the format set by output_format. With n above 1, data has several items
data[].generation_id The ID of a single image, returned by the 2.6 series
usage The token counts reported by the upstream. Token-based models are charged by these. For edits, the reference images count as input tokens
background, output_format, quality, size The values actually used; for example, with quality set to auto, this shows the level actually used
svg_usage Only with SVG output: the tokens used by the model that wrote the SVG

The usage returned by the -client models is for reference only; they're billed per request, not by it.

Streaming

Add "stream": true and the response comes back in pieces as SSE (text/event-stream). The gateway passes on each event as it is: for generation, image_generation.partial_image (a preview, with partial_image_index) and image_generation.completed (the final image, with usage); for edits, image_edit.partial_image and image_edit.completed. The b64_json of each event is one image. The completed event means the end; there's no data: [DONE] at the end of the stream.

A few differences: when the image renders quickly you may get fewer previews than partial_images asks for, or none at all; according to OpenAI, each preview adds about 100 output tokens. Edits with gpt-image-2.6-flare-client and gpt-image-2.6-sunburst-client return no previews: they send a single image_generation.completed event and then end with data: [DONE], so don't send partial_images to them.

stream = client.images.generate(
    model="gpt-image-2.6-flare",
    prompt="An isometric city block at dusk, highly detailed",
    size="1024x1024",
    quality="medium",
    stream=True,
    partial_images=2,
)
for event in stream:
    if event.type == "image_generation.partial_image":
        print("got preview", event.partial_image_index)
    elif event.type == "image_generation.completed":
        with open("city.png", "wb") as f:
            f.write(base64.b64decode(event.b64_json))

Errors

Errors come back as an OpenAI-style body, {"error": {"message": …, "type": …, "code": …}}, where error.message holds the reason and the ID of the request.

  • The gateway rejects these by itself: a missing model, a multiplication sign in size, and an edit without an uploaded image.
  • Common 400s from the upstream: a size that breaks the rules (for example not a multiple of 16, or an aspect ratio beyond 3:1), a transparent background with jpeg, mask and markup together, response_format sent to the 2.6 series, and a GIF whose frames times size go over the pixel budget.
  • moderation_blocked: content moderation stopped the prompt or a reference image. Names of living artists, copyrighted characters, brand logos and portraits of celebrities tend to trigger it, so describe instead of naming. With n above 1, the whole request fails if any one image is blocked. After a block, change the prompt before trying again rather than retrying automatically.

For what each status code means and what to do, see Error codes.

Billing

Token-based models are charged by the tokens in the response's usage: quality affects the cost most, and a larger size or higher level means more output tokens; for edits the reference images count as input tokens, so more and larger images mean more input. Per-request models (the -client ones) are charged the per-request price times the number of images returned: the charge is reserved by n when the request starts and settled by the number actually returned. For the rules, see Pricing and multipliers; for prices, see Model Square and the price list.

View records

The model, size, quality, number of images and charge of each image request are in the console under General → Usage Logs (open it directly). Like the chat endpoints, the synchronous image endpoints don't store request or response content (see Data and privacy).

To generate images in a desktop client, or to learn how to pick a model, see the visual workshop.

Updated