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"
}
'import requests
url = "https://api.apiyi.com/v1/images/generations"
payload = {
"model": "gpt-image-2-all",
"prompt": "Landscape 16:9 cinematic, old lighthouse at sunset"
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
model: 'gpt-image-2-all',
prompt: 'Landscape 16:9 cinematic, old lighthouse at sunset'
})
};
fetch('https://api.apiyi.com/v1/images/generations', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.apiyi.com/v1/images/generations",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'model' => 'gpt-image-2-all',
'prompt' => 'Landscape 16:9 cinematic, old lighthouse at sunset'
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.apiyi.com/v1/images/generations"
payload := strings.NewReader("{\n \"model\": \"gpt-image-2-all\",\n \"prompt\": \"Landscape 16:9 cinematic, old lighthouse at sunset\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.apiyi.com/v1/images/generations")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"model\": \"gpt-image-2-all\",\n \"prompt\": \"Landscape 16:9 cinematic, old lighthouse at sunset\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.apiyi.com/v1/images/generations")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"model\": \"gpt-image-2-all\",\n \"prompt\": \"Landscape 16:9 cinematic, old lighthouse at sunset\"\n}"
response = http.request(request)
puts response.read_body{
"data": [
{
"b64_json": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."
}
],
"created": 1778037127,
"usage": {
"input_tokens": 98,
"output_tokens": 1185,
"total_tokens": 1283
}
}GPT-Image-2-All画像生成
テキストから画像への API リファレンス
gpt-image-2-all テキストから画像への API リファレンスとインタラクティブなプレイグラウンド — text prompt から画像を生成、$0.03/画像
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"
}
'import requests
url = "https://api.apiyi.com/v1/images/generations"
payload = {
"model": "gpt-image-2-all",
"prompt": "Landscape 16:9 cinematic, old lighthouse at sunset"
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
model: 'gpt-image-2-all',
prompt: 'Landscape 16:9 cinematic, old lighthouse at sunset'
})
};
fetch('https://api.apiyi.com/v1/images/generations', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.apiyi.com/v1/images/generations",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'model' => 'gpt-image-2-all',
'prompt' => 'Landscape 16:9 cinematic, old lighthouse at sunset'
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.apiyi.com/v1/images/generations"
payload := strings.NewReader("{\n \"model\": \"gpt-image-2-all\",\n \"prompt\": \"Landscape 16:9 cinematic, old lighthouse at sunset\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.apiyi.com/v1/images/generations")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"model\": \"gpt-image-2-all\",\n \"prompt\": \"Landscape 16:9 cinematic, old lighthouse at sunset\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.apiyi.com/v1/images/generations")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"model\": \"gpt-image-2-all\",\n \"prompt\": \"Landscape 16:9 cinematic, old lighthouse at sunset\"\n}"
response = http.request(request)
puts response.read_body{
"data": [
{
"b64_json": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."
}
],
"created": 1778037127,
"usage": {
"input_tokens": 98,
"output_tokens": 1185,
"total_tokens": 1283
}
}右側のインタラクティブな Playground では、直接オンラインでテストできます。Authorization フィールドに API Key を入力し(形式:
Bearer sk-xxx)、prompt を入力して送信してください。対象: このページは text-to-image generation 用です。prompt を入力するだけで、画像のアップロードは不要です。既存の画像を編集または融合するには、画像編集 endpoint を使用してください。
🖥️ ブラウザ Playground の制限(デフォルトの b64_json モード)この endpoint は
response_format: "b64_json" がデフォルト なので、レスポンスには数MB規模の base64 文字列が含まれ、ブラウザ Playground では 请求时发生错误: unable to complete request と表示されることがあります — リクエスト自体は実際には成功しています; ブラウザはそのような長い base64 文字列をレンダリングできないだけです。推奨ワークフロー:- Playground で画像を表示したいだけですか?
"response_format": "url"を明示的に指定してください — レスポンスは 1 本の R2 リンクになり、問題なく表示されます。 - base64 が欲しいですか? 下のコードサンプルをコピーしてローカルで実行してください — コードが自動でデコードし、画像をファイルに保存します。
すべての image API は 同期型 です — ポーリングする task ID はなく、クライアントが切断されると、リクエストがまだ課金対象のままでも結果は失われます。この model では十分に長い timeout を設定してください。詳細は Image API Essentials & Best Practices を参照してください。
⚠️ パラメータ対応
size: このフィールドは効果がありません —autoや任意の具体的な値を送ってもエラーにはならず、値はサーバー側で 黙って無視 されます。サイズは完全に prompt によって決まります:- prompt にサイズ/比率の指定がある場合(例: “Landscape 16:9”)→ model は prompt に従います
- prompt にサイズのヒントがない場合 → 同じ prompt でも呼び出しごとに 異なるサイズ が返り、“drawing different cards” のようになります — 複数の構図を試すのに便利です
- サイズを厳密に固定したい場合は、
gpt-image-2-vipを使用してください(auto+ 30 の明示サイズに対応)
n/quality/aspect_ratio: ❌ 拒否されます。これらを送信すると、パラメータ検証エラーが発生する場合があります。
prompt に直接書いてください。例:Landscape 16:9 cinematic, old lighthouse by the sea at duskPortrait 9:16 phone wallpaper, cyberpunk city rainy night1024×1024 square logo, minimalist cat line art
コード例
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)
import base64
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()
# Verified 2026-07: b64_json is raw base64 (no data: prefix); earlier versions included the prefix — a check is safest
b64 = response["data"][0]["b64_json"]
if b64.startswith("data:"):
b64 = b64.split(",", 1)[1]
with open("output.png", "wb") as f:
f.write(base64.b64decode(b64))
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();
// Verified raw base64 (no data: prefix) — prepend before rendering; earlier versions included the prefix, so check first
let b64 = data.data[0].b64_json;
if (!b64.startsWith("data:")) b64 = `data:image/png;base64,${b64}`;
document.getElementById("result").src = b64;
ブラウザ 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();
let b64 = data[0].b64_json;
if (!b64.startsWith('data:')) b64 = `data:image/png;base64,${b64}`;
document.getElementById('result').src = b64;
パラメータ早見表
| パラメータ | 型 | 必須 | 説明 |
|---|---|---|---|
model | string | Yes | 固定値: gpt-image-2-all |
prompt | string | Yes | プロンプト。ここにサイズ/比率/スタイルを含めてください |
size | string | No | auto のみが受け付けられます(同じプロンプトでも生成される寸法は変化します)。厳密にサイズを固定したい場合はgpt-image-2-vipを使用してください |
response_format | string | No | b64_json(デフォルト、生の base64)または url(R2 CDN リンクを返します)を常に明示的に指定してください |
詳細なパラメータ制約と許可される値は、右側の Playground に表示されます。
response_format フィールドはドロップダウン選択に対応しています。レスポンス形式
data[0] は url または b64_json のいずれかを返します — 両方ではありません(response_format によります)。このエンドポイントは 既定で b64_json です。
b64_json モード(デフォルト):
{
"data": [
{
"b64_json": "iVBORw0KGgoAAAANSUhEUgAA..."
}
],
"created": 1778037127,
"usage": {
"input_tokens": 98,
"output_tokens": 1185,
"total_tokens": 1283
}
}
url モード(明示的な "response_format": "url" が必要、R2 CDN によるグローバル加速あり):
{
"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
}
}
互換性に関する注意: 2026年7月に確認済み —
b64_json フィールドは data: プレフィックスのない生の base64 です。ファイルとして書き込むにはデコードするか、描画前に自分でプレフィックスを付けてください。以前のバージョンにはプレフィックスが含まれていました ので、両方の形式に対応するため、必ず最初に startsWith('data:') チェックを実行してください。承認
API Key from the API易 Console
ボディ
application/json
Model name, fixed to gpt-image-2-all
利用可能なオプション:
gpt-image-2-all Prompt. Include size/ratio/style here, e.g., Landscape 16:9 cinematic, old lighthouse at sunset
例:
"Landscape 16:9 cinematic, old lighthouse at sunset"
Response format. b64_json returns a base64 string already prefixed with a data URL header (default); url returns an R2 CDN link
利用可能なオプション:
b64_json, url レスポンス
Image successfully generated. Defaults to base64 in data[0].b64_json — url 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).
このページは役に立ちましたか?
⌘I