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(文生图)
接口地址
POST /api/v1/image/task
身份验证
Authorization: Bearer YOUR_API_KEY
请求参数
必需参数
注意: width:height 比例必须是以下之一:"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(图生图)
接口地址
POST /api/v1/image/task
请求参数
必需参数
注意: width:height 比例必须是以下之一:"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(图生图模式)
{
"code": 400,
"message": "image_url_list is required for flux-kontext-pro"
}
图像数量过多
{
"code": 400,
"message": "Maximum 5 images allowed"
}
最佳实践
文生图技巧
- 具体描述: 包含主题、风格、光照、构图细节
- 宽高比选择: 根据使用场景选择比例(网页用 16:9,移动端用 9:16)
- 提示词质量: 更详细的提示词通常能产生更好的结果
- 高效轮询: 每 2-3 秒检查一次状态
图生图技巧
- 清晰指令: 明确说明期望的转换效果
- 多图上下文: 可以组合最多 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
}