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

# Flux 2.0 圖像生成

> 使用 Flux 2.0 模型生成高質量圖像，支持文生圖和圖生圖

* Flux 2.0 圖像生成模型
* 通過 model 參數選擇 `flux-2-flex`（速度較快，適合快速迭代）或 `flux-2-pro`（質量更高，細節更好）
* 支持文生圖、圖生圖，最多 8 張參考圖
* 按分辨率計費（1K/2K）
* 異步任務模式，返回 `task_id` 用於查詢結果

<Warning>
  **重要變更**：爲了更好的性能和成本控制，我們不再支持在 `image_urls` 中直接傳入 base64 圖片數據。請先使用 [上傳圖片接口](../../uploads/images) 上傳圖片，獲取 URL 後再調用本接口。
</Warning>

## Authorizations

<ParamField header="Authorization" type="string" required>
  所有接口均需要使用 Bearer Token 進行認證

  獲取 API Key：訪問 [API Key 管理頁面](https://toapis.com/console/token) 獲取您的 API Key

  使用時在請求頭中添加：

  ```
  Authorization: Bearer YOUR_API_KEY
  ```
</ParamField>

## Body

<ParamField body="model" type="string" required>
  圖像生成模型名稱

  可選值：

  * `flux-2-flex` - 速度較快，適合快速迭代
  * `flux-2-pro` - 質量更高，細節更好
</ParamField>

<ParamField body="prompt" type="string" required>
  圖像生成的文本描述
</ParamField>

<ParamField body="size" type="string" default="1:1">
  圖像寬高比

  支持的寬高比：

  * `1:1` - 正方形（默認）
  * `4:3` / `3:4` - 傳統顯示器比例
  * `16:9` / `9:16` - 寬屏/豎屏
  * `3:2` / `2:3` - 標準照片
</ParamField>

<ParamField body="image_urls" type="string[]">
  參考圖像 URL 數組，用於圖生圖

  **⚠️ 僅支持 URL 格式（不支持 base64）**

  * 公開可訪問的圖片 URL（http\:// 或 https\://）
  * 可使用 [上傳圖片接口](../../uploads/images) 上傳本地圖片獲取 URL

  **限制：**

  * 最多 8 張參考圖
</ParamField>

<ParamField body="metadata" type="object">
  元數據參數，用於傳遞額外的配置選項

  <Expandable title="支持的 metadata 字段">
    <ParamField body="resolution" type="string" default="1K">
      輸出圖像分辨率

      可選值：

      * `1K` - 標準分辨率（默認）
      * `2K` - 高分辨率

      **分辨率與寬高比對應像素尺寸：**

      | 寬高比  | 1K 尺寸     | 2K 尺寸     |
      | ---- | --------- | --------- |
      | 1:1  | 1024×1024 | 2048×2048 |
      | 4:3  | 1365×1024 | 2730×2048 |
      | 3:4  | 1024×1365 | 2048×2730 |
      | 16:9 | 1820×1024 | 3640×2048 |
      | 9:16 | 1024×1820 | 2048×3640 |
      | 3:2  | 1536×1024 | 3072×2048 |
      | 2:3  | 1024×1536 | 2048×3072 |

      **注意：** 不同分辨率計費不同，2K 價格高於 1K
    </ParamField>
  </Expandable>
</ParamField>

## Response

<ResponseField name="id" type="string">
  任務唯一標識符，用於查詢任務狀態
</ResponseField>

<ResponseField name="object" type="string">
  對象類型，固定爲 `generation.task`
</ResponseField>

<ResponseField name="model" type="string">
  使用的模型名稱
</ResponseField>

<ResponseField name="status" type="string">
  任務狀態

  * `queued` - 排隊等待處理
  * `in_progress` - 處理中
  * `completed` - 成功完成
  * `failed` - 失敗
</ResponseField>

<ResponseField name="progress" type="integer">
  任務進度百分比（0-100）
</ResponseField>

<ResponseField name="created_at" type="integer">
  任務創建時間戳（Unix 時間戳）
</ResponseField>

<ResponseField name="metadata" type="object">
  任務元數據
</ResponseField>

## 使用場景

### 場景一：文生圖（快速迭代）

```json theme={null}
{
  "model": "flux-2-flex",
  "prompt": "一隻可愛的橘貓在窗臺上曬太陽",
  "size": "1:1",
  "metadata": {
    "resolution": "1K"
  }
}
```

### 場景二：文生圖（高質量輸出）

```json theme={null}
{
  "model": "flux-2-pro",
  "prompt": "未來城市的天際線，霓虹燈光，賽博朋克風格，電影質感",
  "size": "16:9",
  "metadata": {
    "resolution": "2K"
  }
}
```

### 場景三：圖生圖（多參考圖）

```json theme={null}
{
  "model": "flux-2-pro",
  "prompt": "保持角色風格，生成在森林中奔跑的場景",
  "size": "1:1",
  "image_urls": [
    "https://example.com/ref1.jpg",
    "https://example.com/ref2.jpg"
  ],
  "metadata": {
    "resolution": "2K"
  }
}
```

### 場景四：豎屏海報

```json theme={null}
{
  "model": "flux-2-flex",
  "prompt": "音樂會宣傳海報，簡約風格，深色背景",
  "size": "9:16",
  "metadata": {
    "resolution": "1K"
  }
}
```

<Note>
  **查詢任務結果**

  圖像生成爲異步任務，提交後會返回任務 ID。使用 [獲取圖像任務狀態](../../tasks/image-status) 接口查詢生成進度和結果。
</Note>

<RequestExample>
  ```bash cURL theme={null}
  curl --request POST \
    --url https://toapis.com/v1/images/generations \
    --header 'Authorization: Bearer <token>' \
    --header 'Content-Type: application/json' \
    --data '{
      "model": "flux-2-flex",
      "prompt": "一隻可愛的橘貓在窗臺上曬太陽",
      "size": "1:1",
      "metadata": {
        "resolution": "1K"
      }
    }'
  ```

  ```bash cURL (圖生圖) theme={null}
  curl --request POST \
    --url https://toapis.com/v1/images/generations \
    --header 'Authorization: Bearer <token>' \
    --header 'Content-Type: application/json' \
    --data '{
      "model": "flux-2-pro",
      "prompt": "保持角色風格，生成在森林中奔跑的場景",
      "size": "1:1",
      "image_urls": ["https://example.com/ref1.jpg"],
      "metadata": {
        "resolution": "2K"
      }
    }'
  ```

  ```python Python theme={null}
  import requests

  response = requests.post(
      "https://toapis.com/v1/images/generations",
      headers={
          "Authorization": "Bearer your-ToAPIs-key",
          "Content-Type": "application/json"
      },
      json={
          "model": "flux-2-flex",
          "prompt": "一隻可愛的橘貓在窗臺上曬太陽",
          "size": "1:1",
          "metadata": {
              "resolution": "1K"
          }
      }
  )

  task = response.json()
  print(f"任務 ID: {task['id']}")
  print(f"狀態: {task['status']}")
  ```

  ```python Python (圖生圖) theme={null}
  import requests

  response = requests.post(
      "https://toapis.com/v1/images/generations",
      headers={
          "Authorization": "Bearer your-ToAPIs-key",
          "Content-Type": "application/json"
      },
      json={
          "model": "flux-2-pro",
          "prompt": "保持角色風格，生成在森林中奔跑的場景",
          "size": "1:1",
          "image_urls": ["https://example.com/ref1.jpg"],
          "metadata": {
              "resolution": "2K"
          }
      }
  )

  task = response.json()
  print(f"任務 ID: {task['id']}")
  print(f"狀態: {task['status']}")
  ```

  ```javascript JavaScript theme={null}
  const response = await fetch('https://toapis.com/v1/images/generations', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer your-ToAPIs-key',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      model: 'flux-2-flex',
      prompt: '一隻可愛的橘貓在窗臺上曬太陽',
      size: '1:1',
      metadata: {
        resolution: '1K'
      }
    })
  });

  const task = await response.json();
  console.log(`任務 ID: ${task.id}`);
  console.log(`狀態: ${task.status}`);
  ```

  ```javascript JavaScript (圖生圖) theme={null}
  const response = await fetch('https://toapis.com/v1/images/generations', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer your-ToAPIs-key',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      model: 'flux-2-pro',
      prompt: '保持角色風格，生成在森林中奔跑的場景',
      size: '1:1',
      image_urls: ['https://example.com/ref1.jpg'],
      metadata: {
        resolution: '2K'
      }
    })
  });

  const task = await response.json();
  console.log(`任務 ID: ${task.id}`);
  console.log(`狀態: ${task.status}`);
  ```
</RequestExample>

<ResponseExample>
  ```json 200 theme={null}
  {
    "id": "task_img_abc123def456",
    "object": "generation.task",
    "model": "flux-2-flex",
    "status": "queued",
    "progress": 0,
    "created_at": 1703884800,
    "metadata": {}
  }
  ```
</ResponseExample>
