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

# 将 SwitchX 与 Comfy Router 配合使用

> 通过 Comfy Router 调用 beeble/switchx：端点、请求结构以及 Router 返回的响应。

`beeble/switchx` 的 API 参考，由 Comfy Router 提供，来自 Beeble。

## 快速开始

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

**模型 ID：** `beeble/switchx`

**端点：** `POST https://api.comfy.org/v2/models/beeble/switchx`

<Tabs>
  <Tab title="Wait for the result">
    <CodeGroup>
      ```python Python theme={null}
      from comfy_sdk import Comfy

      # Reads COMFY_API_KEY from the environment.
      # The SDK automatically creates an idempotency key and reuses it for automatic retries.
      with Comfy() as client:
          result = client.models.run(
              "beeble/switchx",
              {
                  "alpha_mode": "auto",
                  "generation_type": "image",
                  "max_resolution": 720,
                  "prompt": "A cinematic product photo of a glass lamp on a marble table",
                  "source_uri": "https://img.freepik.com/free-photo/portrait-woman_395237-33.jpg?w=740&q=80",
              },
          )

      print(result)
      ```

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

      // Reads COMFY_API_KEY from the environment.
      // The SDK automatically creates an idempotency key and reuses it for automatic retries.
      const { data } = await comfy.models.run("beeble/switchx", {
        alpha_mode: "auto",
        generation_type: "image",
        max_resolution: 720,
        prompt: "A cinematic product photo of a glass lamp on a marble table",
        source_uri: "https://img.freepik.com/free-photo/portrait-woman_395237-33.jpg?w=740&q=80",
      });

      console.log(data);
      ```

      ```bash cURL theme={null}
      curl https://api.comfy.org/v2/models/beeble/switchx \
        -H "X-API-Key: $COMFY_API_KEY" \
        -H "Idempotency-Key: $(uuidgen)" \
        -H "Content-Type: application/json" \
        -d "{\"alpha_mode\": \"auto\", \"generation_type\": \"image\", \"max_resolution\": 720, \"prompt\": \"A cinematic product photo of a glass lamp on a marble table\", \"source_uri\": \"https://img.freepik.com/free-photo/portrait-woman_395237-33.jpg?w=740&q=80\"}"
      ```
    </CodeGroup>
  </Tab>

  <Tab title="Queue and collect later">
    <CodeGroup>
      ```python Python theme={null}
      from comfy_sdk import Comfy

      # Reads COMFY_API_KEY from the environment.
      # Each submit() call mints its own Idempotency-Key and reuses it for automatic retries.
      with Comfy() as client:
          handle = client.models.submit(
              "beeble/switchx",
              {
                  "alpha_mode": "auto",
                  "generation_type": "image",
                  "max_resolution": 720,
                  "prompt": "A cinematic product photo of a glass lamp on a marble table",
                  "source_uri": "https://img.freepik.com/free-photo/portrait-woman_395237-33.jpg?w=740&q=80",
              },
          )
          print("request_id:", handle.request_id)  # with the model ID, all another process needs

          # Poll until the request completes, waiting the Retry-After the server names.
          for update in handle.iter_events():
              print(update.status, update.queue_position)

          # The provider's own payload, the same value models.run() returns.
          # A request that failed or was cancelled raises the typed Router error here.
          result = handle.get()

      print(result)
      ```

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

      // Reads COMFY_API_KEY from the environment.
      // Each submit() call mints its own Idempotency-Key and reuses it for automatic retries.
      const handle = await comfy.models.submit("beeble/switchx", {
        alpha_mode: "auto",
        generation_type: "image",
        max_resolution: 720,
        prompt: "A cinematic product photo of a glass lamp on a marble table",
        source_uri: "https://img.freepik.com/free-photo/portrait-woman_395237-33.jpg?w=740&q=80",
      });
      console.log("requestId:", handle.requestId); // with the model ID, all another process needs

      // Poll until the request completes, waiting the Retry-After the server names.
      for await (const update of handle.events()) {
        console.log(update.status, update.queuePosition);
      }

      // The same result models.run() returns. A request that failed or was cancelled rejects here.
      const result = await handle.get();

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

      ```bash cURL theme={null}
      # 1. Submit. Router answers 201 with request_id, status_url, response_url and cancel_url.
      curl https://api.comfy.org/v2/models/beeble/switchx/requests \
        -H "X-API-Key: $COMFY_API_KEY" \
        -H "Idempotency-Key: $(uuidgen)" \
        -H "Content-Type: application/json" \
        -d "{\"alpha_mode\": \"auto\", \"generation_type\": \"image\", \"max_resolution\": 720, \"prompt\": \"A cinematic product photo of a glass lamp on a marble table\", \"source_uri\": \"https://img.freepik.com/free-photo/portrait-woman_395237-33.jpg?w=740&q=80\"}"

      # 2. Poll until status is COMPLETED, waiting the Retry-After seconds each response names.
      REQUEST_ID="<request_id from the 201 body>"
      curl -i https://api.comfy.org/v2/models/beeble/switchx/requests/$REQUEST_ID/status \
        -H "X-API-Key: $COMFY_API_KEY"

      # 3. Collect. 200 with the model's native output, 202 with the status body while it is still running.
      curl https://api.comfy.org/v2/models/beeble/switchx/requests/$REQUEST_ID \
        -H "X-API-Key: $COMFY_API_KEY"
      ```
    </CodeGroup>
  </Tab>
</Tabs>

## Schema

### Input

<ParamField body="alpha_mode" type="string" required>
  透明度模式：自动、填充、自定义或选择

  可选值：`auto`、`fill`、`custom`、`select`
</ParamField>

<ParamField body="alpha_uri" type="string">
  自定义透明度蒙版的 URI。当 alpha\_mode 为 custom 或 select 时必填。在 auto 或 fill 模式下会被忽略。
</ParamField>

<ParamField body="callback_url" type="string">
  用于在任务完成或失败时接收 webhook 通知的 HTTPS URL。
</ParamField>

<ParamField body="generation_type" type="string" required>
  输出类型：图像或视频

  可选值：`image`、`video`
</ParamField>

<ParamField body="idempotency_key" type="string">
  用于安全重试的幂等键。如果你的账号下已存在使用相同键的任务，API 会返回现有任务的状态，而不会创建重复任务。
</ParamField>

<ParamField body="max_resolution" type="integer" default="1080">
  最大输出分辨率：720 或 1080（默认值：1080）。
</ParamField>

<ParamField body="prompt" type="string">
  期望输出的文本描述（最多 2,000 个字符）。prompt 与 reference\_image\_uri 至少需要提供一个。
</ParamField>

<ParamField body="reference_image_uri" type="string">
  用于风格迁移的参考图像 URI。接受的 URI 类型与 source\_uri 相同。
</ParamField>

<ParamField body="source_uri" type="string" required>
  来源图像或视频的 URI。接受 beeble://uploads/\{id}/\{filename}、https URL 或 data:\{mime};base64 URI（最大 50 MB）。
</ParamField>

本文档由 Router 在 `GET /v2/models/beeble/switchx/openapi.json` 提供的 schema 生成，Router 在请求到达提供商之前，正是用这份文档来校验调用。

### Output

<ResponseField name="alpha_mode" type="string">
  自动、填充、自定义或选择
</ResponseField>

<ResponseField name="completed_at" type="string">
  任务完成或失败时的 ISO 8601 时间戳。
</ResponseField>

<ResponseField name="created_at" type="string">
  任务创建时的 ISO 8601 时间戳。
</ResponseField>

<ResponseField name="error" type="string">
  报错信息（status 为 failed 时存在）。
</ResponseField>

<ResponseField name="generation_type" type="string">
  图像或视频
</ResponseField>

<ResponseField name="id" type="string" required>
  任务标识符（swx\_...）
</ResponseField>

<ResponseField name="modified_at" type="string">
  最后一次状态变更的 ISO 8601 时间戳。
</ResponseField>

<ResponseField name="output" type="object">
  SwitchX 任务输出的签名 URL。
</ResponseField>

<ResponseField name="output.alpha" type="string">
  透明度蒙版 URL。
</ResponseField>

<ResponseField name="output.render" type="string">
  合成后的输出 URL。
</ResponseField>

<ResponseField name="output.source" type="string">
  预处理后的来源 URL。
</ResponseField>

<ResponseField name="progress" type="integer">
  进度百分比（0-100）。
</ResponseField>

<ResponseField name="status" type="string" required>
  当前任务状态。

  可选值：`in_queue`、`processing`、`completed`、`failed`
</ResponseField>

<ResponseField name="webhook" type="object">
  SwitchX 任务的 webhook 投递状态。
</ResponseField>

<ResponseField name="webhook.attempts" type="integer">
  到目前为止的投递尝试次数。
</ResponseField>

<ResponseField name="webhook.last_error" type="string">
  最后一次失败投递尝试的报错信息。
</ResponseField>

<ResponseField name="webhook.status" type="string">
  待定、已投递或失败
</ResponseField>

## 示例

### 输入

```json theme={null}
{
  "alpha_mode": "auto",
  "generation_type": "image",
  "max_resolution": 720,
  "prompt": "A cinematic product photo of a glass lamp on a marble table",
  "source_uri": "https://img.freepik.com/free-photo/portrait-woman_395237-33.jpg?w=740&q=80"
}
```

### 输出

```json theme={null}
{
  "alpha_mode": "auto",
  "completed_at": "2027-01-01T00:01:04Z",
  "created_at": "2027-01-01T00:00:00Z",
  "generation_type": "image",
  "id": "swx_1a2b3c4d5e6f7a8b",
  "modified_at": "2027-01-01T00:01:04Z",
  "output": {
    "alpha": "https://example.invalid/beeble/switchx/alpha.png",
    "render": "https://example.invalid/beeble/switchx/render.png",
    "source": "https://example.invalid/beeble/switchx/source.png"
  },
  "progress": 100,
  "status": "completed"
}
```

## 发布前须知

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

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

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

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

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