Skip to main content
POST
Text-to-Image: generate image from text prompt
The interactive Playground on the right supports live testing. Fill in your API Key in Authorization (format: Bearer sk-xxx), enter a prompt, choose size / quality, and send.
Use case: This page is for “text-to-image”. Just enter a prompt — no image upload needed. For reference image editing, multi-image fusion, or mask inpainting, use the Image Edit endpoint.
🖥️ Browser Playground limitation (important)This endpoint returns a raw base64 string (typically several MB) in the response. Due to browser rendering limits, the Playground on the right may show 请求时发生错误: unable to complete request after the response arrives — the request actually succeeded; the browser just can’t render such a long base64 string.Recommended workflow (beginner-friendly):
  • Copy the Python / Node.js / cURL sample below and run it locally. The code automatically base64.b64decodes the response and writes the image to a file.
  • If you must use the in-browser Playground, set size to the smallest tier (e.g. 1024x1024) and quality to low to shrink the response.
All image APIs are synchronous — there is no task ID to poll, and if your client disconnects the result is lost while the request is still billed. Set a generous timeout for this model; see Image API Essentials & Best Practices.
⚠️ Unsupported parameters
  • input_fidelitygpt-image-2 forces high-fidelity; passing this returns 400. When migrating from 1.5, just remove the line.
  • background: "transparent" — Transparent background is not supported. Use opaque or post-process for transparency.
Outputs above 2560×1440 remain experimental. For production, prefer presets: 2048x1152 / 2048x2048 / 3840x2160.

Code Examples

Python (OpenAI SDK)

Python (Raw requests)

cURL

Node.js (Native fetch)

Browser JavaScript (Direct render)

Parameter Reference

Do not pass the legacy DALL·E values standard / hd for quality. Only the four official enum values low / medium / high / auto are accepted. The legacy values behave inconsistently across backend channels: sometimes they fail immediately with a 400 (invalid_value), and sometimes they are silently ignored and the request runs at auto (unpredictable cost). Always pass one of the four official values explicitly.
Detailed constraints, allowed values, and examples are visible in the Playground on the right — all enum fields support dropdown selection.

Response Format

⚠️ b64_json is raw base64, without the data:image/...;base64, prefix. Client must:
  • Write file: base64.b64decode(b64_str) → write to disk
  • Browser render: prepend data:image/png;base64, manually
As of July 2026, gpt-image-2-all / gpt-image-2-vip also return raw base64, but their earlier versions included the prefix — when sharing code across models, always check startsWith('data:') first.
The usage field reflects actual billed tokens for this call. input_tokens_details / output_tokens_details break text and image tokens out separately (image_tokens is always 0 for plain text-to-image). For the full field reference and a self-service cost formula, see How to check the real token count for each call on the overview page.

Authorizations

Authorization
string
header
required

API Key obtained from APIYI Console

Body

application/json
model
enum<string>
default:gpt-image-2
required

Model name, fixed as gpt-image-2

Available options:
gpt-image-2
prompt
string
required

Prompt text. Supports both Chinese and English. Place scene description at the front for better adherence.

Example:

"Cyberpunk city at night, neon sign closeup, cinematic frame"

size
string
default:auto

Output size. Presets: 1024x1024 / 1536x1024 / 1024x1536 / 2048x2048 / 2048x1152 / 3840x2160 / 2160x3840. Also accepts any valid custom size (max edge ≤ 3840, both multiples of 16, ratio ≤ 3:1, total pixels 0.65–8.3MP).

Example:

"2048x1152"

quality
enum<string>
default:auto

Quality tier. low (sketches/batch), medium (daily), high (final/fine text), auto (default)

Available options:
auto,
low,
medium,
high
output_format
enum<string>
default:png

Output format

Available options:
png,
jpeg,
webp
output_compression
integer

Output compression (0–100), only effective for jpeg/webp

Required range: 0 <= x <= 100
Example:

85

background
enum<string>
default:auto

Background mode. auto (default) or opaque. Not supported: transparent

Available options:
auto,
opaque
moderation
enum<string>
default:auto

Moderation strength. auto (default) or low

Available options:
auto,
low
n
enum<integer>
default:1

Number of images. This model only supports 1

Available options:
1

Response

Image generated successfully

created
integer

Unix timestamp

Example:

1776832476

data
object[]

Generation results (this model returns 1 image per call)

usage
object

Token usage for this call (used for token-based billing)