HiAPI
博客

教程

FLUX.2 文生图 API 怎么用?参数、代码和一张真实出图

FLUX.2 Pro 文生图的参数表、curl 和 Python 示例、四档分辨率价格、seed 复现和上线注意事项。

2026年10月18日 3
FLUX.2 文生图 API 怎么用?参数、代码和一张真实出图
1 MP
$0.05
4 MP
$0.125
比例
10 种

TL;DR

  • FLUX.2 Pro 文生图在 HiAPI 上的模型名是 flux-2/text-to-image,Black Forest Labs 出品,提示词遵循和图内文字是强项。打开模型页就能在线试。
  • 按分辨率档计费:0.5 MP $0.0375,1 MP(默认)$0.05,2 MP $0.075,4 MP $0.125。比例不影响价格。
  • 最快的调法:POST /v1/tasks 传 prompt,轮询到 success,下载 output[0].url。resolution 的值中间有空格,要写成 "1 MP"。

先看一张真实出图

下面这张就是用文中的请求生成的,2 MP、16:9:

A wide overhead flat-lay of a modern developer desk: a matte black mechanical
keyboard on the left, a ceramic mug of steaming coffee on the right, a small
potted succulent in the top corner, and an open spiral notebook in the center
with the handwritten words 'API ready' clearly visible on the page, soft
natural window light falling from the left, a muted teal and warm wood color
palette, no other text or logos anywhere in the frame

用这个提示词生成 →(16:9、2 MP)

flux-2/text-to-image,16:9,2 MP,$0.075。笔记本上的「API ready」写对了,画面里没有多余的字。

参数速查

字段 必填 取值 说明
prompt 是 字符串 重要元素放句首,30–80 个英文词效果最好
aspect_ratio 否 1:1 16:9 3:2 2:3 4:5 5:4 9:16 3:4 4:3 custom,默认 1:1 选 custom 时配合 width / height
resolution 否 0.5 MP / 1 MP / 2 MP / 4 MP,默认 1 MP 决定价格;custom 时忽略
width / height 仅 custom 256–2048 自动取整到 16 的倍数
output_format 否 webp(默认)/ jpg / png
output_quality 否 0–100,默认 80 只对 webp、jpg 生效
seed 否 整数 固定后可复现同一结果
safety_tolerance 否 1–5,默认 2 1 最严格

参数是严格校验的:多传一个文档里没有的字段,会直接返回 HTTP 400(additional properties ... not allowed)。这个入口只收文字,要带参考图改图,用 flux-2/image-to-image。

curl:跑通第一张

curl -X POST https://api.hiapi.ai/v1/tasks \
  -H "Authorization: Bearer $HIAPI_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "flux-2/text-to-image",
    "input": {
      "prompt": "A matte charcoal coffee bag on a rustic oak counter, kraft paper label reading DAYBREAK ROAST, warm morning window light",
      "aspect_ratio": "4:3",
      "resolution": "1 MP",
      "output_format": "png"
    }
  }'

返回里只有 data.taskId,图还没生成。轮询 GET /v1/tasks/{taskId},status 变成 success 后从 output[0].url 下载。

Python:提交、轮询、下载

# pip install requests; export HIAPI_KEY=你的 Key
import os, time, uuid, requests

API = "https://api.hiapi.ai/v1/tasks"
H = {"Authorization": f"Bearer {os.environ['HIAPI_KEY']}"}

def generate(timeout_s=300, **inp):
    # 重试同一次提交时复用同一个 Idempotency-Key,只会扣一次费
    r = requests.post(API, json={"model": "flux-2/text-to-image", "input": inp},
                      headers={**H, "Idempotency-Key": str(uuid.uuid4())}, timeout=30)
    r.raise_for_status()
    task_id = r.json()["data"]["taskId"]
    deadline = time.time() + timeout_s
    while time.time() < deadline:
        d = requests.get(f"{API}/{task_id}", headers=H, timeout=30).json()["data"]
        if d["status"] == "success":
            return d["output"][0]["url"]
        if d["status"] in ("fail", "failed"):
            raise RuntimeError(d)
        time.sleep(5)
    raise TimeoutError(task_id)

url = generate(
    prompt="A wide overhead flat-lay of a modern developer desk, an open notebook with the handwritten words 'API ready', soft window light",
    aspect_ratio="16:9", resolution="2 MP", output_format="png", seed=42,
)
with open("desk.png", "wb") as f:
    f.write(requests.get(url).content)

多少钱

分辨率 每张 100 张
0.5 MP $0.0375 $3.75
1 MP(默认) $0.05 $5.00
2 MP $0.075 $7.50
4 MP $0.125 $12.50

HiAPI 现价,2026 年 10 月 18 日核对。

比例、seed、输出格式都不影响价格,只看分辨率档。失败不扣费。想更便宜,FLUX.2 [klein] 4B 1 MP 每张 $0.00143,各档对比见 FLUX.2 API 价格。

上线前注意

  • resolution 中间有空格:写 "1 MP",不是 "1MP" 或 "1K",写错会返回 400 并列出可选值。
  • custom 尺寸会忽略 resolution:aspect_ratio 写 custom 时,按 width × height 出图。
  • 要复现就固定 seed:同一提示词、同一 seed,结果高度一致,方便微调措辞时做对照。
  • 结果马上保存:output[0].url 有有效期,下载后存到自己的存储。
  • 用回调代替轮询:在 input 同级加 "callback": {"url": "https://your-app.example.com/hiapi/callback"},按 taskId 去重。

常见问题

resolution 必须写吗?

不必须,默认 1 MP。但建议每次都写,避免和 4 MP 弄混。

能传参考图吗?

这个入口不能。带参考图改图、多图合成用 flux-2/image-to-image,最多 8 张参考图,现成提示词见 FLUX.2 改图提示词。

为什么返回「additional properties not allowed」?

传了这个模型没有的字段,比如 strength、prompt_upsampling。只用上面参数表里的字段。

和 FLUX.3 Image 怎么选?

FLUX.3 Image 一个模型名同时生图和改图、能出 4K、最多 10 张参考图;FLUX.2 Pro 有 seed 和自定义宽高,4 MP 只要 $0.125。同提示词对照见 FLUX.3 Image vs FLUX.2。

复制上面的桌面提示词,去模型页生成第一张。

立即开始构建持久 AI 产物

获取 API 密钥,通过一个基于任务(task)的 API 调用图像、视频和音频模型。

阅读 API 文档