> ## 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.

# Comfy Router で Krea 2 を使う

> Comfy Router 経由で krea/krea-2 を呼び出します。エンドポイント、リクエストの形状、Router が返すレスポンスについて説明します。

`krea/krea-2` の API リファレンス。Krea から Comfy Router が提供します。

## クイックスタート

[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:** `krea/krea-2`

**エンドポイント:** `POST https://api.comfy.org/v2/models/krea/krea-2`

<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(
              "krea/krea-2",
              {
                  "aspect_ratio": "1:1",
                  "prompt": "a red circle",
                  "resolution": "1K",
              },
          )

      print(result)
      ```

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

      // 環境変数から COMFY_API_KEY を読み取ります。
      // SDK は自動的に冪等キーを作成し、自動リトライのために再利用します。
      const { data } = await comfy.models.run("krea/krea-2", {
        aspect_ratio: "1:1",
        prompt: "a red circle",
        resolution: "1K",
      });

      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(
          "krea/krea-2",
          input: [
              "aspect_ratio": "1:1",
              "prompt": "a red circle",
              "resolution": "1K",
          ]
      )

      print(result.output)
      ```

      ```bash cURL theme={null}
      curl https://api.comfy.org/v2/models/krea/krea-2 \
        -H "X-API-Key: $COMFY_API_KEY" \
        -H "Idempotency-Key: $(uuidgen)" \
        -H "Content-Type: application/json" \
        -d "{\"aspect_ratio\": \"1:1\", \"prompt\": \"a red circle\", \"resolution\": \"1K\"}"
      ```
    </CodeGroup>
  </Tab>

  <Tab title="キューに送信して後で収集">
    同じボディを `POST https://api.comfy.org/v2/models/krea/krea-2/requests` に送信します。Router は実行が受け付けられ次第 `201` と `request_id` を返し、結果は準備が整った時点で、このプロセスからでも別のプロセスからでも取得できます。ステータス、キャンセル、結果の取得の詳細は [キュー配信](/ja/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(
              "krea/krea-2",
              {
                  "aspect_ratio": "1:1",
                  "prompt": "a red circle",
                  "resolution": "1K",
              },
          )
          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("krea/krea-2", {
        aspect_ratio: "1:1",
        prompt: "a red circle",
        resolution: "1K",
      });
      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(
          "krea/krea-2",
          input: [
              "aspect_ratio": "1:1",
              "prompt": "a red circle",
              "resolution": "1K",
          ]
      )
      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 は request_id、status_url、response_url、cancel_url とともに 201 を返します。
      curl https://api.comfy.org/v2/models/krea/krea-2/requests \
        -H "X-API-Key: $COMFY_API_KEY" \
        -H "Idempotency-Key: $(uuidgen)" \
        -H "Content-Type: application/json" \
        -d "{\"aspect_ratio\": \"1:1\", \"prompt\": \"a red circle\", \"resolution\": \"1K\"}"

      # 2. ステータスが COMPLETED になるまでポーリングし、各レスポンスが指定する Retry-After の秒数だけ待機します。
      REQUEST_ID="<request_id from the 201 body>"
      curl -i https://api.comfy.org/v2/models/krea/krea-2/requests/$REQUEST_ID/status \
        -H "X-API-Key: $COMFY_API_KEY"

      # 3. 収集。モデルのネイティブ出力とともに 200、まだ実行中はステータスボディとともに 202 を返します。
      curl https://api.comfy.org/v2/models/krea/krea-2/requests/$REQUEST_ID \
        -H "X-API-Key: $COMFY_API_KEY"
      ```
    </CodeGroup>
  </Tab>
</Tabs>

## スキーマ

### 入力

<ParamField body="aspect_ratio" type="string" required>
  アスペクト比。次のいずれか: 1:1、4:3、3:2、16:9、2.35:1、4:5、2:3、9:16。

  指定可能な値: `1:1`、`4:3`、`3:2`、`16:9`、`2.35:1`、`4:5`、`2:3`、`9:16`
</ParamField>

<ParamField body="creativity" type="string" default="&#x22;medium&#x22;">
  プロンプト解釈の強度: raw=0、low=10、medium=50、high=100。

  指定可能な値: `raw`、`low`、`medium`、`high`
</ParamField>

<ParamField body="image_style_references" type="object[]">
  生成に使用するスタイル参照
</ParamField>

<ParamField body="image_style_references[].strength" type="number" required>
  範囲: `-2` ～ `2`

  フォーマット: `double`
</ParamField>

<ParamField body="image_style_references[].url" type="string (uri)">
  フォーマット: `uri`
</ParamField>

<ParamField body="moodboards" type="object[]">
  生成に使用するムードボード。現在は1つのムードボードに制限されています。
</ParamField>

<ParamField body="moodboards[].id" type="string (uuid)" required>
  フォーマット: `uuid`
</ParamField>

<ParamField body="moodboards[].strength" type="number" default="0.35">
  範囲: `-0.5` ～ `1.5`

  フォーマット: `double`
</ParamField>

<ParamField body="prompt" type="string" required />

<ParamField body="resolution" type="string" required>
  解像度スケール。次のいずれか: 1K。

  指定可能な値: `1K`
</ParamField>

<ParamField body="seed" type="number" />

<ParamField body="styles" type="object[]">
  生成に使用するスタイル（通常はLoRA）
</ParamField>

<ParamField body="styles[].id" type="string" required />

<ParamField body="styles[].strength" type="number" required>
  範囲: `-2` ～ `2`

  フォーマット: `double`
</ParamField>

Routerが `GET /v2/models/krea/krea-2/openapi.json` で提供するスキーマから生成されています。これは、リクエストがプロバイダーに到達する前にRouterが呼び出しの検証に使用するドキュメントと同じものです。

### 出力

<ResponseField name="completed_at" type="string (date-time)" required>
  フォーマット: `date-time`
</ResponseField>

<ResponseField name="created_at" type="string (date-time)" required>
  フォーマット: `date-time`
</ResponseField>

<ResponseField name="job_id" type="string (uuid)" required>
  フォーマット: `uuid`
</ResponseField>

<ResponseField name="result" type="object" required>
  完了した生成結果。`KreaJob` とは異なり、ここではnull許容ではありません。`result` に `urls` が含まれないターミナルジョブはComfy Routerのエラーとして応答されるため、`200` には常にこれが含まれます。
</ResponseField>

<ResponseField name="result.style_id" type="string">
  生成ではなくloraTrainingジョブによって設定されます。これらのモデルに対する `200` には `urls` が含まれます。
</ResponseField>

<ResponseField name="result.urls" type="string (uri)[]" required>
  生成済み画像をダウンロード可能なリンクとして示します。`urls` が空の `completed` ジョブは `success_without_output` となり、このドキュメントには決して到達しないため、少なくとも1つ含まれます。
</ResponseField>

<ResponseField name="status" type="string" required>
  このドキュメントでは常に `completed` です。`classifyKrea` が `succeeded` と応答する唯一のステータスであり、Kreaの語彙は大文字小文字を変換せずに照合されます。

  指定可能な値: `completed`
</ResponseField>

## 例

### 入力

```json theme={null}
{
  "aspect_ratio": "1:1",
  "prompt": "a red circle",
  "resolution": "1K"
}
```

### 出力

```json theme={null}
{
  "completed_at": "2027-01-01T00:00:37Z",
  "created_at": "2027-01-01T00:00:00Z",
  "job_id": "7f1c2e84-5b90-4a37-8d61-2c0f9ab4e153",
  "result": {
    "urls": [
      "https://example.invalid/krea/krea-2/generated.png"
    ]
  },
  "status": "completed"
}
```

## 出荷前の確認

SDK は `Idempotency-Key` を生成し、自動リトライで再利用します。手動リトライでは元のキーを再利用してください。Router は最大 10 分間接続を保持できます。

リクエストが失敗すると、Router は理由を説明する `X-Comfy-Error-Type` レスポンスヘッダーを送信します。`422` は、プロバイダーを呼び出す前に Router が入力を拒否したことを意味し、`413` はリクエスト本文が Router の受け入れ可能なサイズを超えていたことを意味します。生成されたアセットは [結果 URL の有効期限](/ja/development/comfy-router/reference#結果アセット) があるため、早めにダウンロードしてください。

上記のフィールド説明に記載されているサイズ制限は、プロバイダーの仕様から引用した、そのフィールドに対するプロバイダー自身の上限です。Router はリクエスト本文全体に対して別の上限を適用し、base64 エンコードされたメディアもこれにカウントされます。[リクエスト本文のサイズ](/ja/development/comfy-router/limitations) を参照してください。

このページは、Comfy Router 経由で呼び出す 1 つのパートナーモデルについて説明しています。同じ `comfy-sdk` / `@comfyorg/sdk` パッケージには、Comfy Cloud 上で ComfyUI のワークフローグラフ全体を実行するための 2 つ目のクライアントも含まれています: `Comfy(api_key=...)` / `new Comfy({ apiKey })`、および `client.workflows`、`client.assets`、`client.jobs`。[Comfy SDKs](/ja/development/api-development/sdks) を参照してください。

<CardGroup cols={3}>
  <Card title="ヘッダー" icon="list" href="/ja/development/comfy-router/headers">
    認証、冪等性、リクエスト ID、エラー分類、リトライ間隔、支出上限。
  </Card>

  <Card title="Router API の利用" icon="code" href="/ja/development/comfy-router/api">
    モデルの検出、バリデーションエラー、リトライ、課金。
  </Card>

  <Card title="制限事項" icon="triangle-exclamation" href="/ja/development/comfy-router/limitations">
    Router が現在対応していないことと、代替手段。
  </Card>
</CardGroup>
