图片生成
将文字提示词转换为图片。
生成图片
/v1/images/generations请求体使用 JSON。选择可用的模型,描述画面内容,并明确所需数量和尺寸。
curl https://api.sanzhilang.online/v1/images/generations \
-H "Authorization: Bearer $EXGRAVITY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "A white ceramic vase on a pale background, soft natural light",
"n": 1,
"size": "2048x2048"
}'import json
import os
from urllib.request import Request, urlopen
payload = {
"model": "gpt-image-2",
"prompt": "A white ceramic vase on a pale background, soft natural light",
"n": 1,
"size": "2048x2048"
}
request = Request(
"https://api.sanzhilang.online/v1/images/generations",
data=json.dumps(payload).encode(),
headers={
"Authorization": f"Bearer {os.environ['EXGRAVITY_API_KEY']}",
"Content-Type": "application/json",
},
method="POST",
)
with urlopen(request, timeout=180) as response:
result = json.load(response)
print(result["data"])const response = await fetch(
"https://api.sanzhilang.online/v1/images/generations",
{
method: "POST",
headers: {
Authorization: `Bearer ${process.env.EXGRAVITY_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
"model": "gpt-image-2",
"prompt": "A white ceramic vase on a pale background, soft natural light",
"n": 1,
"size": "2048x2048"
}),
signal: AbortSignal.timeout(180_000),
},
);
const result = await response.json();
if (!response.ok) throw new Error(result.error?.message ?? response.statusText);
console.log(result.data);常用参数
| 参数 | 类型 | 说明 |
|---|---|---|
model | string | 必填。当前密钥可用的模型 ID。 |
prompt | string | 必填。描述主体、构图、光线与风格。 |
n | integer | 生成数量,默认 1。上限按模型能力确定。 |
size | string | 输出尺寸,按当前模型支持的规格填写。 |
response_format | string | 返回格式,如 url 或 b64_json;支持情况按模型确定。 |
返回与保存
读取 data 数组里的图片。不要假定所有模型都返回 URL;按实际字段处理 b64_json 或 url。生成需要一定时间,请为客户端设置足够的超时。
{
"created": 1792324800,
"data": [
{
"b64_json": "<base64-encoded image>"
}
]
}





















































