文生图:根据文本提示词生成图片
curl --request POST \
--url https://api.apiyi.com/v1/images/generations \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"model": "MAI-Image-2.6-Flash",
"prompt": "一家古风茶馆的门面,木质招牌上写着「API易 欢迎」,红灯笼,黄昏暖光,写实摄影"
}
'import requests
url = "https://api.apiyi.com/v1/images/generations"
payload = {
"model": "MAI-Image-2.6-Flash",
"prompt": "一家古风茶馆的门面,木质招牌上写着「API易 欢迎」,红灯笼,黄昏暖光,写实摄影"
}
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: 'MAI-Image-2.6-Flash',
prompt: '一家古风茶馆的门面,木质招牌上写着「API易 欢迎」,红灯笼,黄昏暖光,写实摄影'
})
};
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' => 'MAI-Image-2.6-Flash',
'prompt' => '一家古风茶馆的门面,木质招牌上写着「API易 欢迎」,红灯笼,黄昏暖光,写实摄影'
]),
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\": \"MAI-Image-2.6-Flash\",\n \"prompt\": \"一家古风茶馆的门面,木质招牌上写着「API易 欢迎」,红灯笼,黄昏暖光,写实摄影\"\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\": \"MAI-Image-2.6-Flash\",\n \"prompt\": \"一家古风茶馆的门面,木质招牌上写着「API易 欢迎」,红灯笼,黄昏暖光,写实摄影\"\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\": \"MAI-Image-2.6-Flash\",\n \"prompt\": \"一家古风茶馆的门面,木质招牌上写着「API易 欢迎」,红灯笼,黄昏暖光,写实摄影\"\n}"
response = http.request(request)
puts response.read_body{
"created": 1790999642,
"data": [
{
"b64_json": "<string>"
}
],
"usage": {
"prompt_tokens": 1000,
"total_tokens": 1000,
"output_tokens": 0
}
}MAI-Image 2.6 生图/编辑
文生图 API 参考
MAI-Image 2.6 文生图 API 参考与在线调试 — 纯文本提示词出图,width/height 自定义尺寸最高 1536×1536 面积,固定返回 PNG base64
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": "MAI-Image-2.6-Flash",
"prompt": "一家古风茶馆的门面,木质招牌上写着「API易 欢迎」,红灯笼,黄昏暖光,写实摄影"
}
'import requests
url = "https://api.apiyi.com/v1/images/generations"
payload = {
"model": "MAI-Image-2.6-Flash",
"prompt": "一家古风茶馆的门面,木质招牌上写着「API易 欢迎」,红灯笼,黄昏暖光,写实摄影"
}
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: 'MAI-Image-2.6-Flash',
prompt: '一家古风茶馆的门面,木质招牌上写着「API易 欢迎」,红灯笼,黄昏暖光,写实摄影'
})
};
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' => 'MAI-Image-2.6-Flash',
'prompt' => '一家古风茶馆的门面,木质招牌上写着「API易 欢迎」,红灯笼,黄昏暖光,写实摄影'
]),
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\": \"MAI-Image-2.6-Flash\",\n \"prompt\": \"一家古风茶馆的门面,木质招牌上写着「API易 欢迎」,红灯笼,黄昏暖光,写实摄影\"\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\": \"MAI-Image-2.6-Flash\",\n \"prompt\": \"一家古风茶馆的门面,木质招牌上写着「API易 欢迎」,红灯笼,黄昏暖光,写实摄影\"\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\": \"MAI-Image-2.6-Flash\",\n \"prompt\": \"一家古风茶馆的门面,木质招牌上写着「API易 欢迎」,红灯笼,黄昏暖光,写实摄影\"\n}"
response = http.request(request)
puts response.read_body{
"created": 1790999642,
"data": [
{
"b64_json": "<string>"
}
],
"usage": {
"prompt_tokens": 1000,
"total_tokens": 1000,
"output_tokens": 0
}
}右侧的交互式 Playground 支持直接在线调试。请在 Authorization 中填入你的 API Key(格式:
Bearer sk-xxx),选择 model、输入 prompt,按需填 width / height 后一键发送即可。场景说明:本页用于「文本生成图片」,只需提示词。如果你要基于现有图片做修改或双图融合,请使用 图片编辑接口。
⚠️ 三个参数传了就 400
response_format、seed、negative_prompt 本系列都不接受,传入返回 400 Invalid parameters: xxx。从 gpt-image / DALL·E 迁移的代码请先删掉 response_format——返回值固定是 data[0].b64_json。⚠️ 尺寸用 width + height,不用 size本端点传
size 会被静默忽略,恒出 1024×1024。请改用整数 width + height(必须成对),每维 ≥ 768、宽×高 ≤ 2,359,296(1536×1536 面积)。图片 API 全部为同步调用:没有异步任务 ID,客户端断开连接结果即丢失、但请求仍会计费。1024×1024 出图 Flash 约 17 秒、2.6 约 30 秒,建议客户端超时 Flash ≥ 120 秒、2.6 ≥ 180 秒,详见 图片 API 调用须知与最佳实践。
代码示例
Python(OpenAI SDK)
from openai import OpenAI
import base64
client = OpenAI(
api_key="sk-your-api-key",
base_url="https://api.apiyi.com/v1",
timeout=180.0 # 同步调用,超时要给足
)
resp = client.images.generate(
model="MAI-Image-2.6-Flash",
prompt="一家古风茶馆的门面,木质招牌上写着「API易 欢迎」,红灯笼,黄昏暖光,写实摄影",
# width / height 不是 OpenAI SDK 的标准字段,要放进 extra_body
# 不要传 response_format,会 400
extra_body={"width": 1024, "height": 1024}
)
with open("out.png", "wb") as f:
f.write(base64.b64decode(resp.data[0].b64_json))
Python(原生 requests)
import base64
import os
import requests
response = requests.post(
"https://api.apiyi.com/v1/images/generations",
headers={"Authorization": f"Bearer {os.environ['APIYI_API_KEY']}"},
json={
"model": "MAI-Image-2.6",
"prompt": "A red fox sitting on a mossy rock in a misty forest, soft morning light, photorealistic",
"width": 1536,
"height": 1024
},
timeout=180
)
response.raise_for_status()
with open("out.png", "wb") as f:
f.write(base64.b64decode(response.json()["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": "MAI-Image-2.6-Flash",
"prompt": "一张极简风格的咖啡店海报,顶部大字写着「秋日限定」,暖棕配色",
"width": 1024,
"height": 1536
}' | python3 -c "import sys,json,base64; open('out.png','wb').write(base64.b64decode(json.load(sys.stdin)['data'][0]['b64_json']))"
Node.js(原生 fetch)
import fs from 'node:fs';
const resp = await fetch('https://api.apiyi.com/v1/images/generations', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.APIYI_API_KEY}`
},
body: JSON.stringify({
model: 'MAI-Image-2.6-Flash',
prompt: 'A serene Japanese garden with cherry blossoms, koi pond, golden hour',
width: 1792,
height: 1008 // 16:9,宽×高不超过 2,359,296
}),
signal: AbortSignal.timeout(120000)
});
const data = await resp.json();
fs.writeFileSync('out.png', Buffer.from(data.data[0].b64_json, 'base64'));
一次要多张:并发请求
文生图接口每次只返回 1 张(n 无效),要多张就并发:
import base64, os, requests
from concurrent.futures import ThreadPoolExecutor
def gen(i):
r = requests.post(
"https://api.apiyi.com/v1/images/generations",
headers={"Authorization": f"Bearer {os.environ['APIYI_API_KEY']}"},
json={"model": "MAI-Image-2.6-Flash", "prompt": "a watercolor lighthouse at dusk"},
timeout=120
)
r.raise_for_status()
open(f"out-{i}.png", "wb").write(base64.b64decode(r.json()["data"][0]["b64_json"]))
with ThreadPoolExecutor(max_workers=4) as pool:
list(pool.map(gen, range(4)))
参数说明速查
| 参数名 | 类型 | 必填 | 默认 | 说明 |
|---|---|---|---|---|
model | string | ✅ | — | MAI-Image-2.6($0.12/张)或 MAI-Image-2.6-Flash($0.06/张),大小写敏感 |
prompt | string | ✅ | — | 提示词,支持中英文。要出现在图里的文字用引号括起来 |
width | integer | ❌ | 1024 | 输出宽度。≥ 768,与 height 必须成对,非 16 倍数向下取整 |
height | integer | ❌ | 1024 | 输出高度。≥ 768,宽×高 ≤ 2,359,296 |
response_format | — | — | — | 传入返回 400,返回值固定为 b64_json |
seednegative_prompt | — | — | — | 传入返回 400 |
size | — | — | — | 本端点静默忽略 |
n | — | — | — | 本端点无效,恒出 1 张 |
quality、output_format、background、style 等 OpenAI 习惯字段会被静默忽略,输出恒为 PNG。响应格式
{
"created": 1790999642,
"data": [
{
"b64_json": "iVBORw0KGgoAAAANSUhEUgAABAAAAAQACAIAAADwf7zU..."
}
],
"usage": {
"prompt_tokens": 1000,
"total_tokens": 1000,
"input_tokens": 1000,
"output_tokens": 0
}
}
响应字段说明
b64_json是纯 base64,不带data:image/png;base64,前缀,可直接解码,解码后为 PNG。- 没有
url字段,也不返回revised_prompt。 - 1024×1024 的 PNG 约 1.5–1.7 MB,base64 后响应体约 2.1–2.3 MB;1536×1536 约 4–5 MB,注意客户端的响应体大小限制。
usage 不能用来核账:prompt_tokens 恒为 1000 × 张数、output_tokens 恒为 0,是占位值。本系列按张固定计费,真实扣费请以 API易 控制台账单为准。授权
在 API易控制台获取的 API Key
请求体
application/json
模型 ID(大小写敏感)。2.6 画质优先,Flash 速度优先
可用选项:
MAI-Image-2.6-Flash, MAI-Image-2.6 提示词,支持中英文。要出现在图里的文字用引号括起来
示例:
"一家古风茶馆的门面,木质招牌上写着「API易 欢迎」,红灯笼,黄昏暖光,写实摄影"
输出宽度(像素)。每维 ≥ 768,宽×高 ≤ 2,359,296(1536×1536 面积),
必须与 height 成对传入;非 16 倍数向下取整。
必填范围:
x >= 768示例:
1024
输出高度(像素),规则同 width
必填范围:
x >= 768示例:
1024
此页面对您有帮助吗?