图像生成接口
介绍
LinkAI 统一图像生成接口,通过一个接口接入 GPT-Image Nano Banana、Seedream 等多家图像生成模型,支持:
- 文生图:根据文本描述生成图像
- 图生图(图像编辑):传入参考图,根据文本描述对图像进行修改、风格化等
- 多模型聚合:通过切换
model参数即可使用不同厂商的图像模型,响应格式统一 - OpenAI 兼容:响应结构与 OpenAI
images/generations接口对齐
在线接口调试:API接口调试
接口定义
接口地址
POST https://api.link-ai.tech/v1/images/generations
请求头
| 参数 | 取值 | 说明 |
|---|---|---|
| Authorization | Bearer YOUR_API_KEY | 参考 接口鉴权说明 创建 API Key 并填入 |
| Content-Type | application/json | 表明使用 JSON 格式请求 |
请求体
| 参数 | 类型 | 是否必传 | 说明 |
|---|---|---|---|
| model | string | 是 | 图像模型编码,所有可选模型见 模型列表 |
| prompt | string | 是 | 图像生成提示词,描述想要生成的图像内容;当传入 images 时则描述对图像的修改要求 |
| images | list<string> | 否 | 参考图,传入时表示 图生图 / 图像编辑。可传单张或多张(多图融合)。每个元素支持两种格式: ① 公网可访问的图片 URL,如 https://xxx.com/a.jpg② base64 数据 URI,如 data:image/png;base64,iVBORw0KG... |
| size | string | 否 | 图像分辨率档位,可选 1K / 2K / 3K / 4K,取值范围在模型服务查看,不传则使用模型自动尺寸 |
| aspect_ratio | string | 否 | 图像宽高比,可选 1:1 / 2:3 / 3:2 / 3:4 / 4:3 / 4:5 / 5:4 / 9:16 / 16:9 / 21:9。与 size 组合使用时,平台会自动解析为对应像素值(例如 2K + 16:9 → 2048x1152) |
| quality | string | 否 | 图像质量,例如 standard / hd,取值范围由具体模型决定 |
| response_format | string | 否 | 响应格式,目前仅支持 url(返回图像 CDN 地址),默认 url |
请求示例(文生图):
{
"model": "gpt-image-2",
"prompt": "一只在月光下奔跑的白色狐狸,水彩画风格",
"size": "2K",
"aspect_ratio": "16:9"
}
请求示例(图生图,URL 输入):
{
"model": "gpt-image-2",
"prompt": "把背景换成雪山,整体改为油画风格",
"images": ["https://cdn.link-ai.tech/user/generate/eaf7c477-2fe5-4420-8c23-df2a27a55593.png"]
}
请求示例(图生图,base64 输入):
{
"model": "gpt-image-2",
"prompt": "把背景换成雪山,整体改为油画风格",
"images": ["data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."]
}
响应结果
成功响应
接口为同步返回,生成完成后一次性返回所有图像 URL(与 OpenAI images/generations 接口结构兼容):
{
"data": [
{ "url": "https://cdn.link-ai.tech/user/generate/eaf7c477-2fe5-4420-8c23-df2a27a55593.png" }
]
}
注意:
data为生成的图像列表,每个元素的url字段为图像访问地址- 平台会将图像统一转存到 LinkAI CDN,URL 长期有效,可直接用于前端展示或下载
错误响应
接口异常时返回如下结构:
{
"error": {
"message": "模型不存在或用户无权限使用: xxx",
"type": "invalid_request_error"
}
}
根据 HTTP 状态码判断错误类型:
| HTTP 状态码 | 描述 |
|---|---|
| 400 | 请求参数错误,如 prompt 为空、images 不可访问等 |
| 401 | 接口鉴权失败,请检查 API Key 是否填写正确 |
| 402 | 模型不存在或当前账号无权限使用该模型 |
| 406 | 账号积分额度不足 |
| 409 | 内容审核不通过,提示词或参考图中可能包含敏感内容 |
| 503 | 接口调用异常,联系客服处理 |
模型列表
接口支持的图像生成模型如下,完整模型列表可在平台 模型服务 页面查看。各模型价格见 计费规则。
总览:
| 模型编码 (model) | 名称 | 能力 |
|---|---|---|
| gpt-image-2 | GPT-Image 2 | OpenAI 旗舰图像模型,质量与可控性最佳 |
| doubao-seedream-4.5 | Seedream 4.5 | 豆包高质量图像模型,国产新一代 |
| doubao-seedream-4.0 | Seedream 4.0 | 豆包图像模型 |
| gemini-3-pro-image-preview | Nano Banana Pro | Gemini 3 Pro 图像模型,强图像编辑 |
| gemini-3.1-flash-image-preview | Nano Banana V2 | Gemini 3.1 Flash 图像模型,速度快 |
| gemini-2.5-flash-image | Nano Banana | Gemini 2.5 Flash 图像模型 |
各模型详细参数:
GPT-Image 2
模型编码:gpt-image-2
基础参数:
| 参数 | 取值范围 | 默认值 |
|---|---|---|
| size | 1K / 2K / 4K | 2K |
| aspect_ratio | 1:1 / 3:2 / 2:3 / 16:9 / 9:16 | 16:9 |
| quality | low / medium / high | medium |
参考图(图生图):images 最多 15 张
Seedream 4.5
模型编码:doubao-seedream-4.5
基础参数:
| 参数 | 取值范围 | 默认值 |
|---|---|---|
| size | 2K / 4K | 2K |
| aspect_ratio | 自动 / 1:1 / 2:3 / 3:2 / 3:4 / 4:3 / 4:5 / 5:4 / 9:16 / 16:9 / 21:9 | 自动 |
参考图(图生图):images 最多 10 张
Seedream 4.0
模型编码:doubao-seedream-4.0
基础参数:
| 参数 | 取值范围 | 默认值 |
|---|---|---|
| size | 2K / 4K | 2K |
| aspect_ratio | 自动 / 1:1 / 2:3 / 3:2 / 3:4 / 4:3 / 4:5 / 5:4 / 9:16 / 16:9 / 21:9 | 自动 |
参考图(图生图):images 最多 10 张
Nano Banana Pro
模型编码:gemini-3-pro-image-preview
基础参数:
| 参数 | 取值范围 | 默认值 |
|---|---|---|
| size | 1K / 2K / 4K | 1K |
| aspect_ratio | 自动 / 1:1 / 2:3 / 3:2 / 3:4 / 4:3 / 4:5 / 5:4 / 9:16 / 16:9 / 21:9 | 自动 |
参考图(图生图):images 最多 3 张
Nano Banana V2
模型编码:gemini-3.1-flash-image-preview
基础参数:
| 参数 | 取值范围 | 默认值 |
|---|---|---|
| size | 512 / 1K / 2K / 4K | 1K |
| aspect_ratio | 自动 / 1:1 / 2:3 / 3:2 / 3:4 / 4:3 / 4:5 / 5:4 / 9:16 / 16:9 / 21:9 | 自动 |
参考图(图生图):images 最多 3 张
Nano Banana
模型编码:gemini-2.5-flash-image
基础参数:
| 参数 | 取值范围 | 默认值 |
|---|---|---|
| aspect_ratio | 自动 / 1:1 / 2:3 / 3:2 / 3:4 / 4:3 / 4:5 / 5:4 / 9:16 / 16:9 / 21:9 | 自动 |
参考图(图生图):images 最多 3 张
模型不支持的字段会被自动忽略。例如 Nano Banana 不支持
size/quality,传入也不会报错。
示例代码
1. CURL 请求
文生图:
curl --request POST \
--url https://api.link-ai.tech/v1/images/generations \
--header 'Authorization: Bearer YOUR_API_KEY' \
--header 'Content-Type: application/json' \
--data '{
"model": "gpt-image-2",
"prompt": "一只在月光下奔跑的白色狐狸,水彩画风格",
"size": "2K",
"aspect_ratio": "16:9"
}'
图生图:
curl --request POST \
--url https://api.link-ai.tech/v1/images/generations \
--header 'Authorization: Bearer YOUR_API_KEY' \
--header 'Content-Type: application/json' \
--data '{
"model": "gemini-3.1-flash-image-preview",
"prompt": "把背景换成雪山,整体改为油画风格",
"images": ["https://cdn.link-ai.tech/user/generate/eaf7c477-2fe5-4420-8c23-df2a27a55593.png"]
}'
注意: 在 YOUR_API_KEY 处填入你创建的 API Key。
2. Python 代码请求
import requests
url = "https://api.link-ai.tech/v1/images/generations"
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer YOUR_API_KEY"
}
body = {
"model": "gpt-image-2",
"prompt": "一只在月光下奔跑的白色狐狸,水彩画风格",
"size": "2K",
"aspect_ratio": "16:9"
}
res = requests.post(url, json=body, headers=headers)
if res.status_code == 200:
image_url = res.json()["data"][0]["url"]
print(f"生成图像地址: {image_url}")
else:
error = res.json().get("error", {})
print(f"请求异常, 错误码={res.status_code}, 错误信息={error.get('message')}")
3. OpenAI SDK 调用
由于响应结构与 OpenAI 兼容,可直接使用 OpenAI SDK 调用。注意 OpenAI SDK 的 size 参数仅接受像素值(如 1024x1024),如需使用 LinkAI 的 1K/2K/4K 档位与 aspect_ratio 等扩展字段,需通过 extra_body 透传:
from openai import OpenAI
client = OpenAI(
base_url="https://api.link-ai.tech/v1",
api_key="YOUR_API_KEY"
)
response = client.images.generate(
model="gpt-image-2",
prompt="一只在月光下奔跑的白色狐狸,水彩画风格",
extra_body={
"size": "2K",
"aspect_ratio": "16:9"
}
)
print(response.data[0].url)