Skip to main content
POST
/
v1
/
chat
/
completions
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-abc123",
  "object": "chat.completion",
  "created": 1702855400,
  "model": "gpt-image-2-all",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "![image](https://r2cdn.copilotbase.com/r2cdn2/xxxxx.png)"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 25,
    "completion_tokens": 150,
    "total_tokens": 175
  }
}
Recommended endpoint: Compared to /v1/images/generations and /v1/images/edits, the chat endpoint offers better prompt adherence and supports both text-to-image and image editing from the same endpoint, with native multi-turn iteration. Enter your API Key in the right-side Playground and pick an example from the dropdown (text-to-image / edit-with-reference / multi-turn).
Pick your mode:
  • Text-only messagestext-to-image
  • Add image_url (URL or base64 data URL) to messagesedit with reference image
  • Keep assistant historical messages → multi-turn iterative editing

Code Examples

Python (text-to-image)

import requests

API_KEY = "sk-your-api-key"

response = 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": "Landscape 16:9 cinematic, old lighthouse at sunset, photorealistic"}
        ]
    },
    timeout=120
).json()

print(response["choices"][0]["message"]["content"])

Python (edit with reference image)

import requests
import base64

API_KEY = "sk-your-api-key"

# Use 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()

response = 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 image into a watercolor painting"},
                    {"type": "image_url", "image_url": {"url": data_url}}
                ]
            }
        ]
    },
    timeout=120
).json()

print(response["choices"][0]["message"]["content"])

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": "Landscape 16:9, cyberpunk rainy night street, neon sign reading Hello World"}
    ]
  }'

cURL (edit with reference)

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 image 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 response = 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 response.json();
console.log(data.choices[0].message.content);

Node.js (edit with reference)

import fs from "node:fs";

const API_KEY = "sk-your-api-key";
const imgB64 = fs.readFileSync("./photo.png").toString("base64");
const dataUrl = `data:image/png;base64,${imgB64}`;

const response = 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: [
          { type: "text", text: "Turn this image into a watercolor painting" },
          { type: "image_url", image_url: { url: dataUrl } }
        ]
      }
    ]
  })
});

const data = await response.json();
console.log(data.choices[0].message.content);
from openai import OpenAI

client = OpenAI(
    api_key="sk-your-api-key",
    base_url="https://api.apiyi.com/v1"
)

resp = client.chat.completions.create(
    model="gpt-image-2-all",
    messages=[{
        "role": "user",
        "content": "Generate a landscape 16:9 Chinese ink-wash painting of mountains and rivers"
    }]
)
print(resp.choices[0].message.content)

Parameters Quick Reference

ParameterTypeRequiredDescription
modelstringYesFixed: gpt-image-2-all
messagesarrayYesConversation messages; supports system / user / assistant roles
messages[].contentstring | arrayYesPlain text (text-to-image) or multimodal array (edit with reference)
streambooleanNoStreaming. This model returns one-shot — keep false
Multimodal content part (when content is an array):
FieldTypeRequiredDescription
typeenumYestext or image_url
textstringConditionalRequired when type=text
image_url.urlstringConditionalRequired when type=image_url. Accepts https://... or data:image/png;base64,...
Detailed field constraints and allowed values are shown in the right-side Playground. The “Example” dropdown switches between text-to-image / edit-with-reference / multi-turn.

Response Format

The chat endpoint returns a standard OpenAI chat.completion response. The generated image appears as a URL or data URL embedded in choices[0].message.content:
{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1702855400,
  "model": "gpt-image-2-all",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "![image](https://r2cdn.copilotbase.com/r2cdn2/xxxxx.png)"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 25,
    "completion_tokens": 150,
    "total_tokens": 175
  }
}
Parsing tip: Extract image links from content with a regex like https?://[^\s)]+\.(png|jpg|jpeg|webp) or data:image/[^\s)]+.

Better prompt adherence

The chat endpoint follows complex instructions, size hints, and typographic requests more reliably

One endpoint, two modes

No need to switch between generations / edits — a single endpoint handles both

Native multi-turn

Keep assistant history and continue refining — same mental model as ChatGPT

Complete SDK ecosystem

Works out-of-the-box with the OpenAI SDK, LangChain, and any Chat-style frontend

Model Overview

Capabilities, pricing, best practices

Text-to-Image API (/v1/images/generations)

OpenAI Images API compatible endpoint

Image Editing API (/v1/images/edits)

Upload reference images via multipart/form-data

Online Playground

Test on imagen.apiyi.com

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
messages
object[]
required

Conversation messages. Supports multi-turn and multimodal content.

stream
boolean
default:false

Whether to stream the response. This model returns one-shot — keep false. Playground does not support streaming preview.

temperature
number
default:1

Sampling temperature (minor effect on image generation — default is fine)

Required range: 0 <= x <= 2

Response

Image generated (image URL or data URL appears in choices[0].message.content)

id
string
Example:

"chatcmpl-abc123"

object
string
Example:

"chat.completion"

created
integer

Unix timestamp (seconds)

Example:

1702855400

model
string
Example:

"gpt-image-2-all"

choices
object[]
usage
object