Generate images
Endpoint
POST
/v1/images/generationsOpenAI Images API compatible, for gpt-image-2. Generation takes a while (tens of seconds in practice); set the client timeout to 300 seconds.
Request
| Header | Value |
|---|---|
Authorization |
Bearer sk-moyiapi-xxxxxx |
content-type |
application/json |
Minimal request body:
{
"model": "gpt-image-2",
"prompt": "An orange cat typing on a keyboard, illustration style"
}
Parameters
| Field | Type | Required | Notes |
|---|---|---|---|
model |
string | Yes | gpt-image-2 |
prompt |
string | Yes | Description of the image |
n |
integer | No | Number of images, default 1. data holds one entry per image |
size |
string | No | Output size as WIDTHxHEIGHT, for example 1024x1024, or auto |
quality |
string | No | low, medium, high, or auto |
background |
string | No | transparent, opaque, or auto. transparent returns an image with an alpha channel and requires output_format png or webp |
output_format |
string | No | png (default), jpeg, or webp |
output_compression |
integer | No | 0 to 100, applies to jpeg and webp only; lower values give smaller files |
moderation |
string | No | Content moderation level, low or auto |
stream |
boolean | No | Set to true for SSE streaming; format below |
partial_images |
integer | No | 0 to 3. In the current implementation the intermediate chunks carry no image; only the final chunk carries the result |
user |
string | No | Your own end-user identifier, passed through unchanged |
Other fields pass through in the OpenAI Images API format.
Unrecognized fields in the request body are ignored rather than rejected.
Response
{
"created": 1752345600,
"data": [
{"b64_json": "iVBORw0KGgo..."}
],
"usage": {
"input_tokens": 10,
"output_tokens": 272,
"total_tokens": 282,
"input_tokens_details": {"text_tokens": 10, "image_tokens": 0, "cached_tokens": 0},
"output_tokens_details": {"text_tokens": 0, "image_tokens": 272, "reasoning_tokens": 0}
}
}
| Field | Notes |
|---|---|
created |
Creation time (Unix seconds) |
data[].b64_json |
Base64-encoded image in the format set by output_format |
usage |
Usage for this request; images are counted in output_tokens_details.image_tokens |
With stream set to true, the response is text/event-stream and each data: line is a JSON object: progress chunks have object image.generation.chunk with an empty data; the final chunk has object image.generation.result with the image in data[0].b64_json, followed by data: [DONE].
Examples
Basic request
curl https://api.moyiapi.com/v1/images/generations \
-H "Authorization: Bearer sk-moyiapi-xxxxxx" \
-H "content-type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "An orange cat typing on a keyboard, illustration style"
}'
Transparent background
curl https://api.moyiapi.com/v1/images/generations \
-H "Authorization: Bearer sk-moyiapi-xxxxxx" \
-H "content-type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "A red circular icon on a transparent background",
"size": "1024x1024",
"quality": "low",
"background": "transparent",
"output_format": "png"
}'
Compressed JPEG output
curl https://api.moyiapi.com/v1/images/generations \
-H "Authorization: Bearer sk-moyiapi-xxxxxx" \
-H "content-type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "A meadow under a blue sky",
"output_format": "jpeg",
"output_compression": 50
}'
Errors
| HTTP status | error.type |
When it happens |
|---|---|---|
| 401 | missing_auth_credential / invalid_api_key / invalid_bearer_token |
Authentication failed; see Authentication |
| 400 | insufficient_balance |
The account is out of credit; details.recharge_url is the top-up link |