圖像
建立圖片
根據提示詞建立圖片
概覽
使用 GET /v1/models?recommended_for=image 獲取當前的圖片模型,然後明確發送所選的 model。
gpt-image-2 是一款按 Token 計費的 GPT Image 模型。TokenLab 遵循 OpenAI 官方的文字輸入、圖片輸入、已報告的快取輸入以及圖片輸出 Token 的使用量拆分;它並非以單張圖片固定價格計費。
對於 gpt-image-2 圖片生成,支援的公開參數包括 prompt、n、size、quality、response_format、async、background、output_format、output_compression 或 compression、moderation 以及 user。background 接受 auto 或 opaque;不支援 transparent。省略 size 或 quality 可讓 TokenLab 使用 auto;自訂 size 值必須使用下方記錄的靈活 WIDTHxHEIGHT 規範。
input_fidelity 不屬於目前 TokenLab 對 gpt-image-2 支援的欄位;請省略它,否則請求將返回 400 unsupported_parameter。
模型行為注意事項
Google Gemini 圖片系列模型不共享相同的選擇器規範:
gemini-3.1-flash-image、gemini-nano-banana-2.1、gemini-3-pro-image、nano-banana-2.1和nano-banana-pro在其公開的文字轉圖片、圖片轉圖片和圖片編輯操作中支援aspect_ratio以及resolution(1k、2k、4k)。nano-banana-2在文字轉圖片和圖片轉圖片生成中均支援aspect_ratio和resolution(1k、2k、4k)。gemini-3.1-flash-lite-image和nano-banana-2-lite僅支援1k解析度的文字轉圖片、圖片轉圖片和圖片編輯。nano-banana支援aspect_ratio,但不公開resolution選擇。- 對於使用
gemini-3.1-flash-image、gemini-nano-banana-2.1、gemini-3.1-flash-lite-image或gemini-3-pro-image進行圖片轉圖片操作,請使用此端點並設定operation: "image-to-image"以及參考圖片。請將這些模型的圖片編輯請求發送至/v1/images/edits。 - 對於 Nano Banana 參考圖片請求,請在此端點 (
/v1/images/generations) 使用nano-banana-2、nano-banana-2.1、nano-banana-2-lite或nano-banana-pro,並設定operation: "image-to-image"和image_urls。nano-banana-2、nano-banana-2.1、nano-banana-2-lite和nano-banana-pro的圖片編輯請求也使用此端點,並設定operation: "image-edit"。請勿將 Nano Banana 參考圖片請求發送至/v1/images/edits。 - 此端點上的參考圖片可以 JSON
image_url/image_urls形式提供,或作為 multipartimage檔案提供。/v1/images/generations不接受images[]和file_id;僅為記錄了images[].file_id的/v1/images/edits模型建立/v1/files參考。
對於 Google 圖片系列,請優先使用 aspect_ratio,僅在模型明確支援時才發送 resolution。
xAI Grok Imagine 圖片模型 (grok-imagine-image、grok-imagine-image-quality 和舊版 grok-imagine-image-pro) 支援 aspect_ratio 以及 resolution (1k、2k)。grok-imagine-image-pro 作為 grok-imagine-image-quality 的相容性 ID 保留。
請求主體
同步請求逾時: 部分圖片請求會內聯返回最終圖片並等待生成完成。高解析度或高品質請求可能需要接近一分鐘或更長時間,因此請將您的 HTTP 客戶端逾時設定為至少 120s。如果建立回應包含 status: "pending"、task_id 或 poll_url,請改為追蹤返回的 poll_url。
要使用的模型(例如 gpt-image-2、flux-pro-1.1 或 nano-banana-pro)。查詢 GET /v1/models?recommended_for=image 以獲取當前推薦列表。
所需圖片的文字描述。
用於圖片轉圖片生成的公開 HTTPS 參考圖片 URL。對於 Nano Banana 參考圖片請求,請將 operation 設定為 image-to-image;nano-banana-pro 可能包含 resolution。
公開 HTTPS 參考圖片 URL。在 JSON 請求中使用此欄位來提供一個或多個參考圖片。此端點不支援 file_id 和 images[]。
針對區分主要輸入與參考輸入的模型,提供額外的參考圖片 URL。
用於圖片轉圖片生成的 multipart 參考圖片檔案。當來源圖片為私有或受標頭保護時使用此項。這與 /v1/files 的 file_id 不同,此端點不接受 file_id。
1要生成的圖片數量(1-10,視模型而定)。
圖片尺寸。適用於 OpenAI 風格的圖片系列以及其他接受精確像素尺寸的模型。
對於 gpt-image-2,size 接受 auto 或 WIDTHxHEIGHT。自訂尺寸必須均為 16 的倍數,最長邊最多為 3840px,長寬比最多為 3:1,且總像素必須在 655,360 到 8,294,400 之間。aspect_ratio 和 resolution 不屬於目前 TokenLab 對 gpt-image-2 的模型詳細資訊。
對於具有解析度層級的 Google 圖片模型 (gemini-3.1-flash-image、gemini-nano-banana-2.1、gemini-3-pro-image、gemini-3.1-flash-lite-image、nano-banana-2、nano-banana-2.1 和 nano-banana-pro),size 接受來自模型尺寸表中的輸出尺寸,例如 1376x768 (16:9 於 1K),或解析度層級:1K、2K 或 4K。TokenLab 會將層級作為 resolution 發送,因此請求及其價格與使用 resolution 時相同。模型不支援的層級(例如 gemini-3.1-flash-lite-image 上的 2K),或與 resolution 不同的層級,將返回 400 並帶有 invalid_image_size_contract 代碼以及模型的 allowed_resolutions。對於這些模型,推薦的形式是 aspect_ratio 搭配 resolution。
視模型而定的長寬比選擇器。
常見的 Google 圖片系列值包括 1:1、16:9、9:16、3:2 和 2:3。
視模型而定的輸出解析度。gemini-3.1-flash-image、gemini-nano-banana-2.1、gemini-3-pro-image、nano-banana-2、nano-banana-2.1 和 nano-banana-pro 支援 1k、2k 和 4k;Grok Imagine 圖片模型支援 1k 和 2k。除非所選模型和操作明確支援,否則請省略此欄位。
圖片品質。GPT Image 模型(如 gpt-image-2)使用 auto、low、medium 或 high。其他模型可能使用不同的值;在發送非預設值之前,請檢查模型詳細資訊。
圖片返回的方式:url 或 b64_json。
- 省略:當模型返回圖片位元組時,每張圖片位於
data[].b64_json中,若 TokenLab 已儲存,則同時位於data[].url中。當模型返回連結時,每張圖片位於data[].url中。 url:每張圖片僅位於data[].url中。b64_json:每張圖片僅作為 Base64 資料位於data[].b64_json中。
連結為 TokenLab 媒體結果 URL。它們有效期為 30 天。請參閱 資料保留。
false與 gpt-image-2 或官方 FLUX/BFL 圖片模型一起設定為 true 以先建立任務。已完成的非同步圖片任務無論請求的 response_format 為何,均會返回 URL;當您需要 b64_json 時,請使用同步請求。
選用的風格選擇器。僅在所選模型明確記錄時才發送此項;除非模型元資料另有說明,否則對於 gpt-image-2 請省略它。
終端使用者的唯一識別碼。
回應
內聯回應
建立的 Unix 時間戳。
生成的圖片陣列。
每個物件包含:
url(string):TokenLab 媒體結果 URL,有效期為 30 天。當response_format為b64_json時不存在。當省略response_format時,若 TokenLab 無法儲存圖片,則此欄位缺失。b64_json(string):Base64 圖片資料。當response_format為b64_json,或省略該參數且模型返回圖片位元組時存在。revised_prompt(string):選用的提示詞修訂,當所選模型返回修訂時提供。
非同步任務回應
與 gpt-image-2 或官方 FLUX/BFL 圖片模型一起設定 async: true 以建立任務,而不是在建立請求中等待最終圖片。回應包含 status: "pending"、task_id 和 poll_url。請輪詢 /v1/tasks/{task_id} 直到任務達到 completed 或 failed。
非同步圖片任務僅返回最終圖片 URL。如果您需要原始 b64_json 圖片資料,請使用同步請求。
計費可能會在任務建立時預留估算金額。已完成的任務按實際使用量計費,失敗或逾時的任務將釋放或退款。
建立的 Unix 時間戳。
用於輪詢的唯一任務識別碼。
初始狀態:pending。
用於輪詢結果的相對 URL,例如 /v1/tasks/{id}。
任務處於 pending 狀態時為空。已完成的圖片任務會在 data[].url 中返回生成的圖片 URL。
當您收到 status: "pending" 時,請使用 poll_url 或 GET /v1/tasks/{task_id} 來獲取結果。
請求
curl -X POST "https://api.tokenlab.sh/v1/images/generations" \
-H "Authorization: Bearer sk-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "gemini-3-pro-image",
"prompt": "A cinematic portrait of a white cat sitting on a rainy windowsill",
"aspect_ratio": "16:9",
"resolution": "2k"
}'from openai import OpenAI
client = OpenAI(
api_key="sk-your-api-key",
base_url="https://api.tokenlab.sh/v1"
)
response = client.images.generate(
model="gemini-3-pro-image",
prompt="A cinematic portrait of a white cat sitting on a rainy windowsill",
extra_body={"aspect_ratio": "16:9", "resolution": "2k"}
)
print(response.data[0].url)import OpenAI from 'openai';
const client = new OpenAI({
apiKey: 'sk-your-api-key',
baseURL: 'https://api.tokenlab.sh/v1'
});
const response = await client.images.generate({
model: 'gemini-3-pro-image',
prompt: 'A cinematic portrait of a white cat sitting on a rainy windowsill',
aspect_ratio: '16:9',
resolution: '2k'
});
console.log(response.data[0].url);<?php
$ch = curl_init('https://api.tokenlab.sh/v1/images/generations');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Authorization: Bearer sk-your-api-key'
],
CURLOPT_POSTFIELDS => json_encode([
'model' => 'gemini-3-pro-image',
'prompt' => 'A cinematic portrait of a white cat sitting on a rainy windowsill',
'aspect_ratio' => '16:9',
'resolution' => '2k'
])
]);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);
echo $data['data'][0]['url'];僅支援長寬比的圖片系列範例:對於 nano-banana,發送 aspect_ratio 但省略 resolution:
{
"model": "nano-banana",
"prompt": "A clean editorial product shot of a citrus soda can",
"aspect_ratio": "16:9"
}Nano Banana Pro 參考圖片範例:將請求發送至 /v1/images/generations,而非 /v1/images/edits。resolution 為選用,可設定為 1k、2k 或 4k:
{
"model": "nano-banana-pro",
"prompt": "Create a clean cinematic character image based on the reference images",
"operation": "image-to-image",
"image_urls": ["https://example.com/reference-1.png"],
"aspect_ratio": "1:1",
"resolution": "2k"
}針對私有或本地來源圖片的直接 multipart 上傳範例。請勿將 file_id 傳遞給 /v1/images/generations:
curl -X POST "https://api.tokenlab.sh/v1/images/generations" \
-H "Authorization: Bearer sk-your-api-key" \
-F "model=nano-banana-pro" \
-F "prompt=Create a clean cinematic character image based on this reference" \
-F "operation=image-to-image" \
-F "image=@reference.png" \
-F "aspect_ratio=1:1" \
-F "resolution=2k"回應
{
"created": 1706000000,
"data": [
{
"url": "https://...",
"revised_prompt": "A fluffy white cat with bright eyes sitting peacefully on a wooden windowsill, watching raindrops stream down the glass window..."
}
]
}選擇模型
查詢 GET /v1/models?recommended_for=image 或使用 模型頁面 以獲取當前的可用性、功能和價格。
請勿將模型硬編碼為始終同步或始終非同步。如果建立回應返回 status: "pending",請追蹤 poll_url 並輪詢直到完成。
處理基於任務的回應
對於圖片模型,請務必檢查回應是否包含 status: "pending" / status: "processing":
import requests
import time
def generate_image(prompt, model="gpt-image-2"):
# 建立圖片請求
response = requests.post(
"https://api.tokenlab.sh/v1/images/generations",
headers={"Authorization": "Bearer sk-your-api-key"},
json={"model": model, "prompt": prompt, "async": True},
timeout=120
)
response.raise_for_status()
data = response.json()
# 檢查是否為基於任務的請求
if data.get("status") in ("pending", "processing"):
task_id = data["task_id"]
poll_url = data.get("poll_url")
print(f"Image task started: {task_id}")
# 輪詢結果
while True:
status_resp = requests.get(
f"https://api.tokenlab.sh{poll_url}" if poll_url else f"https://api.tokenlab.sh/v1/tasks/{task_id}",
headers={"Authorization": "Bearer sk-your-api-key"},
timeout=30
)
status_resp.raise_for_status()
status_data = status_resp.json()
if status_data["status"] == "completed":
return status_data["data"][0]["url"]
elif status_data["status"] == "failed":
raise Exception(status_data.get("error", "Generation failed"))
time.sleep(3)
else:
# 內聯回應
return data["data"][0]["url"]
# 使用方式
url = generate_image("a beautiful sunset over mountains", model="gpt-image-2")
print(f"Generated image: {url}")Hy Image 3.5 Preview 可從文字建立 1024 像素的正方形圖片,並使用書面指令編輯參考圖片。它適用於草擬視覺效果和優化構圖。
{
"model": "hy-image-v3.5-preview",
"prompt": "A yellow lemon on a white table",
"size": "1024x1024",
"n": 1,
"response_format": "url"
}授權
BearerAuth API Key 驗證。請在 Dashboard > API > API Keys 建立或管理 API 金鑰。
位置: header
請求標頭
單次請求傳遞策略。會覆寫 API key 與 Workspace 的預設值。系統會自動優先嘗試 TokenLab Verified,並可能在輸出、請求接受或建立持久性資源前切換至 Official 模式一次。
可選值
- "auto"
- "verified"
- "official"
回應
application/json
application/json
application/json
application/json
application/json
application/json
application/json