Flux Kontext Pro API
Flux Kontext Pro 的完整 API 參考,具備多圖片上下文理解能力。
模型概覽
快速比較
支援的長寬比
兩種模式都支援 9 種長寬比:
1:1, 16:9, 9:16, 3:2, 2:3, 4:3, 3:4, 21:9, 9:21
費用
Flux Kontext Pro (Text-to-Image)
端點
POST /api/v1/image/task
驗證
Authorization: Bearer YOUR_API_KEY
請求參數
必填
注意: 寬度:高度比例必須是以下其中之一:"1:1", "16:9", "9:16", "3:2", "2:3", "4:3", "3:4", "21:9", "9:21"
請求範例
const response = await fetch('https://api.flaq.ai/api/v1/image/task', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({
model_name: 'flux-kontext-pro',
prompt: 'A serene mountain landscape at sunset',
width: 16,
height: 9
})
});
const { data } = await response.json();
const taskId = data.task_id;
const pollResponse = await fetch(
`https://api.flaq.ai/api/v1/image/${taskId}`,
{
headers: { 'Authorization': 'Bearer YOUR_API_KEY' }
}
);
const result = await pollResponse.json();
console.log(result.data.task_result.images[0].url);
Flux Kontext Pro (Image-to-Image)
端點
POST /api/v1/image/task
請求參數
必填
注意: 寬度:高度比例必須是以下其中之一:"1:1", "16:9", "9:16", "3:2", "2:3", "4:3", "3:4", "21:9", "9:21"
單張圖片編輯範例
{
model_name: 'flux-kontext-pro',
prompt: 'Transform this photo into a watercolor painting',
width: 16,
height: 9,
image_url_list: ['https://example.com/image.jpg']
}
多圖片合成範例
{
model_name: 'flux-kontext-pro',
prompt: 'Combine these images into a cohesive landscape scene',
width: 16,
height: 9,
image_url_list: ['https://example.com/image1.jpg', 'https://example.com/image2.jpg', 'https://example.com/image3.jpg']
}
回應格式
初始回應
兩種模式都會回傳用於輪詢的 task_id:
{
"code": 0,
"data": {
"task_id": "{task_id}",
"task_status": "submitted",
"response_url": "https://api.flaq.ai/api/v1/image/{task_id}"
},
"message": "success"
}
輪詢回應
輪詢 /api/v1/image/{taskId} 以檢查狀態:
{
"code": 0,
"data": {
"task_id": "{task_id}",
"task_status": "succeed",
"task_status_msg": null,
"response_url": "https://api.flaq.ai/api/v1/image/{task_id}",
"task_result": {
"credit": 0.04,
"images": [
{
"url": "https://example.com/generated-image.jpeg"
}
]
}
},
"message": "success"
}
狀態值
submitted:任務已接受
processing:生成進行中
succeed:生成成功完成
failed:生成失敗
錯誤處理
常見錯誤
無效的長寬比
{
"code": 400,
"message": "Ratio must be one of: 1:1, 16:9, 9:16, 3:2, 2:3, 4:3, 3:4, 21:9, 9:21"
}
缺少圖片 URL(Image-to-Image 模式)
{
"code": 400,
"message": "image_url_list is required for flux-kontext-pro"
}
圖片數量過多
{
"code": 400,
"message": "Maximum 5 images allowed"
}
最佳實踐
Text-to-Image 技巧
- 保持具體: 包含主體、風格、光線和構圖細節
- 長寬比選擇: 根據你的使用情境選擇比例(16:9 適合網頁,9:16 適合行動端)
- 提示詞品質: 更詳細的提示詞通常能產生更好的結果
- 高效輪詢: 每 2-3 秒檢查一次狀態
Image-to-Image 技巧
- 清楚指令: 明確說明期望的轉換
- 多圖片上下文: 可結合最多 5 張圖片進行上下文感知生成
- 提示詞引導: 描述期望結果,而不只是描述變更
- 圖片品質: 更高品質的輸入圖片會產生更好的結果
使用案例範例
產品攝影
{
model_name: 'flux-kontext-pro',
prompt: 'Professional product shot of luxury watch on marble surface',
width: 4,
height: 3
}
多圖片拼貼
{
model_name: 'flux-kontext-pro',
image_url_list: ['photo1.jpg', 'photo2.jpg', 'photo3.jpg'],
prompt: 'Create a seamless collage with these vacation photos',
width: 16,
height: 9
}
風格轉換
{
model_name: 'flux-kontext-pro',
image_url_list: ['photo.jpg'],
prompt: 'Transform into impressionist painting style',
width: 1,
height: 1
}