模型能力
持久保存生成的输出
在任意 Imagine 请求中使用 storage_options,即可将 生成的资产持久保存到你的 Files API 存储中,之后可通过经过身份验证的 Files API 获取。此外,设置 storage_options.public_url 还可以为该资产生成 永久、可分享的公开 URL;这是任何人都能打开的免身份验证链接,在你撤销之前会一直有效。
存储与公开 URL 创建彼此独立:你可以只进行私有存储而不创建公开 URL,也可以在存储时创建公开 URL。无论哪种方式,响应仍会包含默认始终返回的临时生成 URL。
快速开始
生成图像、将其持久保存到 Files,并获得可分享的公开 URL:所有操作都在一次调用中完成。
curl -s -X POST https://api.x.ai/v1/images/generations \
-H "Authorization: Bearer $XAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "grok-imagine-image-2.0",
"prompt": "A serene Japanese garden in winter",
"response_format": "url",
"storage_options": {
"filename": "garden.jpg",
"public_url": true
}
}'响应体如下:
{
"data": [
{
"url": "https://imgen.x.ai/xai-imgen/xai-tmp-imgen-abc123.jpg",
"file_output": {
"file_id": "file_7de029f4-eb66-42ee-87f8-b2a9d9e7466a",
"filename": "garden.jpg",
"public_url": "https://files-cdn.x.ai/ZsqeMtdcSYWPPHTQdxXDKQ/file_7de029f4-eb66-42ee-87f8-b2a9d9e7466a.jpg"
}
}
]
}需要注意的要点:
data[i].url是临时 Imagine URL,生命周期较短,适合立即使用。无论是否指定storage_options,它都会返回。data[i].file_output.file_id是生成资产的稳定 Files API 标识符。data[i].file_output.public_url是永久 公开 URL。可用于在应用中分享、嵌入和长期存储。
storage_options 参考
| 字段 | 类型 | 说明 |
|---|---|---|
filename | string(必填) | 存储文件的文件名。公开 URL 路径上的扩展名来自该文件名。 |
expires_after | integer(可选) | 从当前时间到存储文件过期的秒数。必须介于 3600(1 小时)和 2592000(30 天)之间。省略该字段表示永久存储。 |
public_url | boolean 或 object(可选) | 设置为 true 可使用默认设置创建公开 URL,也可以传入对象进行配置。省略该字段(或设为 false)表示私有存储。 |
public_url.expires_after | integer(可选) | 从当前时间到公开 URL 过期的秒数。必须介于 3600(1 小时)和 2592000(30 天)之间。详见 过期行为。 |
filename 为必填项。仅传入 storage_options={"filename": "..."} 而不设置其他字段,会将资产私有地持久保存:存储文件不会过期,也不会创建公开 URL。如果之后改变主意,随时可以对已存储的 create_public_url 调用 file_id。
curl -s -X POST https://api.x.ai/v1/images/generations \
-H "Authorization: Bearer $XAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "grok-imagine-image-2.0",
"prompt": "A red circle on a white background",
"response_format": "url",
"storage_options": {"filename": "circle.jpg"}
}'过期行为
storage_options 提供两个相互独立的过期控制项:storage_options.expires_after 控制存储文件何时自动删除,storage_options.public_url.expires_after 控制公开 URL 则控制何时自动撤销。省略 public_url.expires_after 时,URL 会继承文件的过期时间(如果文件没有过期时间,则 URL 永不过期)。
公开 URL 的生命周期永远不能超过其文件,并且两个值都必须介于 1 小时和 30 天 之间。完整规则请参阅 Public URLs → 过期行为;以下示例展示了这两个控制项如何在 Imagine 请求中组合使用。
# Permanent file, 24h public URL
curl -s -X POST https://api.x.ai/v1/images/generations \
-H "Authorization: Bearer $XAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "grok-imagine-image-2.0",
"prompt": "A futuristic city skyline at night",
"response_format": "url",
"storage_options": {
"filename": "skyline.jpg",
"public_url": {"expires_after": 86400}
}
}'
# 2h file, public URL inherits the same expiry
curl -s -X POST https://api.x.ai/v1/images/generations \
-H "Authorization: Bearer $XAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "grok-imagine-image-2.0",
"prompt": "A futuristic city skyline at night",
"response_format": "url",
"storage_options": {
"filename": "skyline.jpg",
"expires_after": 7200,
"public_url": true
}
}'该 file_output响应
每个设置了 storage_options 的 Imagine 响应都会在每个生成的资产上包含一个 file_output 块:
| 字段 | 始终存在 | 含义 |
|---|---|---|
file_id | 是 | 稳定的 Files API 标识符。将其用于 client.files.* 操作。 |
filename | 是 | 你在 storage_options。 |
expires_at | 仅在文件设置了过期时间时 | 文件过期时的 Unix 时间戳。 |
public_url | 仅当设置了 storage_options.public_url 且创建成功时 | 永久、可分享的 URL。 |
public_url_expires_at | 仅当公开 URL 设置了过期时间时 | 公开 URL 失效时的 Unix 时间戳。永久 URL 不包含该字段。 |
public_url_error | 仅在部分失败时 | 资产已存储但公开 URL 创建失败时返回的可读错误。详见 Public URL 错误。 |
Python SDK 还会将 response.public_url 和 response.public_url_error 作为 ImageResponse 和 VideoResponse。
多个输出(n > 1)
在一次调用中请求多张图像时,每张图像都会获得自己的 file_id 以及具有唯一 token 的 public_url。这些文件完全独立,撤销或删除其中一个不会影响其他文件。
curl -s -X POST https://api.x.ai/v1/images/generations \
-H "Authorization: Bearer $XAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "grok-imagine-image-2.0",
"prompt": "A cat wearing a hat, four different art styles",
"n": 4,
"response_format": "url",
"storage_options": {"filename": "cat-styles.jpg", "public_url": true}
}'存储图像编辑输出
storage_options 在 /v1/images/edits 上的工作方式与 /v1/images/generations 相同。编辑结果会存储为新文件。
curl -s -X POST https://api.x.ai/v1/images/edits \
-H "Authorization: Bearer $XAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "grok-imagine-image-2.0",
"prompt": "Add a party hat to the dog",
"image": {
"url": "https://docs.x.ai/assets/api-examples/images/style-realistic.png"
},
"response_format": "url",
"storage_options": {"filename": "party-dog.png", "public_url": true}
}'存储视频输出
视频端点(/v1/videos/generations、/v1/videos/edits、/v1/videos/extensions)使用相同的 storage_options 结构。由于视频生成是异步的,file_output.public_url 会填充在视频生成完成后的已完成 响应中。
# Start the generation
REQUEST_ID=$(curl -s -X POST https://api.x.ai/v1/videos/generations \
-H "Authorization: Bearer $XAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "grok-imagine-video-1.5",
"prompt": "A ball bouncing slowly on a flat surface",
"duration": 5,
"storage_options": {"filename": "bouncing-ball.mp4", "public_url": true}
}' | jq -r '.request_id')
# Poll until done
while true; do
RESULT=$(curl -s "https://api.x.ai/v1/videos/$REQUEST_ID" \
-H "Authorization: Bearer $XAI_API_KEY")
STATUS=$(echo "$RESULT" | jq -r '.status')
if [ "$STATUS" = "done" ]; then
echo "$RESULT" | jq '.video.file_output'
break
elif [ "$STATUS" = "failed" ]; then
echo "$RESULT" | jq .
break
fi
sleep 5
done图像转视频、视频编辑和视频扩展均以相同方式接受 storage_options。
Public URL 错误
在极少数情况下,即使资产生成和存储成功,公开 URL 创建也可能失败,例如出现暂时性基础设施问题,或团队达到 有效 URL 配额。此时不会让整个请求失败,响应仍会包含有效的 file_output.file_id 和原始资产 URL,仅将 public_url 替换为 public_url_error:
{
"data": [
{
"url": "https://imgen.x.ai/.../xai-tmp-imgen-abc123.jpg",
"file_output": {
"file_id": "file_abc123",
"filename": "poster.jpg",
"public_url_error": "Public URL creation timed out. The file was stored successfully."
}
}
]
}如果看到 public_url_error,文件仍在你的存储中;你可以直接通过 Files API 重试公开 URL 创建,而无需重新生成资产:
import os
import xai_sdk
client = xai_sdk.Client(api_key=os.getenv("XAI_API_KEY"))
response = client.image.sample(
prompt="A vintage poster",
model="grok-imagine-image-2.0",
storage_options={"filename": "poster.jpg", "public_url": True},
)
if response.public_url_error:
# The image is stored — just retry the public URL on the file directly.
resp = client.files.create_public_url(response.file_output.file_id)
public_url = resp.public_url
else:
public_url = response.public_url
print(public_url)管理通过 Imagine 创建的文件
通过 storage_options 创建的文件都是完整的一等 Files API 文件。使用 Files API 列出、获取、更新和删除这些文件,并使用公开 URL 端点撤销或重新创建 URL:
FILE_ID="<file_output.file_id from the generation response>"
# Stop sharing publicly (file stays in your storage)
curl -s -X POST "https://api.x.ai/v1/files/$FILE_ID/public-url/revoke" \
-H "Authorization: Bearer $XAI_API_KEY"
# Re-create with a 7-day expiry
curl -s -X POST "https://api.x.ai/v1/files/$FILE_ID/public-url" \
-H "Authorization: Bearer $XAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{"expires_after": 604800}'
# Delete the file (also revokes the public URL)
curl -s -X DELETE "https://api.x.ai/v1/files/$FILE_ID" \
-H "Authorization: Bearer $XAI_API_KEY"限制
每个团队最多可拥有 1,000 个有效公开 URL。 达到上限时,响应会设置
public_url_error;请撤销未使用的 URL 以释放名额。过期限制(参见 过期行为):
两个
expires_after值都必须介于 1 小时和 30 天。public_url.expires_after必须小于或等于文件的expires_after。
自定义文件名会影响公开 URL 路径。 传入
storage_options.filename = "my-cover.png"会使公开 URL 以.png结尾。存储的内容类型仍由生成的资产决定,而不是由文件名决定。response_format不影响存储。storage_options无论请求url还是b64_json。公开 URL 与临时 URL 相互独立。 二者在同一个响应中返回,但具有独立生命周期。撤销公开 URL 不会影响临时 URL。
所有验证都是同步的。 无效的存储配置会在生成开始前被拒绝,因此不会将计算资源浪费在格式错误的请求上。
相关内容
Files API 集成:概览以及组合使用输入与输出的完整示例。
将文件引用为输入:输入方向:传入已存储的
file_id来替代 URL。Files → Public URLs:任意文件的公开 URL 生命周期,无论文件以何种方式创建。
管理文件:上传、列出、获取、更新和删除文件。
最后更新:2026 年 10 月 9 日