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

# Comfy Router 快速入门

> 从零开始，大约五分钟内，使用 Python 和 TypeScript，通过 Comfy Router 生成一张图像。

<div className="router-quickstart-marker" />

Comfy Router 让您用一个 Comfy API 密钥，通过 `https://api.comfy.org` 调用合作伙伴模型。将模型的输入发送到 `POST /v2/models/{provider}/{model}`，然后等待已完成的结果。本示例使用 `bfl/flux-2-pro` 生成一张图像。

<Steps>
  <Step title="创建 API 密钥">
    在[您的 Comfy 工作区](https://platform.comfy.org/profile/api-keys)中创建一个密钥。在兼容 Bash 的终端中，设置：

    ```bash theme={null}
    export COMFY_API_KEY="comfyui-..."
    ```

    请将 API 密钥保存在您的服务器或本地环境中。这些示例面向终端或服务器，而不是浏览器 JavaScript。
  </Step>

  <Step title="为本次请求保存一个密钥">
    为这张图像生成一次该值。如果重试同一个请求，请复用它。

    ```bash theme={null}
    export COMFY_REQUEST_KEY="$(uuidgen)"
    ```

    如果 `uuidgen` 不可用，请使用其他 UUID 生成器。开始生成新图像时，请使用新的密钥。
  </Step>

  <Step title="生成一张图像">
    选择您的语言并运行示例。图像生成可能需要几分钟。

    <CodeGroup>
      ```bash cURL theme={null}
      curl --max-time 660 \
        https://api.comfy.org/v2/models/bfl/flux-2-pro \
        -H "X-API-Key: $COMFY_API_KEY" \
        -H "Idempotency-Key: $COMFY_REQUEST_KEY" \
        -H "Content-Type: application/json" \
        -d '{"prompt": "a red teapot on a windowsill, morning light"}'
      ```

      ```python Python theme={null}
      # Python 3.10+
      # Install: python -m pip install "comfy-sdk>=0.1.9"
      # Save as quickstart.py, then run: python quickstart.py

      import os

      from comfy_sdk import Comfy

      # Comfy reads COMFY_API_KEY from the environment.
      with Comfy() as client:
          result = client.models.run(
              "bfl/flux-2-pro",
              {"prompt": "a red teapot on a windowsill, morning light"},
              idempotency_key=os.environ["COMFY_REQUEST_KEY"],
              timeout=660.0,
          )

      print("image:", result["result"]["sample"])
      ```

      ```typescript TypeScript theme={null}
      // Node.js 22+
      // Install: npm install @comfyorg/sdk@^0.1.9 --save-dev tsx
      // Save as quickstart.mts, then run: npx tsx quickstart.mts

      import { comfy } from "@comfyorg/sdk";

      // Comfy reads COMFY_API_KEY from the environment.
      type FluxResult = { result: { sample: string } };
      const idempotencyKey = process.env.COMFY_REQUEST_KEY;
      if (!idempotencyKey) throw new Error("Set COMFY_REQUEST_KEY first.");

      const { data } = await comfy.models.run<FluxResult>(
        "bfl/flux-2-pro",
        { prompt: "a red teapot on a windowsill, morning light" },
        { idempotencyKey, timeoutMs: 660_000 },
      );

      console.log("image:", data.result.sample);
      ```
    </CodeGroup>

    两个 SDK 都会从环境中读取 `COMFY_API_KEY`。
  </Step>

  <Step title="读取并保存结果">
    对于该模型，图片网址位于响应体中的 `result.sample`。简略的响应如下所示；下面的网址仅作示意：

    ```json theme={null}
    {
      "status": "Ready",
      "result": { "sample": "https://example.com/generated-image.jpeg" }
    }
    ```

    打开返回的网址，或将其下载：

    ```bash theme={null}
    curl --fail --location "PASTE_IMAGE_URL_HERE" --output teapot.jpg
    ```

    请及时下载。Router 可以将 BFL 资产转存到 Comfy 存储上，但网址会过期，重放并不会为其续期。转存失败可能留下一个有效期更短的提供商网址。参见[结果资产](/zh/development/comfy-router/reference#结果资产)。
  </Step>
</Steps>

## 无需等待，使用队列

`run` 会保持连接直到图像就绪。若想立即拿回 `request_id` 并在之后（从当前进程或其他进程）收集结果，请改为调用 `submit`（`comfy-sdk` 与 `@comfyorg/sdk` 0.3.0 或更高版本），或通过 HTTP 将相同的请求体发送到 `POST /v2/models/{provider}/{model}/requests`。每个模型页面在同步代码片段旁都有一个 **Queue and collect later** 标签页，[队列投递](/zh/development/comfy-router/queue)详细介绍了状态、取消与结果收集。队列投递正在按工作区逐步推出。

## 选择模型

[浏览 Comfy Router 提供的模型](/zh/development/comfy-router/models)，查看它们的输入，然后替换本示例中的模型 ID。
