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

# Comfy Router で Edit Replace Background And Relight を使う

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

`stability/edit-replace-background-and-relight` の API リファレンスです。Stability が提供し、Comfy Router から利用できます。

**入力:** テキスト、画像 · **出力:** 画像

## クイックスタート

お使いの [Comfy ワークスペース](https://platform.comfy.org/profile/api-keys?onboarding=router)でキーを作成し、`COMFY_API_KEY` としてエクスポートします。Python と TypeScript のスニペットは Comfy SDK（`pip install comfy-sdk` と `npm install @comfyorg/sdk`）を使用しています。cURL のスニペットは、同じ呼び出しを raw HTTP で行うものです。

**モデル ID:** `stability/edit-replace-background-and-relight`

**エンドポイント:** `POST https://api.comfy.org/v2/models/stability/edit-replace-background-and-relight`

> [tab] 結果を待つ
>
> ```python Python theme={null}
> from comfy_sdk import Comfy
>
> # 環境から COMFY_API_KEY を読み取ります。
> # SDK は自動的に Idempotency-Key を作成し、自動リトライのために再利用します。
> with Comfy() as client:
>     result = client.models.run(
>         "stability/edit-replace-background-and-relight",
>         {
>             "background_prompt": "a sunlit beach at golden hour",
>             "light_source_direction": "above",
>             "light_source_strength": 0.5,
>             "seed": 12345,
>             "subject_image": "https://storage.googleapis.com/example-bucket/subject.png",
>         },
>     )
>
> print(result)
> ```
>
> ```typescript TypeScript theme={null}
> import { comfy } from "@comfyorg/sdk";
>
> // 環境から COMFY_API_KEY を読み取ります。
> // SDK は自動的に Idempotency-Key を作成し、自動リトライのために再利用します。
> const { data } = await comfy.models.run("stability/edit-replace-background-and-relight", {
>   background_prompt: "a sunlit beach at golden hour",
>   light_source_direction: "above",
>   light_source_strength: 0.5,
>   seed: 12345,
>   subject_image: "https://storage.googleapis.com/example-bucket/subject.png",
> });
>
> console.log(data);
> ```
>
> ```bash cURL theme={null}
> curl https://api.comfy.org/v2/models/stability/edit-replace-background-and-relight \
>   -H "X-API-Key: $COMFY_API_KEY" \
>   -H "Idempotency-Key: $(uuidgen)" \
>   -H "Content-Type: application/json" \
>   -d "{\"background_prompt\": \"a sunlit beach at golden hour\", \"light_source_direction\": \"above\", \"light_source_strength\": 0.5, \"seed\": 12345, \"subject_image\": \"https://storage.googleapis.com/example-bucket/subject.png\"}"
> ```

> [tab] キューに送信し、後で収集する
>
> 同じボディを `POST https://api.comfy.org/v2/models/stability/edit-replace-background-and-relight/requests` に送信します。Router は実行が受け付けられるとすぐに `201` と `request_id` を返し、結果は準備ができ次第、このプロセスまたは別のプロセスから収集されます。[キュー配信](https://docs.comfy.org/ja/development/comfy-router/queue.md)では、ステータス、キャンセル、収集の流れを説明します。
>
> ```python Python theme={null}
> import asyncio
> from comfy_sdk import AsyncComfy
>
> # 環境から COMFY_API_KEY を読み取ります。
> # 各 submit() 呼び出しは独自の Idempotency-Key を生成し、自動リトライのために再利用します。
> async def main():
>     async with AsyncComfy() as client:
>         handle = await client.models.submit(
>             "stability/edit-replace-background-and-relight",
>             {
>                 "background_prompt": "a sunlit beach at golden hour",
>                 "light_source_direction": "above",
>                 "light_source_strength": 0.5,
>                 "seed": 12345,
>                 "subject_image": "https://storage.googleapis.com/example-bucket/subject.png",
>             },
>         )
>         print("request_id:", handle.request_id)  # モデル ID と併せて、別のプロセスに必要なすべての情報です
>
>         # リクエストが完了するまでポーリングし、サーバーが指定する Retry-After の時間だけ待機します。
>         async for update in handle.iter_events():
>             print(update.status, update.queue_position)
>
>         # プロバイダー自身のペイロードで、models.run() が返すのと同じ値です。
>         # 失敗したリクエストまたはキャンセル済みのリクエストは、ここで型付きの Router エラーを発生させます。
>         result = await handle.get()
>
>     print(result)
>
> asyncio.run(main())
> ```
>
> ```typescript TypeScript theme={null}
> import { comfy } from "@comfyorg/sdk";
>
> // 環境から COMFY_API_KEY を読み取ります。
> // 各 submit() 呼び出しは独自の Idempotency-Key を生成し、自動リトライのために再利用します。
> const handle = await comfy.models.submit("stability/edit-replace-background-and-relight", {
>   background_prompt: "a sunlit beach at golden hour",
>   light_source_direction: "above",
>   light_source_strength: 0.5,
>   seed: 12345,
>   subject_image: "https://storage.googleapis.com/example-bucket/subject.png",
> });
> 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);
> ```
>
> ```bash cURL theme={null}
> # 1. 送信します。Router は request_id、status_url、response_url、cancel_url を含む 201 を返します。
> curl https://api.comfy.org/v2/models/stability/edit-replace-background-and-relight/requests \
>   -H "X-API-Key: $COMFY_API_KEY" \
>   -H "Idempotency-Key: $(uuidgen)" \
>   -H "Content-Type: application/json" \
>   -d "{\"background_prompt\": \"a sunlit beach at golden hour\", \"light_source_direction\": \"above\", \"light_source_strength\": 0.5, \"seed\": 12345, \"subject_image\": \"https://storage.googleapis.com/example-bucket/subject.png\"}"
>
> # 2. ステータスが COMPLETED になるまでポーリングし、各レスポンスが指定する Retry-After 秒待機します。
> REQUEST_ID="<request_id from the 201 body>"
> curl -i https://api.comfy.org/v2/models/stability/edit-replace-background-and-relight/requests/$REQUEST_ID/status \
>   -H "X-API-Key: $COMFY_API_KEY"
>
> # 3. 収集します。モデルのネイティブ出力を含む 200、まだ実行中の場合はステータス本文を含む 202 を返します。
> curl https://api.comfy.org/v2/models/stability/edit-replace-background-and-relight/requests/$REQUEST_ID \
>   -H "X-API-Key: $COMFY_API_KEY"
> ```

## スキーマ

### 入力

**background\_prompt** `string`

新しい背景。`background_reference` を省略した場合に必須です。

**background\_reference** `string (uri)`

新しい背景の基にする画像の、Comfy 署名付きアセットURL。

形式: `uri`

**foreground\_prompt** `string`

被写体が何であるか。

**keep\_original\_background** `string`

`true` にすると元の背景の要素を保持します。デフォルトは `false` です。Stability のフォームフィールドと同様に文字列です。

指定可能な値: `true`, `false`

**light\_reference** `string (uri)`

ライティングの基にする画像の、Comfy 署名付きアセットURL。

形式: `uri`

**light\_source\_direction** `string`

ライトの方向。`left` は提供されません。

指定可能な値: `right`, `below`, `above`

**light\_source\_strength** `number`

ライトの強さ。`light_reference` または `light_source_direction` が必要です。

範囲: `0` から `1`

**negative\_prompt** `string`

画像に含めたくないもの。

**original\_background\_depth** `number`

新しい背景が被写体の奥行きにどれだけ一致するか。デフォルトは 0.5 です。

範囲: `0` から `1`

**output\_format** `string`

返されるのは `png` のみです。

指定可能な値: `png`

**preserve\_original\_subject** `number`

元の被写体をどれだけ正確に保持するか。デフォルトは 0.6 です。

範囲: `0` から `1`

**seed** `integer`

0 または省略するとランダムなシードが選ばれます。

範囲: `0` から `4294967294`

**subject\_image** `string (uri)` *required*

被写体を保持する画像の、Comfy 署名付きアセットURL。

形式: `uri`

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

### 出力

**finish\_reason** `string` *required*

200 の場合は常に SUCCESS です。

**image** `string (uri)` *required*

（最初の）PNG 画像の Comfy 署名付きURL。最大 24 時間有効です。

形式: `uri`

**images** `string (uri)[]`

SDXL のみで、複数の画像が返された場合に含まれます。Comfy 署名付きのすべての画像URLを出力順に並べたもので、それぞれ最大 24 時間有効です。

**seed** `integer` *required*

画像の生成に使用されたシード。

## 例

### 入力

```json theme={null}
{
  "background_prompt": "a sunlit beach at golden hour",
  "light_source_direction": "above",
  "light_source_strength": 0.5,
  "seed": 12345,
  "subject_image": "https://storage.googleapis.com/example-bucket/subject.png"
}
```

### 出力

```json theme={null}
{
  "finish_reason": "SUCCESS",
  "image": "https://example.invalid/stability/image.png",
  "seed": 12345
}
```

## 出荷前の確認

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

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

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

このページは、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](https://docs.comfy.org/ja/development/api-development/sdks.md) を参照してください。

> [card] [ヘッダー](https://docs.comfy.org/ja/development/comfy-router/headers.md)
>
> 認証、冪等性、リクエスト ID、エラー分類、リトライ間隔、支出上限。

> [card] [Router API の利用](https://docs.comfy.org/ja/development/comfy-router/api.md)
>
> モデルの検出、バリデーションエラー、リトライ、課金。

> [card] [制限事項](https://docs.comfy.org/ja/development/comfy-router/limitations.md)
>
> Router が現在対応していないことと、代替手段。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.