文档 / 示例 / 文本生成图像
文本生成图像
使用 POST /v1/images/generations 从文本提示词生成图像。一次调用,响应中即返回图像 URL。
#Python
text_to_image.pypython
import requests, os
response = requests.post(
"https://relayrouter.io/v1/images/generations",
headers={
"Authorization": f"Bearer {os.environ['RELAYROUTER_API_KEY']}",
"Content-Type": "application/json",
},
json={
"model": "gpt-image-1",
"prompt": "a cinematic shot of an astronaut floating above Earth at sunrise, photorealistic",
"n": 1,
},
)
response.raise_for_status()
print(response.json()["data"][0]["url"])#Node.js / TypeScript
textToImage.tstypescript
const res = await fetch("https://relayrouter.io/v1/images/generations", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.RELAYROUTER_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "gpt-image-1",
prompt: "a cinematic shot of an astronaut floating above Earth at sunrise, photorealistic",
n: 1,
}),
});
const { data } = await res.json();
console.log("Image URL:", data[0].url);#cURL
terminalbash
curl https://relayrouter.io/v1/images/generations \
-H "Authorization: Bearer $RELAYROUTER_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-1",
"prompt": "a cinematic shot of an astronaut floating above Earth at sunrise, photorealistic",
"n": 1
}'#参数
| 参数 | 类型 | 说明 |
|---|---|---|
model | string | 图像模型 ID(必填) |
prompt | string | 图像的文本描述(必填) |
n | integer | 要生成的图像数量。默认值:1 |
size | string | 可选的输出尺寸,取决于具体模型(例如 "1024x1024") |
图像模型 ID 可通过
GET /v1/models 或模型页面查找。完整的端点细节请参阅 Generate Image 参考。