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

# Flux 2 API：文字生成圖片與影像編輯

> Black Forest Labs 影像生成 API，用於文生圖和圖生圖工作流程

## 概覽

Flux 2 是 Black Forest Labs 的影像模型，支援高質量文字生成圖片，也可以參考已有圖片進行編輯。在 APIXO Playground 試用後，可依本頁範例呼叫 API。

| 功能 | 值 |
| - | - |
| 模型 ID | `flux-2` |
| 模式 | `pro-text-to-image`, `pro-image-to-image`, `flex-text-to-image`, `flex-image-to-image` |
| 提示詞長度 | 3 至 5000 個字元 |
| 參考圖片 | 1-8 結合圖生圖模式的 URL |
| 尺寸 | 每側 `width*height`、`64-2048` |
| 縱橫比 | `1:1`、`4:3`、`3:4`、`3:2`、`2:3`、`16:9`、`9:16`、`auto`（`auto` 僅適用於與 `image_urls` 的圖生圖） |
| 解析度級距 | `1K`, `2K` |

## 端點與身分驗證

API 基底網址：

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

| 方法 | 端點 | 用途 |
| - | - | - |
| `POST` | `/generateTask/flux-2` | 送出生成任務 |
| `GET` | `/statusTask/flux-2?taskId={taskId}` | 查詢任務狀態並取得結果 |

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

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

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

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

## 非同步請求範例

以下範例提交一個最簡單的文字生成圖片任務，回應中會傳回 `taskId`。

```bash theme={null}
curl -X POST "https://api.apixo.ai/api/v1/generateTask/flux-2" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "request_type": "async",
    "input": {
      "mode": "pro-text-to-image",
      "prompt": "a cozy cyberpunk cafe with neon signs and rain on the windows",
      "size": "1000*700",
      "prompt_upsampling": true
    }
  }'
```

成功回應：

```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/flux-2?taskId=task_12345678" \
  -H "Authorization: Bearer YOUR_API_KEY"
```

任務處理中的回應範例：

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

成功回應：

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

任務失敗時的回應範例：

```json theme={null}
{
  "code": 200,
  "message": "success",
  "data": {
    "taskId": "task_12345678",
    "state": "failed",
    "failCode": "SensitiveContent",
    "failMsg": "Content was rejected by safety checks, please adjust the prompt",
    "createTime": 1767965610929,
    "completeTime": 1767965620132
  }
}
```

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

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

## 請求體

### 文生圖

```json theme={null}
{
  "request_type": "async",
  "input": {
    "mode": "pro-text-to-image",
    "prompt": "a minimal poster for a jazz night, with crisp readable typography",
    "size": "1024*1365",
    "prompt_upsampling": true
  }
}
```

### 圖生圖

```json theme={null}
{
  "request_type": "async",
  "input": {
    "mode": "flex-image-to-image",
    "prompt": "turn this product photo into a polished studio campaign image",
    "image_urls": [
      "https://example.com/source.png"
    ],
    "aspect_ratio": "1:1",
    "resolution": "2K",
    "size": "1024*1024",
    "use_preview": false
  }
}
```

## 參數

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

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

**callback\_url** `string`

當 `request_type` 為 `callback` 時必填。請提供可從網際網路存取的 HTTPS 網址，以接收任務的最終結果。詳見 [Webhook](https://apixo.ai/docs/zh-hant/api-reference/webhooks.md)。

**input** `object` *required*

Flux 2 的輸入參數。

**屬性**

**mode** `string` *required*

生成模式。可用值為 `pro-text-to-image`、`pro-image-to-image`、`flex-text-to-image`、`flex-image-to-image`。

**prompt** `string` *required*

用文字描述想生成的影像或想修改的內容。長度須為 3 至 5000 個字元。

**image\_urls** `string[]`

參考影像 URL。`pro-image-to-image` 和 `flex-image-to-image` 需要；不用於文生圖模式。必須包含 `1-8` 個公用、可直接存取、非空 URL。支援格式：`JPG`、`PNG`、`WebP`；每個檔案必須是 `<=10MB`

**size** `string`

`width*height` 格式的可選輸出尺寸。每條邊必須位於 `64` 和 `2048` 之間。若同時提供`size`、`resolution`和`aspect_ratio`，則`resolution`和`aspect_ratio`優先

**aspect\_ratio** `string`

輸出縱橫比。支援的值：`1:1`、`4:3`、`3:4`、`3:2`、`2:3`、`16:9`、`9:16`、`auto`。`auto` 僅在提供 `image_urls` 時對圖生圖模式有效，並使用第一個輸入影像的比率。除非提供 `size`，否則是必需的

**resolution** `string`

輸出分辨率層。支援的值：`1K`、`2K`。值區分大小寫。除非提供 `size`，否則為必需。如果與 `size` 一起提供，則 `resolution` 優先。

**prompt\_upsampling** `boolean`

為支援的工作流程啟用提示上採樣行為。

**use\_preview** `boolean`

對支援的工作流程使用預覽行為。

### 模式選項

| 模式 | 使用案例 |
| - | - |
| `pro-text-to-image` | 專業文生圖生成 |
| `pro-image-to-image` | 使用參考影像進行專業圖生圖編輯 |
| `flex-text-to-image` | Flex 文生圖生成 |
| `flex-image-to-image` | 使用參考影像進行 Flex 圖生圖編輯 |

## 回應格式

### 送出任務時的回應

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

**code** `integer`

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

**message** `string`

狀態說明。

**data.taskId** `string`

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

### 狀態回應欄位

**taskId** `string`

任務的唯一識別碼。

**state** `string`

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

**resultJson** `string`

內含生成圖片 URL 的 JSON 字串。`state` 為 `success` 時傳回。

**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/flux-2" \
  -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": "pro-text-to-image",
      "prompt": "a premium coffee bag label with exact readable brand typography",
      "aspect_ratio": "1:1",
      "resolution": "2K"
    }
  }'
```

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)。

Flux 2 依產生的影像計費。在任一支援的解析度下，`pro-image-to-image` 成本為 `$0.06 / image`。其他模式使用以下價格。

| 模式層 | 解析度 | APIXO 價格 |
| - | - | - |
| `pro-text-to-image` | `1K` | `$0.04 / image` |
| `pro-text-to-image` | `2K` | `$0.05 / image` |
| `pro-image-to-image` | `1K`或`2K` | `$0.06 / image` |
| `flex-*` | `1K` | `$0.10 / image` |
| `flex-*` | `2K` | `$0.16 / image` |

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

## 耗時與輪詢

實際延遲可能會因提示複雜性、參考影像計數、提供者路由和目前佇列負載而異。

| 一般生成時間 | 建議首次查詢時間 | 輪詢間隔 |
| - | - | - |
| 大約30秒 | 任務創建後 20 秒 | 3-10 秒 |

> [!TIP]
>
> 正式環境的並行請求較多時，建議使用回呼，以免頻繁輪詢。

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

## 錯誤與排查

### HTTP 錯誤

| 代碼 | 含義 | 處理方式 |
| - | - | - |
| `400` | 請求主體無效、參數缺失、值不受支援、提示長度問題或影像無法存取 URL | 修正請求後再試 |
| `401` | 缺少 API 金鑰，或金鑰無效 | 檢查 `Authorization` 請求標頭 |
| `402` | 餘額或配額不足 | 充值或更換帳號或金鑰 |
| `403` | 鍵或路由無法存取模型，或內容被拒絕 | 檢查權限並調整提示或影像 |
| `429` | 已達到速率或並行限制 | 以指數退避方式重試 |
| `500` | 伺服器錯誤或未知任務失敗 | 逐步拉長間隔後重試 |
| `502` | 服務錯誤 | 逐步拉長間隔後重試 |
| `504` | 服務超時 | 重新嘗試；耗時較長的任務可改用回呼模式 |

### 任務失敗代碼

| 錯誤碼 | 含義 | 處理方式 |
| - | - | - |
| `SensitiveContent` | 提示、輸入影像或輸出已透過安全檢查標記 | 更改提示或參考影像 |
| `ProhibitedContentDetected` | 安全檢查因禁止內容而拒絕任務 | 調整提示或輸入影像 |
| `ImageNotAccess` | 無法取得參考影像 URL | 使用公開的直接影像 URL |
| `PromptInvalid` | 提示詞無效或遭拒 | 重寫提示 |
| `MissingParameter` | 缺少一個或多個必要參數 | 檢查 `mode`、`prompt`、`aspect_ratio`、`resolution` 和 `image_urls` |
| `RateLimited` | 請求頻率已達上限 | 逐步拉長間隔後重試 |
| `Timeout` | 任務逾時 | 重試、降低輸入複雜度或使用回呼模式 |
| `Unknown error` | 任務因不明原因失敗 | 重試後退或聯絡支援人員（如果問題仍然存在） |

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

## MCP 整合

[完整模型 Schema](https://apixo.ai/docs/models/schemas/image/flux-2.json) 中的 `mcp.result` 指定結果的解析方式，`mcp.attachments` 列出可繫結檔案的欄位。這些中繼資料供用戶端整合使用，無須當作生成參數提交。附件欄位為 `image_urls`。請保留使用者指定的檔案順序。模型參數由 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)
* [路由策略](https://apixo.ai/docs/zh-hant/concepts/routing-strategies.md)
* [在 APIXO Playground 嘗試 Flux 2](https://apixo.ai/models/flux-2)
* [API定價](https://apixo.ai/pricing/api)


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