/omni/media/v1/images/generations/tasks圖像生成
圖像生成分兩個步驟:先建立任務(回 202 + task id),再輪詢到終態、下載簽章 URL。把 client 指向媒體 base URL https://api.atptoken.ai/omni/media/v1,用專案(project) atp- 金鑰驗證。Gateway 會把你的 unified model 路由到圖像供應商(例如 gpt-image-2);名稱用 GET /v1/models 確認。流程與影片生成一致。
- 輸出寫入物件儲存、以帶簽章的邊緣 URL(
https://media-<env>.atptoken.ai/v/...)回傳,TTL 30 分鐘——不會內嵌 base64。請盡快取用。 - 當專案餘額 ≤ 0,請求會以
402 insufficient_quota拒絕。
媒體模型依環境與專案開放。請先呼叫 GET https://api.atptoken.ai/v1/models;未出現在回應中的模型,代表這把金鑰不能使用。
建立任務#
curl https://api.atptoken.ai/omni/media/v1/images/generations/tasks \
-H "Authorization: Bearer atp-..." -H "Content-Type: application/json" \
-H "Idempotency-Key: img-job-001" \
-d '{ "model": "gpt-image-2", "prompt": "a watercolor cat", "size": "1024x1024", "quality": "high", "n": 1 }'
# → 202 { "id": "img_..." }網路失敗重試建立時沿用同一個 Idempotency-Key;真正的新任務用新的冪等鍵。
編輯類模型的輸入圖——reference_assets#
編輯類模型(nano-banana-pro-edit、qwen-image-edit-max …)的輸入圖要放在頂層 reference_assets 陣列、元素是物件。必須是 [{ "url": "…" }]。
content[]、image、image_url三種寫法都會被擋422 Invalid input.reference_assets: required.- 給一個純字串陣列也會被擋
Invalid input.reference_assets。
curl https://api.atptoken.ai/omni/media/v1/images/generations/tasks \
-H "Authorization: Bearer atp-..." -H "Content-Type: application/json" \
-H "Idempotency-Key: img-edit-001" \
-d '{
"model": "nano-banana-pro-edit",
"prompt": "Background: a sunlit marble kitchen counter. Preserve the product exactly — same shape, label text, colors and proportions as the reference.",
"n": 1,
"reference_assets": [
{ "url": "https://example.com/product.png" }
]
}'
# → 202 { "id": "img_..." }url 接受哪些形式(圖片端點):
把上傳檔案換成可用 URL 的最簡做法:POST /v1/files(回應的金鑰是 id),再對 GET /v1/files/{id} 不跟隨重導、取 Location header——那是物件儲存的 presigned URL,免驗證、時效約 15 分鐘。見 /v1/files。
編輯呼叫仍然必填 prompt。帶一句簡短指示就夠,但這個欄位不能省略。
輪詢至終態#
每 3–8 秒輪詢一次:
curl https://api.atptoken.ai/omni/media/v1/images/generations/tasks/img_... \
-H "Authorization: Bearer atp-..."
# → { "status": "succeeded", "data": [ { "url": "https://media-prod.atptoken.ai/v/image/...png?exp=...&sig=..." } ], "usage": { "prompt_tokens": 37, "completion_tokens": 7024, "total_tokens": 7061 } }status流轉:queued→running→succeeded/failed/cancelled/expired。- 成功任務的
data[].url是 30 分鐘時效的簽章 URL;過期後任務仍顯示succeeded但expired: true、url為 null——請立即下載(要重拿只能重新生成)。 usage回報 token 用量供帳務核對;failed任務帶結構化error物件。data[].url的副檔名不能當格式依據:結尾寫.png的 URL 實際位元組可能是 JPEG。若格式會影響後續處理(例如把結果再上傳、或做轉檔),請用位元組(magic number)判斷,不要相信檔名。
Python 範例#
最小的建立+輪詢流程:
import os, time, requests
BASE = "https://api.atptoken.ai/omni/media/v1"
HEADERS = {
"Authorization": f"Bearer {os.environ['ATP_API_KEY']}",
"Content-Type": "application/json",
}
resp = requests.post(
f"{BASE}/images/generations/tasks",
headers={**HEADERS, "Idempotency-Key": "img-job-001"},
json={
"model": "gpt-image-2",
"prompt": "a watercolor cat",
"size": "1024x1024",
"quality": "high",
"n": 1,
},
timeout=60,
)
resp.raise_for_status()
task_id = resp.json()["id"]
while True:
task = requests.get(
f"{BASE}/images/generations/tasks/{task_id}", headers=HEADERS, timeout=60
).json()
if task["status"] in ("succeeded", "failed", "cancelled", "expired"):
break
time.sleep(5)
if task["status"] == "succeeded":
for i, item in enumerate(task["data"]):
image = requests.get(item["url"], timeout=120).content # signed URL, 30-min TTL
with open(f"result_{i}.png", "wb") as f:
f.write(image)
else:
raise RuntimeError(task.get("error"))按張計費怎麼判尺寸檔位#
按張計費的模型,是依我們實際交付的圖片、以其最長邊判定檔位:
估算成本前有兩點要知道:
- 模型的預設輸出可能比你以為的寬。 例如 1408×768 的結果(約 1.1 百萬像素)最長邊是 1408,因此依
2K檔計費——即使它的總像素更接近 1K 圖。 - 部分模型會忽略請求中的
size,一律回傳原生解析度。若檔位會影響你的成本,請讀取回傳圖片的實際尺寸,不要假設請求一定被採用。
只有實際交付的圖片才計費——生成失敗或上傳未完成的不收費。若某模型多個檔位同價(Nano Banana、Nano Banana Pro),檔位就不影響你付的金額。