> ## Documentation Index
> Fetch the complete documentation index at: https://dripart-chore-mintlify-theme-mint.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 将 FLUX 2 Pro 与 Comfy Router 配合使用

> 通过 Comfy Router 调用 bfl/flux-2-pro：端点、请求结构，以及 Router 返回的响应。

`bfl/flux-2-pro` 的 API 参考，由 Comfy Router 从 Black Forest Labs 提供。

## 快速开始

在[你的 Comfy 工作区](https://platform.comfy.org/profile/api-keys?onboarding=router)中创建一个密钥，并将其导出为 `COMFY_API_KEY`。Python、TypeScript 和 Swift 代码片段使用 Comfy SDK（`pip install comfy-sdk`、`npm install @comfyorg/sdk`，以及 [`ComfySwiftSDK`](https://github.com/Comfy-Org/comfy-swift-sdk) Swift 包）；cURL 代码片段是通过原始 HTTP 发起的相同调用。

**模型 ID：** `bfl/flux-2-pro`

**端点：** `POST https://api.comfy.org/v2/models/bfl/flux-2-pro`

<Tabs>
  <Tab title="等待结果">
    <CodeGroup>
      ```python Python theme={null}
      from comfy_sdk import Comfy

      # 从环境变量中读取 COMFY_API_KEY。
      # SDK 会自动创建幂等键，并在自动重试时复用它。
      with Comfy() as client:
          result = client.models.run(
              "bfl/flux-2-pro",
              {
                  "prompt": "A single red maple leaf on a plain white background.",
              },
          )

      print(result)
      ```

      ```typescript TypeScript theme={null}
      import { comfy } from "@comfyorg/sdk";

      // 从环境变量中读取 COMFY_API_KEY。
      // SDK 会自动创建幂等键，并在自动重试时复用它。
      const { data } = await comfy.models.run("bfl/flux-2-pro", {
        prompt: "A single red maple leaf on a plain white background.",
      });

      console.log(data);
      ```

      ```swift Swift theme={null}
      import Foundation
      import ComfySwiftSDK

      // 从环境变量中读取 COMFY_API_KEY。
      // SDK 为每次调用生成一个幂等键，并在自动重试时复用它。
      let client = ComfyCloudClient(apiKey: ProcessInfo.processInfo.environment["COMFY_API_KEY"]!)
      let result = try await client.models.run(
          "bfl/flux-2-pro",
          input: [
              "prompt": "A single red maple leaf on a plain white background.",
          ]
      )

      print(result.output)
      ```

      ```bash cURL theme={null}
      curl https://api.comfy.org/v2/models/bfl/flux-2-pro \
        -H "X-API-Key: $COMFY_API_KEY" \
        -H "Idempotency-Key: $(uuidgen)" \
        -H "Content-Type: application/json" \
        -d "{\"prompt\": \"A single red maple leaf on a plain white background.\"}"
      ```
    </CodeGroup>
  </Tab>

  <Tab title="排队并稍后收集">
    将相同的请求体发送到 `POST https://api.comfy.org/v2/models/bfl/flux-2-pro/requests`。运行被受理后，Router 会立即返回 `201` 和 `request_id`；结果就绪后，可以从本进程或其他进程收集。状态、取消与收集的细节见 [队列投递](/zh/development/comfy-router/queue)。

    <CodeGroup>
      ```python Python theme={null}
      from comfy_sdk import Comfy

      # 从环境变量中读取 COMFY_API_KEY。
      # 每次 submit() 调用都会生成自己的 Idempotency-Key，并在自动重试时复用它。
      with Comfy() as client:
          handle = client.models.submit(
              "bfl/flux-2-pro",
              {
                  "prompt": "A single red maple leaf on a plain white background.",
              },
          )
          print("request_id:", handle.request_id)  # 配合模型 ID，这就是另一个进程所需的全部信息

          # 轮询直到请求完成，按服务器指定的 Retry-After 等待。
          for update in handle.iter_events():
              print(update.status, update.queue_position)

          # 提供商自己的响应载荷，与 models.run() 返回的值相同。
          # 失败或已取消的请求会在此处抛出类型化的 Router 错误。
          result = handle.get()

      print(result)
      ```

      ```typescript TypeScript theme={null}
      import { comfy } from "@comfyorg/sdk";

      // 从环境变量中读取 COMFY_API_KEY。
      // 每次 submit() 调用都会生成自己的 Idempotency-Key，并在自动重试时复用它。
      const handle = await comfy.models.submit("bfl/flux-2-pro", {
        prompt: "A single red maple leaf on a plain white background.",
      });
      console.log("requestId:", handle.requestId); // 配合模型 ID，这就是另一个进程所需的全部信息

      // 轮询直到请求完成，按服务器指定的 Retry-After 等待。
      for await (const update of handle.events()) {
        console.log(update.status, update.queuePosition);
      }

      // 与 models.run() 返回的结果相同。失败或已取消的请求会在此处 reject。
      const result = await handle.get();

      console.log(result.data);
      ```

      ```swift Swift theme={null}
      import Foundation
      import ComfySwiftSDK

      // 从环境变量中读取 COMFY_API_KEY。
      // 每次 submit() 调用都会生成自己的 Idempotency-Key，并在自动重试时复用它。
      let client = ComfyCloudClient(apiKey: ProcessInfo.processInfo.environment["COMFY_API_KEY"]!)
      let handle = try await client.models.submit(
          "bfl/flux-2-pro",
          input: [
              "prompt": "A single red maple leaf on a plain white background.",
          ]
      )
      print("requestId:", handle.requestId)  // 配合模型 ID，这就是另一个进程所需的全部信息

      // 轮询直到请求完成，按服务器指定的 Retry-After 等待。
      for try await update in handle.events() {
          print(update.state.rawValue, update.queuePosition.map(String.init) ?? "unknown")
      }

      // 提供商自己的响应载荷，与 models.run() 返回的值相同。
      // 失败或已取消的请求会在此处抛出类型化的 Router 错误。
      let result = try await handle.result()

      print(result.output)
      ```

      ```bash cURL theme={null}
      # 1. 提交。Router 返回 201，以及 request_id、status_url、response_url 和 cancel_url。
      curl https://api.comfy.org/v2/models/bfl/flux-2-pro/requests \
        -H "X-API-Key: $COMFY_API_KEY" \
        -H "Idempotency-Key: $(uuidgen)" \
        -H "Content-Type: application/json" \
        -d "{\"prompt\": \"A single red maple leaf on a plain white background.\"}"

      # 2. 轮询直到状态为 COMPLETED，按每个响应指定的 Retry-After 秒数等待。
      REQUEST_ID="<request_id from the 201 body>"
      curl -i https://api.comfy.org/v2/models/bfl/flux-2-pro/requests/$REQUEST_ID/status \
        -H "X-API-Key: $COMFY_API_KEY"

      # 3. 收集。返回 200 及模型的原生输出；若仍在运行，则返回 202 及状态响应体。
      curl https://api.comfy.org/v2/models/bfl/flux-2-pro/requests/$REQUEST_ID \
        -H "X-API-Key: $COMFY_API_KEY"
      ```
    </CodeGroup>
  </Tab>
</Tabs>

## Schema

### 输入

<ParamField body="height" type="integer" default="1024">
  图像的高度。

  范围：`256` 到 `2048`
</ParamField>

<ParamField body="input_image" type="string">
  用于图生图的 Base64 编码图像。
</ParamField>

<ParamField body="input_image_2" type="string">
  用于图生图的 Base64 编码图像。
</ParamField>

<ParamField body="input_image_3" type="string">
  用于图生图的 Base64 编码图像。
</ParamField>

<ParamField body="input_image_4" type="string">
  用于图生图的 Base64 编码图像。
</ParamField>

<ParamField body="input_image_5" type="string">
  用于图生图的 Base64 编码图像。
</ParamField>

<ParamField body="input_image_6" type="string">
  用于图生图的 Base64 编码图像。
</ParamField>

<ParamField body="input_image_7" type="string">
  用于图生图的 Base64 编码图像。
</ParamField>

<ParamField body="input_image_8" type="string">
  用于图生图的 Base64 编码图像。
</ParamField>

<ParamField body="input_image_9" type="string">
  用于图生图的 Base64 编码图像。
</ParamField>

<ParamField body="output_format" type="string" default="&#x22;jpeg&#x22;">
  已生成图像的输出格式。

  可选值：`jpeg`、`png`
</ParamField>

<ParamField body="prompt" type="string" required>
  要生成的图像的文本描述。
</ParamField>

<ParamField body="prompt_upsampling" type="boolean" default="true">
  自动修改用于生成的提示词。
</ParamField>

<ParamField body="safety_tolerance" type="integer" default="2">
  审核阈值级别（仅 Flux 2 Max）。

  范围：`0` 到 `5`
</ParamField>

<ParamField body="seed" type="integer">
  用于结果可复现的种子。
</ParamField>

<ParamField body="width" type="integer" default="1024">
  图像的宽度。

  范围：`256` 到 `2048`
</ParamField>

由 Router 在 `GET /v2/models/bfl/flux-2-pro/openapi.json` 提供的 schema 生成，也就是它在请求到达提供商之前用于校验调用的同一份文档。

### 输出

<ResponseField name="cost" type="number">
  提供商报告的以积分计的成本，在任务就绪后填充。

  格式：`float`
</ResponseField>

<ResponseField name="id" type="string" required>
  BFL 任务标识符。
</ResponseField>

<ResponseField name="progress" type="number">
  BFL 报告的可选生成进度。

  范围：`0` 到 `1`

  格式：`float`
</ResponseField>

<ResponseField name="result" type="object" required>
  已完成的生成结果。此处不可为空：该组件的 `required` 条目是对 `200` 会携带结果的承诺，而可空的 `result` 会把它降格为仅检查键是否存在。
</ResponseField>

<ResponseField name="result.cost" type="number">
  提供商报告的本次生成成本。这是 BFL 的数字，不是 Comfy 的收费。

  格式：`double`
</ResponseField>

<ResponseField name="result.duration" type="number">
  提供商报告的生成时长，单位为秒。

  格式：`double`
</ResponseField>

<ResponseField name="result.end_time" type="number">
  提供商报告的生成完成时间，为自 Unix 纪元起的秒数。使用 `double` 的原因与 `start_time` 相同。

  格式：`double`
</ResponseField>

<ResponseField name="result.prompt" type="string">
  生成实际运行的提示词，即经过任何提示词上采样之后的结果。
</ResponseField>

<ResponseField name="result.sample" type="string (uri)">
  已生成资产的签名 URL。Router 会把该资产重新托管到 Comfy 存储上并改写此字段，因此它通常是 Comfy 托管的 URL，有效期最长 24 小时：签发时签 24 小时，并从一份 23 小时的备忘中重放，所以较晚的轮询可能返回一个只剩一小时的链接；如果某个叶子节点无法执行重新托管，则保留 BFL 自己的短时效分发 URL：视频大约两小时，图像大约十分钟。无论哪种情况链接都会过期，因此请下载资产，而不要保存 URL。

  格式：`uri`
</ResponseField>

<ResponseField name="result.seed" type="integer">
  本次生成使用的种子，无论是由用户提供还是由提供商选定。声明为 `int64` 是因为 BFL 会返回超过 2^31 的种子（例如 2784347701），而未加格式修饰的 `integer` 在许多 SDK 生成器中会生成 32 位字段。

  格式：`int64`
</ResponseField>

<ResponseField name="result.start_time" type="number">
  提供商报告的生成开始时间，为自 Unix 纪元起的秒数。是 `double` 而不是 `float`：在当前日期的纪元值附近，float32 的间距约为 128 秒，这会把整个生成的跨度压缩成单个解码值。

  格式：`double`
</ResponseField>

<ResponseField name="status" type="string" required>
  任务状态：Pending、Reasoning、Generating、Ready、Request Moderated、Content Moderated、Error 或 Task not found。
</ResponseField>

## 示例

### 输入

```json theme={null}
{
  "prompt": "A single red maple leaf on a plain white background."
}
```

### 输出

```json theme={null}
{
  "cost": null,
  "id": "b2e0c1a4-0f2f-4a55-9f2e-2f9a1c0d4e77",
  "progress": null,
  "result": {
    "cost": null,
    "duration": 3.4,
    "end_time": 1767225603.4,
    "prompt": "A watercolor painting of a lighthouse at dawn, soft light on the water",
    "sample": "https://example.invalid/bfl/flux-pro-1.1/sample.png",
    "seed": 2784347701,
    "start_time": 1767225600
  },
  "status": "Ready"
}
```

## 发布前须知

SDK 会生成 `Idempotency-Key` 并在自动重试中复用它。手动重试时，请复用原始 key。Router 最长可保持连接 10 分钟。

请求失败时，Router 会发送 `X-Comfy-Error-Type` 响应头说明原因。`422` 表示 Router 在调用提供商之前就拒绝了输入，`413` 表示请求体超出了 Router 可接受的大小。已生成的资源请及时下载，因为[结果 URL 会过期](/zh/development/comfy-router/reference#结果资产)。

上文任何字段描述中提到的尺寸限制，都是提供商对该字段自身的限定，引自提供商的规范。Router 会对整个请求体另行设置上限，base64 编码的媒体内容也计入其中：参见[请求体大小](/zh/development/comfy-router/limitations)。

本页记录的是通过 Comfy Router 调用的某一个合作伙伴模型。同一个 `comfy-sdk` / `@comfyorg/sdk` 包还提供第二个客户端，用于在 Comfy Cloud 上运行完整的 ComfyUI 工作流图：`Comfy(api_key=...)` / `new Comfy({ apiKey })`，并带有 `client.workflows`、`client.assets` 和 `client.jobs`。请参阅 [Comfy SDKs](/zh/development/api-development/sdks)。

<CardGroup cols={3}>
  <Card title="请求头" icon="list" href="/zh/development/comfy-router/headers">
    身份验证、幂等性、请求 ID、错误分类、重试节奏、消费限额。
  </Card>

  <Card title="使用 Router API" icon="code" href="/zh/development/comfy-router/api">
    模型发现、验证错误、重试与计费。
  </Card>

  <Card title="限制" icon="triangle-exclamation" href="/zh/development/comfy-router/limitations">
    Router 目前不支持的功能，以及替代方案。
  </Card>
</CardGroup>
