搜索

搜索 HiAPI 模型、工具和资源。

返回博客
教程3 分钟阅读

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

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

HiAPI 编辑部FLUX.2接入教程API

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:

FLUX.2 Pro 文生图:俯拍的开发者桌面,机械键盘、咖啡、多肉,笔记本上手写着 API ready

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仅 custom256–2048自动取整到 16 的倍数
output_format否webp(默认)/ jpg / png
output_quality否0–100,默认 80只对 webp、jpg 生效
seed否整数固定后可复现同一结果
safety_tolerance否1–5,默认 21 最严格

参数是严格校验的:多传一个文档里没有的字段,会直接返回 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。

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

现在就用 HiAPI 生成

选一个模型,输入你的提示词,直接查看生成结果。

HiAPI Blog

相关文章

HiAPI

现在就用 HiAPI 生成

开始生成