curl --request POST \
--url https://api.apiyi.com/v1/chat/completions \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"model": "gpt-image-2-all",
"messages": [
{
"role": "user",
"content": "Landscape 16:9 cinematic, old lighthouse at sunset, photorealistic"
}
]
}
'{
"id": "chatcmpl-xxx",
"object": "chat.completion",
"created": 1778037331,
"model": "gpt-image-2-all",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "\n\n"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 24,
"completion_tokens": 818,
"total_tokens": 842
}
}Chat-Style API Reference
gpt-image-2-all chat-style endpoint — one endpoint for both text-to-image and reference-image editing via inline URLs; for multi-turn edits, pass the previous output as image_url in a new user message.
curl --request POST \
--url https://api.apiyi.com/v1/chat/completions \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"model": "gpt-image-2-all",
"messages": [
{
"role": "user",
"content": "Landscape 16:9 cinematic, old lighthouse at sunset, photorealistic"
}
]
}
'{
"id": "chatcmpl-xxx",
"object": "chat.completion",
"created": 1778037331,
"model": "gpt-image-2-all",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "\n\n"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 24,
"completion_tokens": 818,
"total_tokens": 842
}
}gpt-image-2. The chat-style endpoint on this page still works, and remains useful for multi-turn iterative editing or passing online image URLs directly.choices[0].message.content.If you want one codebase that works across both official-relay and reverse channels, use /v1/images/generations and /v1/images/edits (standard OpenAI Images API format).- text-only
messages→ text-to-image - add
image_url(URL or base64 data URL) to the user message → reference-image editing - to edit the previous image across turns → put the previous output’s URL into the
image_urlof a new user message (see Multi-turn editing)
image_url in the last user message as the base image; any image placed in assistant history (whether a plain-text URL or an image_url structure) is ignored. To edit the previous image you must pass it as the reference in a new user message — see Multi-turn editing.Response format
The response is standard Chat Completions format, with the generated image as Markdown insidechoices[0].message.content (an R2 CDN link by default):
{
"id": "chatcmpl-xxx",
"object": "chat.completion",
"created": 1778037331,
"model": "gpt-image-2-all",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "\n\n"
},
"finish_reason": "stop"
}
],
"usage": { "prompt_tokens": 24, "completion_tokens": 818, "total_tokens": 842 }
}
 out of choices[0].message.content with a regex. In rare cases content holds a base64 data URL (), which the same regex captures — usable directly as an <img src>.content returns a long base64 blob, the response string can reach several MB and the Playground may show unable to complete request — the request actually succeeded; the browser just can’t render that much. Copy the code below and run it locally.Code examples
choices field, so you can also use the OpenAI SDK directly (client.chat.completions.create(...)), read resp.choices[0].message.content for the Markdown, and extract the image URL. The examples below use plain requests / fetch.Python (text-to-image)
import re, requests
API_KEY = "sk-your-api-key"
resp = requests.post(
"https://api.apiyi.com/v1/chat/completions",
headers={"Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json"},
json={
"model": "gpt-image-2-all",
"messages": [
{"role": "user", "content": "16:9 cinematic, an old seaside lighthouse at dusk, photorealistic"}
],
},
timeout=300, # generous, to absorb tail latency + image upload/download
).json()
content = resp["choices"][0]["message"]["content"]
url = re.search(r'!\[[^\]]*\]\((.*?)\)', content).group(1) # pull the image URL from Markdown
print(url)
Python (reference-image editing)
import re, base64, requests
API_KEY = "sk-your-api-key"
# Either an HTTPS URL or a base64 data URL
with open("photo.png", "rb") as f:
data_url = "data:image/png;base64," + base64.b64encode(f.read()).decode()
resp = requests.post(
"https://api.apiyi.com/v1/chat/completions",
headers={"Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json"},
json={
"model": "gpt-image-2-all",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "Turn this photo into a watercolor painting"},
{"type": "image_url", "image_url": {"url": data_url}},
],
}
],
},
timeout=300,
).json()
content = resp["choices"][0]["message"]["content"]
print(re.search(r'!\[[^\]]*\]\((.*?)\)', content).group(1))
cURL (text-to-image)
curl -X POST "https://api.apiyi.com/v1/chat/completions" \
-H "Authorization: Bearer sk-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2-all",
"messages": [
{"role": "user", "content": "16:9, cyberpunk rainy night street, neon sign reading Hello World"}
]
}'
{/* the image is in choices[0].message.content, like  */}
cURL (reference-image editing)
curl -X POST "https://api.apiyi.com/v1/chat/completions" \
-H "Authorization: Bearer sk-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2-all",
"messages": [
{
"role": "user",
"content": [
{ "type": "text", "text": "Turn this photo into a watercolor painting" },
{ "type": "image_url", "image_url": { "url": "https://example.com/photo.png" } }
]
}
]
}'
Node.js (text-to-image)
const API_KEY = "sk-your-api-key";
const resp = await fetch("https://api.apiyi.com/v1/chat/completions", {
method: "POST",
headers: { "Authorization": `Bearer ${API_KEY}`, "Content-Type": "application/json" },
body: JSON.stringify({
model: "gpt-image-2-all",
messages: [{ role: "user", content: "1024x1024 square logo, minimalist cat line art" }],
}),
});
const data = await resp.json();
const content = data.choices[0].message.content;
const url = content.match(/!\[[^\]]*\]\((.*?)\)/)[1]; // extract image URL
console.log(url);
Multi-turn editing
To keep editing on top of the previous image, do not rely on conversation history (images inassistant turns are ignored). The correct way: send the previous output’s URL again as the image_url of a new user message, together with the new instruction. To keep iterating, feed the latest output back in.
import re, requests
API_KEY = "sk-your-api-key"
URL = "https://api.apiyi.com/v1/chat/completions"
H = {"Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json"}
def edit_with(image_url, instruction):
"""Use image_url as the base image, edit per instruction, return the new image URL."""
resp = requests.post(URL, headers=H, json={
"model": "gpt-image-2-all",
"messages": [{
"role": "user",
"content": [
{"type": "text", "text": instruction},
{"type": "image_url", "image_url": {"url": image_url}},
],
}],
}, timeout=300).json()
content = resp["choices"][0]["message"]["content"]
return re.search(r'!\[[^\]]*\]\((.*?)\)', content).group(1)
# Turn 1: change the sofa color based on the original image
img1 = edit_with("https://example.com/cat.png", "Make the sofa red; keep the cat and composition unchanged")
# Turn 2: feed turn 1's output back in to refine further
img2 = edit_with(img1, "Put a small yellow hat on the cat; keep everything else the same")
print(img2)
assistant history” pattern below does NOT work (the output won’t be based on the previous image) — do not use it:{"messages": [
{"role": "user", "content": "Generate an orange cat sitting on a blue sofa"},
{"role": "assistant", "content": ""},
{"role": "user", "content": "Make the sofa red"}
]}
https://.../cat.png into the image_url of a new user message (see the code above) for the edit to actually build on that image./v1/images/edits standard editing endpoint, uploading the previous output as the image field plus a new instruction — same iterative result. See Image Editing API.Parameter reference
| Parameter | Type | Required | Description |
|---|---|---|---|
model | string | Yes | Fixed to gpt-image-2-all |
messages | array | Yes | Message array; supports system / user / assistant roles (note: the base image is taken only from the last user message’s image_url) |
messages[].content | string | array | Yes | Plain text string (text-to-image) or multimodal array (editing with an image) |
stream | boolean | No | Whether to stream. This model produces the image in one shot — keep false |
content is an array):
| Field | Type | Required | Description |
|---|---|---|---|
type | enum | Yes | text or image_url |
text | string | Conditional | Required when type=text |
image_url.url | string | Conditional | Required when type=image_url. Supports https://... or data:image/png;base64,... |
Why the chat-style endpoint
Two abilities, one endpoint
Inline URLs
image_url accepts a CDN image link or base64 data URL directly — no multipart uploadStandard chat response
choices, so the OpenAI SDK and Chat frontends work directly; the image is in the Markdown of message.contentIterative editing
/v1/images/generations and /v1/images/edits (standard OpenAI Images API format) — one codebase switches channels.Related resources
Model Overview
Text-to-Image API (/v1/images/generations)
Image Editing API (/v1/images/edits)
Online generation
授权
API Key from the APIYI Console
请求体
Model name, fixed to gpt-image-2-all
gpt-image-2-all Conversation messages. The base image is taken only from the last user message's image_url.
Show child attributes
Show child attributes
Whether to stream the response. This model returns one-shot — keep false. Playground does not support streaming preview.
Sampling temperature (minor effect on image generation — default is fine)
0 <= x <= 2响应
Image generated. Standard Chat Completions format, with the image as Markdown inside choices[0].message.content.
Standard Chat Completions response. The generated image is returned as Markdown () inside choices[0].message.content — an R2 CDN link by default; in rare cases a base64 data URL.
此页面对您有帮助吗?