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

# Nano Banana 2.1 API：影像生成與編輯

> 使用 Nano Banana 2.1 API 產生和編輯影像，最多支援 10 個參考影像、靈活的寬高比以及 1K、2K 或 4K 解析度選項。

## 概覽

Nano Banana 2.1 支援文生圖生成和參考引導影像編輯。它接受靈活的寬高比，在圖生圖模式下支援最多 10 個參考影像，以及 1K、2K 或 4K 輸出。

| 功能 | 值 |
| - | - |
| 模型 ID | `nano-banana-2-1` |
| 模式 | `text-to-image`, `image-to-image` |
| 提示詞長度 | 1-20,000 個字元 |
| 參考圖片 | `image-to-image` 模式所需；最多 10 個 URL |
| 縱橫比 | `auto`, `1:1`, `1:4`, `4:1`, `1:8`, `8:1`, `2:3`, `3:2`, `3:4`, `4:3`, `4:5`, `5:4`, `9:16`, `16:9`, `21:9` |
| 解析度級距 | `1K`, `2K`, `4K` |

## 端點與身分驗證

API 基底網址：

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

| 方法 | 端點 | 用途 |
| - | - | - |
| `POST` | `/generateTask/nano-banana-2-1` | 送出生成任務 |
| `GET` | `/statusTask/nano-banana-2-1?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/nano-banana-2-1" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "request_type": "async",
    "input": {
      "mode": "text-to-image",
      "prompt": "A cinematic banana-shaped spaceship above a neon city",
      "aspect_ratio": "16:9",
      "resolution": "2K"
    }
  }'
```

成功回應：

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

任務處理中的回應範例：

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

成功回應：

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

任務失敗時的回應範例：

```json theme={null}
{
  "code": 200,
  "message": "success",
  "data": {
    "taskId": "task_12345678",
    "state": "failed",
    "failCode": "SensitiveContent",
    "failMsg": "Content violates safety policy, please adjust the prompt",
    "createTime": 1791469979361,
    "completeTime": 1791469990132
  }
}
```

等 `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 warm editorial portrait with soft window light",
    "aspect_ratio": "4:5",
    "resolution": "2K"
  }
}
```

### 圖生圖

```json theme={null}
{
  "request_type": "async",
  "input": {
    "mode": "image-to-image",
    "prompt": "Keep the subject and change the background to a snowy mountain",
    "image_urls": [
      "https://example.com/reference.png"
    ],
    "aspect_ratio": "3:2",
    "resolution": "4K"
  }
}
```

## 參數

**request\_type** `string` (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*

Nano Banana 2.1 的輸入參數。

**屬性**

**mode** `string` *required*

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

**prompt** `string` *required*

描述所需影像或編輯的文字提示。驗證時將忽略前導和尾隨空格。支援1-20,000字元。

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

參考影像 URL。`image-to-image` 所需；提供 1-10 公共、可直接取得的影像 URL。在 `text-to-image` 模式下可選。

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

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

**resolution** `string` (default: 1K)

輸出解析度級距。可用值為 `1K`、`2K`、`4K`。值不區分大小寫。

> [!NOTE]
>
> `image_urls` 僅當 `mode` 為 `image-to-image` 時必需。若省略，`aspect_ratio` 與 `resolution` 預設為 `1:1` 與 `1K`。

## 回應格式

### 送出任務時的回應

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

**code** `integer`

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

**message** `string`

狀態說明。

**data.taskId** `string`

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

### 狀態回應欄位

**taskId** `string`

任務的唯一識別碼。

**state** `string`

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

**resultJson** `string`

JSON 字串包含 `resultUrls` 陣列。當 `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/nano-banana-2-1" \
  -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": "image-to-image",
      "prompt": "Turn this product photo into a polished studio ad",
      "image_urls": [
        "https://example.com/product.png"
      ],
      "aspect_ratio": "1:1",
      "resolution": "2K"
    }
  }'
```

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

## 計費

市場參考價等於標準 APIXO 價格乘以 1.30，四捨五入至小數點後三位。它們是比較值，而不是即時第三方報價或收費金額。

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

Nano Banana 2.1 依產生的影像計費。所選的`resolution`決定單價。

| 解析度 | APIXO 價格 | 市場參考價 |
| - | -: | -: |
| `1K` | `$0.04 / image` | `$0.052 / image` |
| `2K` | `$0.06 / image` | `$0.078 / image` |
| `4K` | `$0.135 / image` | `$0.176 / image` |

有關目前發布的定價，請參閱 [API 定價](https://apixo.ai/pricing/api)。

## 耗時與輪詢

實際延遲會因提示複雜性、參考影像可存取性、解析度和目前佇列負載而異。

| 階段 | 使用指引 |
| - | - |
| 第一次輪詢 | 任務建立後等待 10s-20s，然後再發出第一個狀態請求。 |
| 輪詢間隔 | 當 `state` 狀態為 `pending` 或 `processing` 時，每 10 秒輪詢一次。 |
| 生產交付 | 對於高並行度工作負載，建議使用回呼模式。 |

> [!TIP]
>
> 應立即下載並儲存重要輸出，避免僅依賴結果 URL 作為永久儲存。

速率限制和並發數因帳戶和 API 金鑰而異。若收到 `429`，請減慢請求並透過退避重試。

## 錯誤與排查

### HTTP 錯誤

| 代碼 | 含義 | 處理方式 |
| - | - | - |
| `400` | 請求主體、模式、參數或影像 URL 形狀無效 | 修正請求後再試 |
| `401` | 缺少 API 金鑰，或金鑰無效 | 檢查 `Authorization` 請求標頭 |
| `402` | 餘額或配額不足 | 充值或更換帳號或金鑰 |
| `403` | 您的金鑰無權存取該模型 | 檢查帳戶權限 |
| `429` | 已達到速率或並行限制 | 以指數退避方式重試 |
| `500` | 伺服器錯誤 | 逐步拉長間隔後重試 |
| `502` | 產生服務錯誤 | 逐步拉長間隔後重試 |
| `504` | 生成超時 | 重新嘗試；耗時較長的任務可改用回呼模式 |

### 任務失敗代碼

| 錯誤碼 | 含義 | 處理方式 |
| - | - | - |
| `PromptInvalid` | 提示詞無效或遭拒 | 修改提示 |
| `SensitiveContent` | 提示、引用或輸出違反安全策略 | 更改提示或參考影像 |
| `ImageFormatIncorrect` | 無法處理參考影像 | 使用通用影像格式的公共、可直接取得的影像 URL |
| `MissingParameter` | 缺少必需的參數 | 編輯時請確認`mode`、`prompt`、`image_urls` |
| `RateLimited` | 發電量暫時受到限制 | 逐步拉長間隔後重試 |
| `Timeout` | 生成超時 | 重試或使用回呼模式 |
| `Unknown error` | 任務因不明原因失敗 | 逐步拉長間隔後重試，或聯絡支援團隊並提供 `taskId` |

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

## MCP 整合

[完整模型 Schema](https://apixo.ai/docs/models/schemas/image/nano-banana-2-1.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)
* [Nano Banana](https://apixo.ai/docs/zh-hant/models/image/nano-banana.md)
* [Nano Banana Pro](https://apixo.ai/docs/zh-hant/models/image/nano-banana-pro.md)
* [Nano Banana 2](https://apixo.ai/docs/zh-hant/models/image/nano-banana-2.md)
* [在 APIXO Playground 嘗試 Nano Banana 2.1](https://apixo.ai/models/nano-banana-2-1)
* [API定價](https://apixo.ai/pricing/api)


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