> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.beatapi.io/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.beatapi.io/_mcp/server.

# Qwen Image 2.1

> 通过文字或最多十张公开 HTTPS 参考图生成、编辑图片，支持 1K、2K 及透明 PNG、WebP。

`POST https://api.beatapi.io/v1/images/tasks`

通过文字或最多十张公开 HTTPS 参考图生成、编辑图片，支持 1K、2K 及透明 PNG、WebP。

### 支持的生成方式

| 生成方式  | 必填参数                        | 使用规则                      |
| ----- | --------------------------- | ------------------------- |
| 文生图   | `model`, `prompt`           | 不要传入 `images`。            |
| 参考图生成 | `model`, `prompt`, `images` | 传入 1–10 个公网 HTTPS 图片 URL。 |

**请求规则：** auto 沿用首张参考图比例，无图时为 1:1。enhance\_prompt 无图默认 true、有图默认 false。透明背景须用 png 或 webp，不能用 jpeg。参考示例 URL 是占位地址，请替换为可访问的 HTTPS 图片。示例描述保存的公开合同，不是已完成的模型调用。

**异步任务流程**

创建任务后保存 `data.id`，每 5–10 秒调用 `GET /v1/tasks/{task_id}` 查询状态。`status` 为 `succeeded` 时读取 `data.output.media`，为 `failed` 时读取 `data.error_code` 和 `data.error_message`，随后按[错误与重试策略](/quick-guide#errors-and-retry-policy)处理。

**使用本地文件**

请先[上传本地输入](/quick-guide#upload-an-input-file)，再将返回的 `data.url` 填入请求。

## 鉴权

`Authorization`   `string`   **必填**

在 `Authorization` 请求头中以 Bearer 令牌发送服务端 API 密钥，可在[控制台](https://beatapi.io/dashboard)创建。

```
Authorization: Bearer <BEATAPI_API_KEY>
```

永久 API 密钥只能保存在可信服务端，不能写入浏览器、移动端、公开脚本、日志或截图。请求体必须使用 `Content-Type: application/json`。建议为每个逻辑创建请求设置唯一的 `Idempotency-Key`，只有完全相同的请求体才能安全复用原键。

## 请求体

字段名、类型和枚举值保留英文原样，用途和限制用中文说明。

`aspect_ratio`   `enum<string>`   **选填**

auto 沿用首张参考图比例；无图时输出 1:1。

**取值与默认值:** 可选值 `auto`、`1:1`、`4:3`、`3:4`、`3:2`、`2:3`、`16:9`、`9:16`、`21:9`、`9:21`；默认值 `auto`

---

`background`   `enum<string>`   **选填**

opaque 或 transparent；透明输出须选 PNG 或 WebP。

**取值与默认值:** 可选值 `opaque`、`transparent`；默认值 `opaque`

---

`enhance_prompt`   `boolean`   **选填**

渲染前改写提示词；无图默认 true，有图默认 false，没有无条件默认值。

---

`images`   `string[]`   **选填**

1–10 个公开 HTTPS 图片 URL；文生图省略此字段。

**取值与默认值:** 1–10 项

---

`model`   `enum<string>`   **必填**

必须使用 qwen-image-2.1。

**取值与默认值:** 可选值 `qwen-image-2.1`

---

`output_format`   `enum<string>`   **选填**

输出格式；透明背景使用 png 或 webp，jpeg 不保留 alpha。

**取值与默认值:** 可选值 `png`、`webp`、`jpeg`；默认值 `png`

---

`prompt`   `string`   **必填**

生成或参考编辑指令，长度为 1–5000 字符。

**取值与默认值:** 长度 1–5000 字符

---

`resolution`   `enum<string>`   **选填**

输出档位为 1K 或 2K；提示词不能代替档位设置。

**取值与默认值:** 可选值 `1K`、`2K`；默认值 `1K`

---

`seed`   `integer`   **选填**

可选的 0–2147483647 整数；不接受 -1，没有文档默认值。

**取值与默认值:** 取值范围 0–2147483647

## 请求示例

### 文生图

```bash
curl --request POST \
  --url https://api.beatapi.io/v1/images/tasks \
  --header 'Authorization: Bearer <BEATAPI_API_KEY>' \
  --header 'Content-Type: application/json' \
  --data '{
  "model": "qwen-image-2.1",
  "prompt": "A white porcelain crane above misty mountains, no text."
}'
```

### 透明 WebP

```bash
curl --request POST \
  --url https://api.beatapi.io/v1/images/tasks \
  --header 'Authorization: Bearer <BEATAPI_API_KEY>' \
  --header 'Content-Type: application/json' \
  --data '{
  "model": "qwen-image-2.1",
  "prompt": "A single green glass perfume bottle, isolated on a transparent background, no text.",
  "resolution": "2K",
  "aspect_ratio": "1:1",
  "background": "transparent",
  "output_format": "webp",
  "enhance_prompt": true,
  "seed": 0
}'
```

### 参考图生成

```bash
curl --request POST \
  --url https://api.beatapi.io/v1/images/tasks \
  --header 'Authorization: Bearer <BEATAPI_API_KEY>' \
  --header 'Content-Type: application/json' \
  --data '{
  "model": "qwen-image-2.1",
  "prompt": "Keep the product shape and label from the reference, place it on a pale stone counter under soft morning light.",
  "images": [
    "https://example.com/product.png"
  ],
  "resolution": "1K",
  "aspect_ratio": "auto",
  "background": "opaque",
  "output_format": "png",
  "enhance_prompt": false,
  "seed": 42
}'
```

## 响应

仅为合同响应示例，不是实际运行或生成结果。1K 示例金额来自本页保存价目，实际用量以任务返回为准。

```json
{
  "data": {
    "id": "task_8K2qA",
    "object": "task",
    "task_kind": "image",
    "capability_id": "qwen-image-2.1",
    "capability_version": null,
    "media_type": "image",
    "model": "qwen-image-2.1",
    "status": "queued",
    "stage": "queued",
    "created_at": 1782210000,
    "updated_at": 1782210000,
    "completed_at": null,
    "output": null,
    "usage": {
      "credits_reserved": 0.03,
      "credits_charged": 0.03,
      "credits_settled": 0,
      "credits_refunded": 0
    },
    "request_id": "req_abc123",
    "error_code": null,
    "error_message": null
  }
}
```

## 下一步

保存接受响应中的 `data.id`，每 5–10 秒轮询 `GET /v1/tasks/{task_id}`。任务成功后从 `data.output.media` 读取托管结果。

## 错误

| 状态码   | 含义与处理方式                                                                                                                                                                                                                  |
| ----- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `400` | JSON 格式错误，或字段、取值、参数组合不被接受（`bad_request`，错误信息会点名字段）；输入未通过内容审核时为 `content_policy_violation`。修正后再请求，原样重试仍会失败。                                                                                                               |
| `401` | 未提供 API 密钥（`missing_api_key`），或密钥错误、已过期、已停用或额度已用尽（`invalid_api_key`，错误信息会说明是哪一种）。不要原样重试。                                                                                                                                 |
| `402` | 余额不足（`insufficient_credits`）：账户余额为零或低于本次请求的价格。充值后再请求。                                                                                                                                                                    |
| `403` | 密钥或账户无权执行该调用（`forbidden`），例如免费额度不能使用该模型（充值即可解锁）、请求来源不在密钥的 IP 白名单内，或模型不在密钥的分组里。不要原样重试。                                                                                                                                    |
| `409` | 幂等键已用于其他请求体，或使用该键的首个请求仍在处理中（`idempotency_conflict`）。只对完全相同的请求复用原键。                                                                                                                                                       |
| `429` | 请求过于频繁（`rate_limit_exceeded`，含免费模型限额）或同时处理中的任务过多（`user_concurrency_exceeded`）。按 `Retry-After`（也在 `error.retry_after_seconds`）等待后重试。                                                                                      |
| `500` | 内部异常（`internal_error`），可以重试；持续出现时请保留 `request_id` 联系支持。                                                                                                                                                                  |
| `503` | 请求未能完成（`processing_unavailable`、`processing_timeout`、`processing_failed` 或 `result_transfer_failed`），未扣费，可退避重试；已知等待时间时会返回 `Retry-After`。实时接口未启用时为 `realtime_disabled`（不可重试），容量不足时为 `realtime_capacity_unavailable`（可重试）。 |