# Stable Diffusion with LoRAs API

> 使用任何 Stable Diffusion 基礎模型，支援 LoRA、嵌入向量、ControlNets 與 IP adapters 的文字生成圖像

使用由 URL 或 HuggingFace ID 在 `model_name` 中指定的基礎模型，根據文字提示詞生成圖像。可合併多個 `loras`，並支援嵌入向量、ControlNets、IP adapters 與 IC Light 輸入。取樣器選項包含許多排程器，步驟範圍為 1 到 150。可生成 1 到 8 張 png 或 jpeg 圖像。

- Model id: `lora`
- Made by: Open model
- Category: Text to Image
- Output: image
- Estimated price per run: $0.057 / image (cheapest run $0.057 (57 API credits))
- Run: `POST https://api.doitong.com/v1/run/lora`
- Quote: `POST https://api.doitong.com/v1/quote/lora`
- Docs page: https://doitong.com/zh-TW/developers/models/lora

## Pricing

Price: **$0.057 / image**. Estimated from the typical cost of one run, per output (`num_images`). The quote is exactly what the run charges.

The full example input below costs $0.057 = 57 API credits (1 image × 57 API credits).

Prices are the upstream price in USD, paid from the API balance in API credits (1000 API credits = 1 USD). `POST https://api.doitong.com/v1/quote/lora` with the same JSON body returns the exact price for that input; the quote is exactly what a run charges. Invalid input is never charged and failed or canceled requests are refunded automatically.

## Input parameters

Send either the canonical name or the native field name (not both). Canonical names are the same across models.

| Parameter | Canonical name | Type | Required | Default | Allowed values / range | Description |
|---|---|---|---|---|---|---|
| `model_name` |  | string | yes |  |  | 用於生成圖像的基礎模型，以 URL 或 HuggingFace ID 識別。 |
| `unet_name` |  | string \| null | no |  |  | 用於生成圖像的自訂 U-Net 選擇；請提供其 HuggingFace ID 或 URL。 |
| `variant` |  | string \| null | no |  |  | HuggingFace 模型變體選擇；'fp16' 是其中一個範例。 |
| `prompt` |  | string | yes |  |  | 盡可能詳細地描述想要生成的圖像，以取得最佳結果。 |
| `negative_prompt` |  | string | no | `` |  | 指定不需要圖像細節的負面提示詞。可排除色彩、物件、景物或更細微的特徵與品質，例如鬍鬚、模糊或低解析度。 |
| `prompt_weighting` |  | boolean | no | `false` |  | 提示詞加權切換：true 會透過嵌入平均移除 77 Token 限制，並啟用加權語法。 |
| `loras` |  | array of object {path, scale} | no | `[]` |  | 其組合效果會產生生成圖像的 LoRA，數量不限。 |
| `loras[].path` |  | string | yes |  |  | LoRA 權重的位置，以 URL 或路徑指定。 |
| `loras[].scale` |  | number | no | `1` | 0 – 4 | 合併至基礎模型前套用至 LoRA 權重的倍數。 |
| `embeddings` |  | array of object {path, tokens} | no | `[]` |  | 在圖像生成期間套用的嵌入，用於將提示詞 Token 對應至嵌入權重。目前僅支援一個嵌入。 |
| `embeddings[].path` |  | string | yes |  |  | 由 URL 或檔案路徑指定的嵌入權重。 |
| `embeddings[].tokens` |  | array of string \| null | no |  |  | 對應至嵌入權重的提示詞 Token。省略時，會為每個嵌入檔案向量建立一個 Token，名稱為 <s0>、<s1>、...；請在提示詞中使用對應的 Token。 |
| `controlnets` |  | array of object {path, config_url, variant, image_url, mask_url, conditioning_scale, start_percentage, end_percentage, ip_adapter_index} | no | `[]` |  | 生成期間在指定時間步驟套用至圖像的 ControlNet，可提供任意數量。 |
| `controlnets[].path` |  | string | yes |  |  | ControlNet 權重位置，可指定為 URL 或路徑。 |
| `controlnets[].config_url` |  | string \| null | no |  |  | 可選的 URL，指向 ControlNet 的 config.json 檔案。 |
| `controlnets[].variant` |  | string \| null | no |  |  | 搭配 Hugging Face 儲存庫金鑰使用的可選變體。 |
| `controlnets[].image_url` |  | string (image URL) | yes |  |  | 作為 ControlNet 輸入的圖像 URL。 |
| `controlnets[].mask_url` |  | string (image URL) \| null | no |  |  | ControlNet 遮罩；提供時必須與控制圖像尺寸相同，寬度和高度都必須可被 32 整除。 |
| `controlnets[].conditioning_scale` |  | number | no | `1` | 0 – 2 | ControlNet 權重合併至基礎模型前套用的縮放。 |
| `controlnets[].start_percentage` |  | number | no | `0` | 0 – 1 | ControlNet 開始影響圖像生成時所占總時間步的百分比。 |
| `controlnets[].end_percentage` |  | number | no | `1` | 0 – 1 | ControlNet 停止影響圖像生成時所占總時間步的百分比。 |
| `controlnets[].ip_adapter_index` |  | integer \| null | no |  |  | InstantID ControlNet 的 IP adapter 索引選擇；其他 ControlNet 不需要此設定。 |
| `controlnet_guess_mode` |  | boolean | no | `false` |  | 為 true 時，將 ControlNet 的套用限制為條件式預測。 |
| `ip_adapter` |  | array of object {ip_adapter_image_url, ip_adapter_mask_url, path, model_subfolder, weight_name, insight_face_model_path, scale, scale_json, unconditional_noising_factor, image_projection_shortcut} | no | `[]` |  | 用於生成圖像的 IP adapter。提供任何項目都需要 image_encoder_path。 |
| `ip_adapter[].ip_adapter_image_url` |  | string (image URL) \| array of image URL | yes |  |  | 作為 IP adapter 輸入的圖像 URL。 |
| `ip_adapter[].ip_adapter_mask_url` |  | string (image URL) \| null | no |  |  | IP adapter 遮罩；提供時受尺寸限制，其尺寸必須與 adapter 圖像相同。 |
| `ip_adapter[].path` |  | string | yes |  |  | IP adapter 權重位置，以 URL 或路徑指定。 |
| `ip_adapter[].model_subfolder` |  | string \| null | no |  |  | IP adapter 權重位置，以模型目錄中的子資料夾指定。 |
| `ip_adapter[].weight_name` |  | string \| null | no |  |  | 權重檔案名稱。 |
| `ip_adapter[].insight_face_model_path` |  | string \| null | no |  |  | InsightFace 模型權重位置，以 URL 或路徑指定。 |
| `ip_adapter[].scale` |  | number | no | `1` | ≥ 0 | IP adapter 權重的縮放係數，在合併至基礎模型前套用。 |
| `ip_adapter[].scale_json` |  | object \| null | no |  |  | IP adapter 權重的縮放係數，在合併至基礎模型前套用。 |
| `ip_adapter[].unconditional_noising_factor` |  | number | no | `0` | 0 – 1 | 控制 IP adapter 無條件加噪的係數。 |
| `ip_adapter[].image_projection_shortcut` |  | boolean | no | `true` |  | 圖像投影捷徑設定，預設為 True。FaceID plus V1 模型使用 False，FaceID plus V2 模型使用 True。 |
| `image_encoder_path` |  | string \| null | no |  |  | 以 HTTPS URL 或 Hugging Face ID 指定的圖像編碼器，其相容性由 IP-Adapter 權重決定：SD1.5 使用 image_encoder_subfolder='models/image_encoder'，而 h94/IP-Adapter SDXL 使用 h94/IP-Adapter 和 image_encoder_subfolder='sdxl_models/image_encoder'。只要 ip_adapter 含有值，就必須提供編碼器。 |
| `image_encoder_subfolder` |  | string \| null | no |  |  | image_encoder_path 中存放圖像編碼器權重的子目錄。 |
| `image_encoder_weight_name` |  | string \| null | no | `pytorch_model.bin` |  | 依名稱選取用於生成圖像的圖像編碼器權重。 |
| `ic_light_model_url` |  | string \| null | no |  |  | 生成圖像時使用的 IC Light 模型 URL。 |
| `ic_light_model_background_image_url` |  | string (image URL) \| null | no |  |  | IC Light 圖像生成所需、與模型相容的背景，以圖像 URL 提供。 |
| `ic_light_image_url` |  | string (image URL) \| null | no |  |  | 使用 IC Light 模型生成時的輸入圖像 URL。 |
| `seed` |  | integer \| null | no |  |  | 用於在 Stable Diffusion 中重現圖像的種子。只要模型版本、提示詞和種子保持不變，每次執行都能精確重現圖像。 |
| `image_size` |  | object {width, height} \| enum (string) \| null | no | `square_hd` | `square_hd`, `square`, `portrait_4_3`, `portrait_16_9`, `landscape_4_3`, `landscape_16_9` | 輸出圖像尺寸，可從預設值選取或自訂寬度與高度。兩個自訂尺寸都必須可被 8 整除。 |
| `num_inference_steps` |  | integer | no | `30` | 1 – 150 | 控制 Stable Diffusion 執行的生成步驟數；增加步驟可能產生更詳細的最終圖像。 |
| `guidance_scale` |  | number | no | `7.5` | 0 – 20 | 圖像選取的提示詞遵循強度，以 CFG (Classifier Free Guidance) 表示。 |
| `clip_skip` |  | integer | no | `0` | 0 – 2 | 略過部分圖像生成以加快渲染速度，結果會略有不同。 |
| `scheduler` |  | enum (string) \| null | no |  | `DPM++ 2M`, `DPM++ 2M Karras`, `DPM++ 2M SDE`, `DPM++ 2M SDE Karras`, `Euler`, `Euler A`, `Euler (trailing timesteps)`, `LCM`, `LCM (trailing timesteps)`, `DDIM`, `TCD` | 用於對圖像去噪的排程器或取樣器。 |
| `timesteps` |  | object {method, array} | no | `{"array":[],"method":"default"}` |  | 去噪時間步驟排程覆寫，與 sigmas 互斥。未覆寫時（預設），排程器會從 num_inference_steps 計算時間步驟；提供自訂排程後，num_inference_steps 不再適用。覆寫需要排程器支援 set_timesteps 的 timesteps 引數。 |
| `timesteps.method` |  | enum (string) | no | `default` | `default`, `array` | 選擇決定時間步驟的方式：'default'（預設設定）讓排程器使用 `num_inference_steps`。若要自行提供時間步驟排程，請選取 'array' 並將排程放入 `array` 欄位。 |
| `timesteps.array` |  | array of integer | no | `[]` |  | 所選方法為 'custom' 時套用的時間步驟排程。 |
| `sigmas` |  | object {method, array} | no | `{"array":[],"method":"default"}` |  | 去噪 sigma 排程覆寫，與 timesteps 互斥。未覆寫時（預設），排程器會從 num_inference_steps 計算 sigma；提供自訂排程後，num_inference_steps 不再適用。覆寫需要排程器支援 set_sigmas 的 sigmas 引數。 |
| `sigmas.method` |  | enum (string) | no | `default` | `default`, `array` | 選擇決定 sigma 的方式：'default'（預設設定）使用排程器自身的 sigma 值。若要自行提供 sigma 排程，請選取 'custom' 並將排程放入 `array` 欄位。 |
| `sigmas.array` |  | array of number | no | `[]` |  | 所選方法為 'custom' 時套用的 sigma 排程。 |
| `prediction_type` |  | enum (string) | no | `epsilon` | `v_prediction`, `epsilon` | 選擇生成圖像的預測類型；未另行選擇時使用 `epsilon`。 |
| `rescale_betas_snr_zero` |  | boolean | no | `false` |  | 控制採樣器是否啟用 rescale_betas_snr_zero。 |
| `image_format` |  | enum (string) | no | `png` | `jpeg`, `png` | 生成圖像的檔案格式。 |
| `num_images` | `num_outputs` | integer | no | `1` | 1 – 8 | 設定單一請求生成的圖像數量，較大的批次會需要更長的生成時間。 |
| `enable_safety_checker` |  | boolean | no | `false` |  | 安全檢查器啟用狀態；true 會啟用檢查。 |
| `tile_width` |  | integer | no | `4096` | 128 – 4,096 | 設定圖像生成期間每個圖塊的大小。 |
| `tile_height` |  | integer | no | `4096` | 128 – 4,096 | 設定圖像生成期間每個圖塊的大小。 |
| `tile_stride_width` |  | integer | no | `2048` | 64 – 2,048 | 設定圖像生成期間連續圖塊位置之間的間距。 |
| `tile_stride_height` |  | integer | no | `2048` | 64 – 2,048 | 設定圖像生成期間連續圖塊位置之間的間距。 |
| `eta` |  | number | no | `0` | 0 – 1 | 生成圖像時使用的 Eta 設定。 |
| `debug_latents` |  | boolean | no | `false` |  | 為 true 時儲存潛在變數以供偵錯。 |
| `debug_per_pass_latents` |  | boolean | no | `false` |  | 設為 true 以啟用逐輪偵錯，並儲存每一輪的潛在變數。 |

File parameters (`ic_light_model_background_image_url`, `ic_light_image_url`) take a public `https://` URL, a `data:` URI up to 10 MB, or the `file_url` of an upload from `POST /files`.

## Example input

Required parameters only:

```json
{
  "model_name": "stabilityai/stable-diffusion-xl-base-1.0",
  "prompt": "A red fox trots through fresh snow at sunrise, soft golden light, shallow depth of field"
}
```

A fuller example:

```json
{
  "model_name": "stabilityai/stable-diffusion-xl-base-1.0",
  "prompt": "A red fox trots through fresh snow at sunrise, soft golden light, shallow depth of field"
}
```

## Run it

Set `DOITONG_API_KEY` to a key from https://doitong.com/developers/dashboard#keys. `?wait=20` returns the finished request in the same call when it is ready within 20 seconds; otherwise poll `GET /requests/{id}` (it also accepts `?wait=`), or pass `webhook_url`.

### curl

```bash
# Run the model; wait up to 20 s for the finished result in the same call
curl -X POST "https://api.doitong.com/v1/run/lora?wait=20" \
  -H "Authorization: Bearer $DOITONG_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model_name": "stabilityai/stable-diffusion-xl-base-1.0",
    "prompt": "A red fox trots through fresh snow at sunrise, soft golden light, shallow depth of field"
  }'

# Still "queued" or "running"? Poll with the id from the response:
curl "https://api.doitong.com/v1/requests/REQUEST_ID?wait=20" \
  -H "Authorization: Bearer $DOITONG_API_KEY"
```

### JavaScript

```javascript
const API = "https://api.doitong.com/v1";
const headers = {
  "Authorization": "Bearer " + process.env.DOITONG_API_KEY,
  "Content-Type": "application/json",
};

// Run the model; wait up to 20 s for the finished result in the same call
let res = await fetch(API + "/run/lora?wait=20", {
  method: "POST",
  headers,
  body: JSON.stringify({
    "model_name": "stabilityai/stable-diffusion-xl-base-1.0",
    "prompt": "A red fox trots through fresh snow at sunrise, soft golden light, shallow depth of field"
  }),
});
let request = await res.json();
if (!res.ok) throw new Error(request.error.code + ": " + request.error.message);

// Not finished yet: long-poll until it is
while (request.status === "queued" || request.status === "running") {
  res = await fetch(API + "/requests/" + request.id + "?wait=20", { headers });
  request = await res.json();
}
if (request.status !== "succeeded") throw new Error(JSON.stringify(request.error));
console.log(request.outputs.map((o) => o.url), request.cost.credits + " API credits ($" + request.cost.usd + ")");
```

### Python

```python
import os
import requests

API = "https://api.doitong.com/v1"
headers = {"Authorization": "Bearer " + os.environ["DOITONG_API_KEY"]}

payload = {
    "model_name": "stabilityai/stable-diffusion-xl-base-1.0",
    "prompt": "A red fox trots through fresh snow at sunrise, soft golden light, shallow depth of field",
}

# Run the model; wait up to 20 s for the finished result in the same call
r = requests.post(API + "/run/lora", params={"wait": 20}, json=payload, headers=headers)
request = r.json()
if not r.ok:
    raise RuntimeError(request["error"]["code"] + ": " + request["error"]["message"])

# Not finished yet: long-poll until it is
while request["status"] in ("queued", "running"):
    request = requests.get(API + "/requests/" + request["id"], params={"wait": 20}, headers=headers).json()
if request["status"] != "succeeded":
    raise RuntimeError(request["error"])
print([o["url"] for o in request["outputs"]], request["cost"]["credits"], "API credits", "$" + request["cost"]["usd"])
```

### MCP

The MCP server at `https://api.doitong.com/v1/mcp` exposes the tools `search_models`, `get_model`, `quote`, `run`, `get_request`, `cancel_request`, `list_requests`, `create_upload` and `get_account`. A `run` call for this model:

```json
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "tools/call",
  "params": {
    "name": "run",
    "arguments": {
      "model_id": "lora",
      "input": {
        "model_name": "stabilityai/stable-diffusion-xl-base-1.0",
        "prompt": "A red fox trots through fresh snow at sunrise, soft golden light, shallow depth of field"
      },
      "wait_seconds": 20
    }
  }
}
```

## Response

Every call returns a request object. `status` is one of `queued`, `running`, `succeeded`, `failed`, `canceled`. `outputs` lists the generated files with URLs on our storage (kept forever, or 7 days with `?storage=temporary`; `storage` says which); `output` is the model's own result object with the same URLs.

```json
{
  "id": "req_8k2m4q7x1z9c3v5b6n0p2r4t",
  "object": "request",
  "model": "lora",
  "status": "succeeded",
  "created_at": "2026-10-20T12:00:00Z",
  "started_at": "2026-10-20T12:00:02Z",
  "completed_at": "2026-10-20T12:00:09Z",
  "queue_position": null,
  "cost": {
    "credits": 57,
    "usd": "0.057",
    "charged": 57,
    "refunded": 0
  },
  "input": {
    "model_name": "stabilityai/stable-diffusion-xl-base-1.0",
    "prompt": "A red fox trots through fresh snow at sunrise, soft golden light, shallow depth of field"
  },
  "output": {
    "images": [
      {
        "url": "https://content.doitong.com/dev-api/2026/10/20/req_8k2m4q7x1z9c3v5b6n0p2r4t/0-output.jpeg",
        "content_type": "image/jpeg",
        "file_name": "0-output.jpeg",
        "file_size": 1843200,
        "width": 1024,
        "height": 768
      }
    ],
    "seed": 42,
    "has_nsfw_concepts": [
      false
    ],
    "debug_latents": {
      "url": "https://content.doitong.com/dev-api/2026/10/20/req_8k2m4q7x1z9c3v5b6n0p2r4t/0-output.jpeg",
      "content_type": "image/jpeg",
      "file_name": "0-output.jpeg",
      "file_size": 1843200
    },
    "debug_per_pass_latents": {
      "url": "https://content.doitong.com/dev-api/2026/10/20/req_8k2m4q7x1z9c3v5b6n0p2r4t/0-output.jpeg",
      "content_type": "image/jpeg",
      "file_name": "0-output.jpeg",
      "file_size": 1843200
    }
  },
  "outputs": [
    {
      "type": "image",
      "url": "https://content.doitong.com/dev-api/2026/10/20/req_8k2m4q7x1z9c3v5b6n0p2r4t/0-output.jpeg",
      "content_type": "image/jpeg",
      "file_size": 1843200,
      "width": 1024,
      "height": 768,
      "stored": true
    }
  ],
  "error": null,
  "metrics": {
    "inference_seconds": 6.8
  },
  "urls": {
    "self": "https://api.doitong.com/v1/requests/req_8k2m4q7x1z9c3v5b6n0p2r4t",
    "cancel": "https://api.doitong.com/v1/requests/req_8k2m4q7x1z9c3v5b6n0p2r4t/cancel"
  }
}
```

### Output fields (`output`)

| Field | Type | Description |
|---|---|---|
| `images` | array of object {url, content_type, file_name, file_size, width, height} | 生成圖像的檔案資訊。 |
| `seed` | integer | 圖像生成種子；若輸入未提供種子，則包含隨機選取的值，否則會重複提供的值。 |
| `has_nsfw_concepts` | array of boolean | 指出生成的圖像中是否出現 NSFW 概念。 |
| `debug_latents` | object {url, content_type, file_name, file_size} \| null | 儲存以供偵錯的潛在資料。 |
| `debug_per_pass_latents` | object {url, content_type, file_name, file_size} \| null | 儲存以供偵錯的逐輪潛在資料。 |

## Related models

- [Stable Diffusion with LoRAs · Image to Image](https://doitong.com/developers/models/lora/image-to-image.md) (`lora/image-to-image`): $0.264 / image
- [Stable Diffusion with LoRAs · Inpaint](https://doitong.com/developers/models/lora/inpaint.md) (`lora/inpaint`): $0.184 / image
- [FLUX.1 \[dev\]](https://doitong.com/developers/models/flux/dev.md) (`flux/dev`): $0.025 / megapixel
- [FLUX.1 \[schnell\]](https://doitong.com/developers/models/flux/schnell.md) (`flux/schnell`): $0.003 / megapixel
- [FLUX.2 \[dev\]](https://doitong.com/developers/models/flux-2.md) (`flux-2`): $0.012 / megapixel
- [FLUX.2 \[pro\]](https://doitong.com/developers/models/flux-2-pro.md) (`flux-2-pro`): $0.015 / megapixel of output image
- [GPT Image 2.5 Text to Image](https://doitong.com/developers/models/openai/gpt-image-2.5/flare/text-to-image.md) (`openai/gpt-image-2.5/flare/text-to-image`): from $0.008 / image
- [Ideogram V4.5](https://doitong.com/developers/models/ideogram/v4.5.md) (`ideogram/v4.5`): from $0.03 / image

## Links

- HTML page: https://doitong.com/developers/models/lora
- All models and guides for AI agents: https://doitong.com/developers/llms.txt
- OpenAPI 3.1 with this model's typed request body: https://doitong.com/developers/openapi.json?model=lora
- Errors: https://doitong.com/developers/docs/errors
- Model details as JSON: https://api.doitong.com/v1/models/lora
