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

# Qwen 2 Image API：Standard 和 Pro 文字生成圖片模式

> Qwen 2 文生圖 API，具備標準和專業生成模式、靈活的尺寸以及依提示生成的文字渲染。

## 概覽

Qwen 2 Image 是一個異步文生圖 API。它支援標準與專業生成模式、多種寬高比、提示擴展、選填的負面提示，以及每個任務最多 6 張圖片。

| 功能 | 值 |
| - | - |
| 模型 ID | `qwen-2-image` |
| 模式 | `text-to-image`, `text-to-image-pro` |
| 預設模式 | `text-to-image` |
| 提示詞長度 | 1 至 800 個字元 |
| 負面提示 | 可選，最多 500 個字元 |
| 每個任務的影像數量 | `num_images` 預設為 `1`；範圍為 `1-6` |
| 縱橫比 | `1:1`, `3:4`, `4:3`, `9:16`, `16:9` |
| 提示擴展 | `prompt_extend`，預設為 `true` |
| 水印 | `watermark`，預設`false` |

## 端點與身分驗證

API 基底網址：

```text theme={null}
https://api.apixo.ai/api/v1
```

| 方法 | 端點 | 用途 |
| - | - | - |
| `POST` | `/generateTask/qwen-2-image` | 提交文字轉影像任務 |
| `GET` | `/statusTask/qwen-2-image?taskId={taskId}` | 查詢任務狀態並取得結果 |

每個請求都必須附上 APIXO API 金鑰：

```http theme={null}
Authorization: Bearer YOUR_API_KEY
```

送出任務時，還需設定以下請求標頭：

```http theme={null}
Content-Type: application/json
```

## 非同步請求範例

```bash theme={null}
curl -X POST "https://api.apixo.ai/api/v1/generateTask/qwen-2-image" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "request_type": "async",
    "input": {
      "mode": "text-to-image-pro",
      "prompt": "a premium poster for a bookstore event, crisp typography, elegant layout",
      "negative_prompt": "blurry text, distorted letters, low resolution",
      "aspect_ratio": "16:9",
      "num_images": 2,
      "prompt_extend": true,
      "watermark": false
    }
  }'
```

成功回應：

```json theme={null}
{
  "code": 200,
  "message": "success",
  "data": {
    "taskId": "task_12345678"
  }
}
```

請保留 `taskId`，後續輪詢結果時會用到。

## 輪詢結果

```bash theme={null}
curl -X GET "https://api.apixo.ai/api/v1/statusTask/qwen-2-image?taskId=task_12345678" \
  -H "Authorization: Bearer YOUR_API_KEY"
```

成功回應：

```json theme={null}
{
  "code": 200,
  "message": "success",
  "data": {
    "taskId": "task_12345678",
    "state": "success",
    "resultJson": "{\"resultUrls\":[\"https://file.apixo.ai/image.png\"]}",
    "createTime": 1767965610929,
    "completeTime": 1767965652317,
    "costTime": 41388
  }
}
```

等 `state` 變為 `success` 後，再解析 `resultJson`：

```javascript theme={null}
const payload = JSON.parse(data.resultJson);
const imageUrls = payload.resultUrls;
```

## 請求體

### 標準文生圖

```json theme={null}
{
  "request_type": "async",
  "input": {
    "mode": "text-to-image",
    "prompt": "a minimalist product poster for wireless earbuds, clean layout",
    "aspect_ratio": "4:3",
    "num_images": 1,
    "prompt_extend": true,
    "watermark": false
  }
}
```

### 專業文字轉影像

```json theme={null}
{
  "request_type": "callback",
  "callback_url": "https://your-server.com/webhooks/apixo",
  "input": {
    "mode": "text-to-image-pro",
    "prompt": "a high-end brand key visual with readable headline text and premium lighting",
    "negative_prompt": "blurred typography, misspelled words, jagged edges",
    "aspect_ratio": "9:16",
    "num_images": 2,
    "seed": 123456
  }
}
```

## 參數

**request\_type** `string` *required* (default: async)

取得結果的方式。選擇 `async` 時，透過 `statusTask` 輪詢結果；選擇 `callback` 時，透過 Webhook 接收結果。

**callback\_url** `string`

當`request_type`為`callback`時需要。必須是可以接收最終任務負載的回呼URL。請參閱 [Webhooks](https://apixo.ai/docs/zh-hant/api-reference/webhooks.md)。

**input** `object` *required*

Qwen 2 Image 的輸入參數。

**屬性**

**mode** `string` (default: text-to-image)

生成模式。支援的值：`text-to-image`、`text-to-image-pro`。若省略，後端預設為 `text-to-image`。

**prompt** `string` *required*

提示文字。必須是非空字串且不能超過 `800` 個字元。

**negative\_prompt** `string`

選填的否定提示。最大長度為 `500` 個字元。

**aspect\_ratio** `string` (default: 1:1)

輸出畫面的長寬比。可用值為 `1:1`、`3:4`、`4:3`、`9:16`、`16:9`。

**num\_images** `integer` (default: 1)

輸出影像的數量。接受整數或數字字串輸入。支援範圍：`1-6`。

**prompt\_extend** `boolean` (default: true)

是否啟用提示擴充。

**watermark** `boolean` (default: false)

是否為生成的影像添加浮水印。

**seed** `integer`

選填的隨機種子。支援範圍：`0-2147483647`。

**nsfw\_checker** `boolean` (default: false)

設為 `true` 可在支援的生成路由上啟用上游內容審核。API 預設值為 `false`。

## 回應格式

### 送出任務時的回應

任務受理後，`POST /generateTask/qwen-2-image` 會傳回任務 ID：

**code** `integer`

API 狀態碼。`200` 表示任務已受理。

**message** `string`

狀態說明。

**data.taskId** `string`

任務的唯一 ID，用於查詢任務狀態。

### 狀態回應欄位

**taskId** `string`

任務的唯一識別碼。

**state** `string`

目前的任務狀態，可能的值為 `pending`、`processing`、`success`、`failed`。

**resultJson** `string`

當 `state` 為 `success` 時傳回。解析此 JSON 字串後，即可從 `resultUrls` 取得生成的圖片連結。

**failCode** `string`

供程式判讀的錯誤碼，僅在 `state` 為 `failed` 時傳回。

**failMsg** `string`

失敗原因說明，僅在 `state` 為 `failed` 時傳回。

**createTime** `integer`

任務建立時的 Unix 時間戳記，單位為毫秒。

**completeTime** `integer`

任務完成時的 Unix 時間戳記，單位為毫秒。任務完成後才會傳回。

**costTime** `integer`

處理時間，以毫秒為單位。有計時資料時才會傳回。

## Webhook 回呼模式

若希望後端自動收到最終結果，可使用回呼模式，無須輪詢。

```bash theme={null}
curl -X POST "https://api.apixo.ai/api/v1/generateTask/qwen-2-image" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "request_type": "callback",
    "callback_url": "https://your-server.com/webhooks/apixo",
    "input": {
      "mode": "text-to-image-pro",
      "prompt": "a premium campaign poster with readable headline text",
      "aspect_ratio": "16:9",
      "num_images": 1
    }
  }'
```

Webhook 的接收條件與重試規則請見 [Webhook 文件](https://apixo.ai/docs/zh-hant/api-reference/webhooks.md)。

## 計費

新提交的非 LLM 請求按 **1 點 = `$0.001`** 計費。先依用量和加購項目計算總額，再套用帳戶折扣，最後將請求總額無條件進位至整數點數。若已約定固定單價，則保留該單價的精度，不再使用標準價或另行打折；最終點數仍須進位。詳見[積分換算與進位](https://apixo.ai/docs/zh-hant/pricing.md#credit-conversion-and-rounding)。

Qwen 2 Image 依產生的影像計費。

| 模式 | APIXO 價格 |
| - | - |
| `text-to-image` | `$0.03 / image` |
| `text-to-image-pro` | `$0.07 / image` |

計費行為：

```text theme={null}
precharge = requested_num_images * unit_price_for_mode
final_charge = successful_images * unit_price_for_mode
refund = precharge - final_charge (if partial failure)
```

最新的美元費率請見 [API 價目表](https://apixo.ai/pricing/api)。

## 耗時與輪詢

Qwen 2 Image 任務是異步的。實際延遲會因提示複雜性、請求的影像計數和佇列負載而異。

| 使用案例 | 建議首次查詢時間 | 輪詢間隔 |
| - | - | - |
| 標準世代 | 任務建立 10 秒後 | 5s-10s |
| 專業模式或多張影像 | 任務建立 10 至 15 秒後 | 5s-10s |

> [!TIP]
>
> 對於生產佇列或批次，使用回呼模式以避免頻繁輪詢。

請求頻率與並行數上限會依帳戶、API 金鑰和路由而異。收到 `429` 時，請降低請求頻率，並逐步拉長重試間隔。帳戶層級的限制請見[系統 API](https://apixo.ai/docs/zh-hant/api-reference/system.md)。

## 錯誤與排查

### HTTP 錯誤

| 代碼 | 含義 | 處理方式 |
| - | - | - |
| `400` | 請求主體、參數類型或值無效 | 修正請求後再試 |
| `401` | 缺少 API 金鑰，或金鑰無效 | 檢查 `Authorization` 請求標頭 |
| `402` | 餘額或配額不足 | 充值或更換帳號或金鑰 |
| `403` | 金鑰或路由無權訪問該模型 | 檢查權限和路由策略 |
| `404` | 未找到任務 | 驗證 `taskId` 和模型 ID |
| `429` | 已達到速率或並行限制 | 以指數退避方式重試 |
| `500` | 伺服器錯誤或未知任務失敗 | 逐步拉長間隔後重試 |
| `502` | 模型服務錯誤 | 逐步拉長間隔後重試 |
| `504` | 模型服務逾時 | 重新嘗試；耗時較長的任務可改用回呼模式 |

### 請求驗證

| 條件 | 後端行為 |
| - | - |
| `mode` 無效 | 傳回`Invalid mode type. Supported: text-to-image, text-to-image-pro` |
| 缺失或為空 `prompt` | 傳回缺少參數或驗證錯誤 |
| 提示超過 `800` 個字元 | 回傳長度錯誤 |
| `num_images` 無效 | 傳回`The parameter {{num_images}} must be between 1 and 6.` |
| `aspect_ratio` 無效 | 傳回支援的寬高比列表 |
| `prompt_extend` 或 `watermark` 無效 | 傳回布林類型錯誤 |
| `seed` 無效 | 傳回支援的種子範圍 |

完整列表見[錯誤碼](https://apixo.ai/docs/zh-hant/api-reference/errors.md)。

## MCP 整合

[完整模型 Schema](https://apixo.ai/docs/models/schemas/image/qwen-2-image.json) 中的 `mcp.result` 指定結果的解析方式，`mcp.attachments` 列出可繫結檔案的欄位。這些中繼資料供用戶端整合使用，無須當作生成參數提交。模型參數由 API 驗證。

## 相關連結

* [生成 API 總覽](https://apixo.ai/docs/zh-hant/models.md)
* [影像模型](https://apixo.ai/docs/zh-hant/models/image)
* [送出生成任務](https://apixo.ai/docs/zh-hant/api-reference/generate-task.md)
* [查詢任務狀態](https://apixo.ai/docs/zh-hant/api-reference/status-task.md)
* [Webhook](https://apixo.ai/docs/zh-hant/api-reference/webhooks.md)
* [錯誤碼](https://apixo.ai/docs/zh-hant/api-reference/errors.md)
* [參數規範](https://apixo.ai/docs/zh-hant/api-reference/parameters.md)
* [API定價](https://apixo.ai/pricing/api)


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