文生图:根据文本描述生成图片
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": "横版 16:9 电影画幅,黄昏时的海边老灯塔"
}
'import requests
url = "https://api.apiyi.com/v1/images/generations"
payload = {
"model": "gpt-image-2-all",
"prompt": "横版 16:9 电影画幅,黄昏时的海边老灯塔"
}
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: '横版 16:9 电影画幅,黄昏时的海边老灯塔'})
};
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' => '横版 16:9 电影画幅,黄昏时的海边老灯塔'
]),
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\": \"横版 16:9 电影画幅,黄昏时的海边老灯塔\"\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\": \"横版 16:9 电影画幅,黄昏时的海边老灯塔\"\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\": \"横版 16:9 电影画幅,黄昏时的海边老灯塔\"\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 參考與線上除錯 — 輸入文本描述生成圖片,$0.03/張
POST
/
v1
/
images
/
generations
文生图:根据文本描述生成图片
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": "横版 16:9 电影画幅,黄昏时的海边老灯塔"
}
'import requests
url = "https://api.apiyi.com/v1/images/generations"
payload = {
"model": "gpt-image-2-all",
"prompt": "横版 16:9 电影画幅,黄昏时的海边老灯塔"
}
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: '横版 16:9 电影画幅,黄昏时的海边老灯塔'})
};
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' => '横版 16:9 电影画幅,黄昏时的海边老灯塔'
]),
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\": \"横版 16:9 电影画幅,黄昏时的海边老灯塔\"\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\": \"横版 16:9 电影画幅,黄昏时的海边老灯塔\"\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\": \"横版 16:9 电影画幅,黄昏时的海边老灯塔\"\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 後一鍵傳送即可。場景說明:本頁用於「文本生成圖片」。只需輸入提示詞即可,無需上傳任何圖片。如需根據現有圖片做編輯或融合,請使用 圖片編輯介面。
🖥️ 瀏覽器 Playground 限制(預設 b64_json 模式)本端點預設
response_format: "b64_json",響應會包含數 MB 的 base64 字串,瀏覽器 Playground 可能彈出 請求時發生錯誤: unable to complete request ——實際請求已經成功,只是瀏覽器無法顯示這麼長的 base64。推薦做法:- 只想在 Playground 裡看圖:顯式傳
"response_format": "url",響應是單條 R2 連結,瀏覽器渲染正常。 - 想要 base64:複製下方”程式碼示例”到本地執行,程式碼會自動解碼並把圖片儲存為本地檔案。
圖片 API 全部為同步呼叫:沒有非同步任務 ID,客戶端斷開連線結果即丟失、但請求仍會計費。請為本模型設定足夠大的 timeout,詳見 圖片 API 呼叫須知與最佳實踐。
⚠️ 引數支援情況
size:欄位不生效——傳auto或具體尺寸都不會報錯,但會被服務端靜默忽略。最終尺寸完全由 prompt 決定:- prompt 寫了尺寸/比例(如”橫版 16:9”)→ 模型會遵循 prompt
- prompt 沒寫尺寸 → 同一 prompt 多次呼叫會抽卡式出現多樣化尺寸,適合看多種構圖變體
- 需要嚴格鎖尺寸請改用
gpt-image-2-vip(支援auto+ 30 檔鎖定)
n/quality/aspect_ratio:❌ 不接受,傳入可能觸發引數校驗錯誤。
prompt,例如:橫版 16:9 電影畫幅,黃昏時的海邊老燈塔豎版 9:16 手機桌布,賽博朋克城市雨夜1024×1024 方形 LOGO,極簡貓咪線條
程式碼示例
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": "橫版 16:9 日落海邊的老燈塔,電影畫幅,寫實風格",
"response_format": "url"
},
timeout=300 # 保守值,吸收長尾 + 圖片下載耗時
).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 方形 LOGO,極簡貓咪線條",
"response_format": "b64_json"
},
timeout=300 # 保守值,吸收長尾 + 圖片下載耗時
).json()
# 實測(2026-07)b64_json 為純 base64(無 data: 字首);歷史版本曾含字首,做個檢測最穩
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": "橫版 16:9 電影畫幅,黃昏時的海邊老燈塔",
"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 方形 LOGO,極簡貓咪線條",
response_format: "b64_json"
})
}
);
const data = await response.json();
// 實測為純 base64(無 data: 字首),渲染前需拼接;歷史版本曾自帶字首,做個檢測最穩
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: '豎版 9:16 手機桌布,賽博朋克城市雨夜',
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 | 是 | 固定填 gpt-image-2-all |
prompt | string | 是 | 提示詞,尺寸/比例/風格請寫在此處(唯一控制尺寸的方式) |
response_format | string | 否 | b64_json(預設,純 base64)或 url(返回 R2 CDN 連結);建議始終顯式傳值 |
本模型不支援
size 入參——即便傳入也會被靜默忽略,不會報錯;如需用 size 欄位鎖定 30 檔尺寸,請改用 gpt-image-2-vip。詳細的引數約束和可選值請檢視右側 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-07 實測
b64_json 欄位為純 base64(不含 data: 字首),需解碼寫檔案或自行拼接字首後渲染;歷史版本曾直接帶字首。請在程式碼裡做 startsWith('data:') 檢測後再處理,相容兩種形態。授權
在 API易控制台获取的 API Key
主體
application/json
這個頁面有幫助嗎?
⌘I