切换主题
图片生成
Sprelay API 通过 POST /v1/images/generations 提供 OpenAI 兼容的图片生成接口。当前已实际验证 gpt-image-2、1024x1024、low 质量和 Base64 图片返回。
验证状态
本页示例已使用临时低额度密钥完成真实请求。图片编辑接口尚未验证,因此本页不包含 /v1/images/edits。
使用前准备
- 在模型广场确认
gpt-image-2当前可见。 - 创建 API 密钥时选择“image-2生图专用”分组。
- 为图片密钥设置单独额度,首次测试建议只生成 1 张低质量图片。
- 将密钥保存到
SPRELAY_API_KEY环境变量,不要写进脚本或前端代码。
模型、分组和价格会动态调整,实时信息以模型广场为准。
最小请求
bash
curl https://sprelaytoken.com/v1/images/generations \
-H "Authorization: Bearer $SPRELAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "A simple flat blue circle centered on a white background.",
"n": 1,
"size": "1024x1024",
"quality": "low"
}' \
--output response.json成功响应的图片位于 data[0].b64_json。在支持 jq 和 base64 的环境中,可以解码为 PNG:
bash
jq -r '.data[0].b64_json' response.json | base64 --decode > sprelay-image.pngPython 示例
python
import base64
import os
import requests
response = requests.post(
"https://sprelaytoken.com/v1/images/generations",
headers={
"Authorization": f"Bearer {os.environ['SPRELAY_API_KEY']}",
"Content-Type": "application/json",
},
json={
"model": "gpt-image-2",
"prompt": "A simple flat blue circle centered on a white background.",
"n": 1,
"size": "1024x1024",
"quality": "low",
},
timeout=180,
)
response.raise_for_status()
payload = response.json()
image_bytes = base64.b64decode(payload["data"][0]["b64_json"])
with open("sprelay-image.png", "wb") as image_file:
image_file.write(image_bytes)PowerShell 示例
powershell
$headers = @{
Authorization = "Bearer $env:SPRELAY_API_KEY"
"Content-Type" = "application/json"
}
$body = @{
model = "gpt-image-2"
prompt = "A simple flat blue circle centered on a white background."
n = 1
size = "1024x1024"
quality = "low"
} | ConvertTo-Json
$response = Invoke-RestMethod `
-Uri "https://sprelaytoken.com/v1/images/generations" `
-Method Post `
-Headers $headers `
-Body $body `
-TimeoutSec 180
$bytes = [Convert]::FromBase64String($response.data[0].b64_json)
[IO.File]::WriteAllBytes("sprelay-image.png", $bytes)已验证字段
| 字段 | 说明 |
|---|---|
model | 必填;当前验证值为 gpt-image-2 |
prompt | 必填;描述需要生成的画面 |
n | 生成数量;首次调用使用 1 控制费用 |
size | 当前验证值为 1024x1024 |
quality | 当前验证值为 low |
不要把其他模型的尺寸、质量或风格参数直接套用到 gpt-image-2。增加字段前先用最小请求确认当前线路支持。
返回格式
当前验证响应使用 Base64:
json
{
"created": 1786099200,
"data": [
{
"b64_json": "iVBORw0KGgoAAA..."
}
]
}Base64 内容通常很大。生产环境应直接解码或存入对象存储,不要把完整内容写入日志、数据库文本列或错误上报。
费用与重试
- 使用专用低额度密钥隔离图片费用。
- 首次验证固定
n: 1和quality: low。 - 请求成功后到使用日志核对模型、分组和消费。
- 图片请求可能已经在上游执行;连接中断时不要立即无限重试,以免重复生成和重复计费。
常见错误
401:密钥缺失、格式错误或已被禁用。403:密钥额度不足,或密钥分组不支持gpt-image-2。400:检查prompt、n、size和quality。429:降低并发并稍后重试。5xx:查看使用日志确认请求是否已执行,再决定是否重试。
