Skip to main content
POST
/
v1
/
images
/
generations
Text-to-Image: generate an image from a text prompt
curl --request POST \
  --url https://api.apiyi.com/v1/images/generations \
  --header 'Authorization: Bearer <token>' \
  --header 'Content-Type: application/json' \
  --data '
{
  "model": "gpt-image-2-all",
  "prompt": "Landscape 16:9 cinematic, old lighthouse at sunset"
}
'
{
  "data": [
    {
      "b64_json": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."
    }
  ],
  "created": 1778037127,
  "usage": {
    "input_tokens": 98,
    "output_tokens": 1185,
    "total_tokens": 1283
  }
}
The interactive Playground on the right supports direct online testing. Enter your API Key in the Authorization field (format: Bearer sk-xxx), type a prompt, and click send.
Scope: This page is for text-to-image generation. Just enter a prompt — no image upload required. To edit or fuse existing images, use the Image Editing endpoint.
🖥️ Browser Playground limitation (default b64_json mode)This endpoint defaults to response_format: "b64_json", so the response carries a multi-MB base64 string and the browser Playground may show 请求时发生错误: unable to complete requestthe request actually succeeded; the browser just can’t render such a long base64 string.Recommended workflow:
  • Just want to view the image in the Playground? Pass "response_format": "url" explicitly — the response is a single R2 link and renders fine.
  • Want base64? Copy the code sample below and run it locally — the code will decode and save the image to a file automatically.
⚠️ Parameter Support
  • size: the field has no effect — sending auto or any concrete value does not error, but the value is silently ignored by the server. Dimensions are driven entirely by the prompt:
    • Prompt mentions a size/ratio (e.g., “Landscape 16:9”) → the model follows the prompt
    • Prompt has no size hints → the same prompt yields varied dimensions across calls, like “drawing different cards” — useful for exploring multiple compositions
    • For strict size locking, use gpt-image-2-vip (supports auto + 30 explicit sizes)
  • n / quality / aspect_ratio: ❌ rejected. Sending these may trigger parameter validation errors.
Write size and ratio directly into the prompt, e.g.:
  • Landscape 16:9 cinematic, old lighthouse by the sea at dusk
  • Portrait 9:16 phone wallpaper, cyberpunk city rainy night
  • 1024×1024 square logo, minimalist cat line art
Put size descriptions at the front of the prompt for better adherence.

Code Examples

Python

import requests

API_KEY = "sk-your-api-key"

response = requests.post(
    "https://api.apiyi.com/v1/images/generations",
    headers={"Authorization": f"Bearer {API_KEY}"},
    json={
        "model": "gpt-image-2-all",
        "prompt": "Landscape 16:9 cinematic, old lighthouse at sunset, photorealistic",
        "response_format": "url"
    },
    timeout=300  # conservative — absorbs tail latency + image download time
).json()

image_url = response["data"][0]["url"]
print(image_url)
b64_json mode (get a ready-to-render data URL):
import requests

response = requests.post(
    "https://api.apiyi.com/v1/images/generations",
    headers={"Authorization": "Bearer sk-your-api-key"},
    json={
        "model": "gpt-image-2-all",
        "prompt": "1024x1024 square logo, minimalist cat line art",
        "response_format": "b64_json"
    },
    timeout=300  # conservative — absorbs tail latency + image download time
).json()

# Note: b64_json already includes the "data:image/png;base64," prefix — do not re-prepend
data_url = response["data"][0]["b64_json"]

cURL

curl -X POST "https://api.apiyi.com/v1/images/generations" \
  -H "Authorization: Bearer sk-your-api-key" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-image-2-all",
    "prompt": "Landscape 16:9 cinematic, old lighthouse at sunset",
    "response_format": "url"
  }'

Node.js

const API_KEY = "sk-your-api-key";

const response = await fetch(
  "https://api.apiyi.com/v1/images/generations",
  {
    method: "POST",
    headers: {
      "Authorization": `Bearer ${API_KEY}`,
      "Content-Type": "application/json"
    },
    body: JSON.stringify({
      model: "gpt-image-2-all",
      prompt: "1024x1024 square logo, minimalist cat line art",
      response_format: "b64_json"
    })
  }
);

const data = await response.json();
// b64_json already includes the prefix — can be used directly as <img src>
document.getElementById("result").src = data.data[0].b64_json;

Browser JavaScript (Fetch)

const resp = await fetch('https://api.apiyi.com/v1/images/generations', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer sk-your-api-key'
    },
    body: JSON.stringify({
        model: 'gpt-image-2-all',
        prompt: 'Portrait 9:16 phone wallpaper, cyberpunk city rainy night',
        response_format: 'b64_json'
    })
});
const { data } = await resp.json();
document.getElementById('result').src = data[0].b64_json;

Parameters Quick Reference

ParameterTypeRequiredDescription
modelstringYesFixed value: gpt-image-2-all
promptstringYesPrompt. Include size/ratio/style here
sizestringNoOnly auto is accepted (the same prompt yields varied dimensions). For strict size locking, use gpt-image-2-vip
response_formatstringNob64_json (default, returns base64 data URL) or url (returns R2 CDN link)
Detailed parameter constraints and allowed values are shown in the right-hand Playground. The response_format field supports dropdown selection.

Response Format

data[0] returns either url or b64_json — never both (depends on response_format). This endpoint defaults to b64_json. b64_json mode (default):
{
  "data": [
    {
      "b64_json": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."
    }
  ],
  "created": 1778037127,
  "usage": {
    "input_tokens": 98,
    "output_tokens": 1185,
    "total_tokens": 1283
  }
}
url mode (requires explicit "response_format": "url", R2 CDN globally accelerated):
{
  "data": [
    {
      "url": "https://r2cdn.copilotbase.com/r2cdn2/0e82148a-bec0-4b42-bbca-117c6b42581b.png"
    }
  ],
  "created": 1778037331,
  "usage": {
    "input_tokens": 30,
    "output_tokens": 2074,
    "total_tokens": 2104
  }
}
Compatibility note: The b64_json field already contains the data:image/png;base64, prefix. You can use it directly as <img src> or write it to a file. Do not manually prepend data:image/...;base64,. This differs from some legacy OpenAI-compatible models.

Authorizations

Authorization
string
header
required

API Key from the API易 Console

Body

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

Model name, fixed to gpt-image-2-all

Available options:
gpt-image-2-all
prompt
string
required

Prompt. Include size/ratio/style here, e.g., Landscape 16:9 cinematic, old lighthouse at sunset

Example:

"Landscape 16:9 cinematic, old lighthouse at sunset"

response_format
enum<string>
default:b64_json

Response format. b64_json returns a base64 string already prefixed with a data URL header (default); url returns an R2 CDN link

Available options:
b64_json,
url

Response

Image successfully generated. Defaults to base64 in data[0].b64_jsonurl is not returned in the same response.

Image generation response. data[0] returns either url or b64_json, never both (depends on response_format; this endpoint defaults to b64_json).

data
object[]

Result array (this model returns 1 image per call)

created
integer

Unix timestamp (seconds)

usage
object

Token usage statistics