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

# Grok Imagine Image 2.0 API：影像生成與編輯

> 使用 Grok Imagine Image 2.0 API 從文字產生一張影像或編輯具有最多五個參考的影像。

## 概覽

Grok Imagine Image 2.0支援文生圖生成和圖生圖編輯。每個成功的任務都會傳回一張影像。

| 功能 | 值 |
| - | - |
| 模型 ID | `grok-imagine-image-2-0` |
| 模式 | `text-to-image`, `image-to-image` |
| 提示詞 | 文字轉影像所需；圖生圖可選；最多 8,000 個字元 |
| 參考圖片 | 1–5 圖生圖的 URL |
| 縱橫比 | `1:1`、`2:3`、`3:2`、`16:9`、`9:16`；圖生圖也支援 `auto` |
| 輸出 | 一張圖片 URL 在 `resultJson.resultUrls` |

## 端點與身分驗證

API 基底網址：

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

| 方法 | 端點 | 用途 |
| - | - | - |
| `POST` | `/generateTask/grok-imagine-image-2-0` | 提交生成或編輯任務 |
| `GET` | `/statusTask/grok-imagine-image-2-0?taskId={taskId}` | 輪詢任務狀態並檢索結果 |

每個請求中包含 `Authorization: Bearer YOUR_API_KEY`，提交 JSON 時則需包含 `Content-Type: application/json`。

## 非同步請求範例

```bash theme={null}
curl -X POST "https://api.apixo.ai/api/v1/generateTask/grok-imagine-image-2-0" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "request_type": "async",
    "input": {
      "mode": "text-to-image",
      "prompt": "A small wooden cabin beside a lake at sunrise",
      "aspect_ratio": "16:9"
    }
  }'
```

任務受理後會傳回任務 ID，此時生成可能仍在進行中。

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

## 圖生圖

提供 1–5 個可公開存取的參考 URL。提示為選填項目；若提供，可用以描述所需的編輯。省略 `aspect_ratio` 時將使用 `auto`。

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

## 參數

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

結果交付方式。支援的值為 `async`、`callback`。省略此欄位或使用 `async` 來輪詢結果。

**callback\_url** `string`

當 `request_type` 為 `callback` 時必須提供。請提供一個公開的 HTTPS URL 用以接收最終任務的負載。詳情請見 [Webhooks](https://apixo.ai/docs/zh-hant/api-reference/webhooks.md)。

**input** `object` *required*

Grok Imagine Image 2.0 的輸入參數。

**屬性**

**mode** `string` *required*

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

**prompt** `string`

`text-to-image` 為必填且不得為空白；`image-to-image` 為可選。最多支援 8,000 個字元。對於影像編輯，可省略或提供編輯指令。

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

`image-to-image` 時必須提供：1–5 個可公開存取的圖片 URL，每一個皆為非空字串，並需維持預期的參考順序。`text-to-image` 無效。

**aspect\_ratio** `string`

輸出縱橫比。支援的值：`1:1`、`2:3`、`3:2`、`16:9`、`9:16`、`auto`。價值 `auto` 僅支援 `image-to-image`。預設為 `1:1` 用於 `text-to-image`，`auto` 用於 `image-to-image`。

## 輪詢結果

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

當 `data.state` 為 `pending` 或 `processing` 時繼續輪詢。於 `success` 或 `failed` 時停止。

成功回應：

```json theme={null}
{
  "code": 200,
  "message": "success",
  "data": {
    "taskId": "task_12345678",
    "state": "success",
    "resultJson": "{\"resultUrls\":[\"https://example.com/generated.png\"]}",
    "createTime": 1793318400000,
    "completeTime": 1793318440000,
    "costTime": 40000
  }
}
```

`resultJson` 是 JSON **字串**。請先解析它，再讀取圖片 URL。

```javascript theme={null}
const { resultUrls } = JSON.parse(response.data.resultJson);
const imageUrl = resultUrls[0];
```

### 狀態回應欄位

| `data` 中的欄位 | 型別 | 說明 |
| - | - | - |
| `taskId` | string | 提交時傳回的任務識別碼 |
| `state` | string | `pending`、`processing`、`success` 或 `failed` |
| `resultJson` | string | 成功時包含 `resultUrls` 的 JSON 字串 |
| `failCode` | string | 任務失敗時的錯誤代碼 |
| `failMsg` | string | 任務失敗時的錯誤訊息 |
| `createTime` | integer | 任務建立時間戳記（以 Unix 毫秒為單位） |
| `completeTime` | integer | 任務完成時間戳記（以 Unix 毫秒為單位）（如果可用） |
| `costTime` | integer | 任務總持續時間（以毫秒為單位）（如果可用） |

### 失敗的任務範例

```json theme={null}
{
  "code": 200,
  "message": "success",
  "data": {
    "taskId": "task_12345678",
    "state": "failed",
    "failCode": "500",
    "failMsg": "Image generation failed."
  }
}
```

成功的狀態查找可以描述失敗的生成。請確認 `data.state`，並在請求支援時保留任務 ID。

## Webhook 回呼模式

將 `request_type` 設定為 `callback` 並提供 `callback_url`；模型輸入不變。

```json theme={null}
{
  "request_type": "callback",
  "callback_url": "https://your-server.com/webhooks/apixo",
  "input": {
    "mode": "text-to-image",
    "prompt": "An illustrated travel poster of a quiet seaside town",
    "aspect_ratio": "2:3"
  }
}
```

Webhook 交付使用與輪詢相同的公共最終任務欄位。請參閱 [Webhooks](https://apixo.ai/docs/zh-hant/api-reference/webhooks.md) 以了解交付和重試要求。

## 計費

兩種模式的每個生成影像的成本為 **\$0.04**，每個成功的任務只有一個輸出影像。參考影像不新增輸入費用。失敗的任務將被退款。

| 模式 | APIXO 價格 | 市場參考價 |
| - | - | - |
| `text-to-image` | `$0.040 / image` | `$0.060 / image` |
| `image-to-image` | `$0.040 / image` | `$0.060 / image` |

市場參考價僅供比較，不影響實際收費。

新的非 LLM 請求使用 **1 點 = `$0.001`**。計算全部請求金額，套用任何適用的帳戶折扣，然後將總額無條件進位到下一個完整積分。商定的固定單價保留其精度並取代標準價格和折扣；最終無條件進位仍然適用。如果沒有折扣，一項成功的任務需要花費 40 個積分。請參閱[點數換算與進位](https://apixo.ai/docs/zh-hant/pricing.md#credit-conversion-and-rounding)和[當前API價格](https://apixo.ai/pricing/api)。

## 耗時與輪詢

生成時間隨提示複雜性、影像存取和佇列負載而變化。以下間隔是客戶建議，而不是延遲保證。

| 模式 | 建議首次查詢時間 | 輪詢間隔 |
| - | - | - |
| `text-to-image` | 任務創建後 10 秒 | 3–5 秒，有退避 |
| `image-to-image` | 任務創建後 10 秒 | 3–5 秒，有退避 |

使用回呼模式來取得完成通知。在最終狀態停止輪詢，並及時保存重要輸出。

## 錯誤與排查

* 缺少 `mode`、缺少或空白的文生圖 `prompt`、提示過長、寬高比無效或圖生圖參考清單無效會導致驗證錯誤。
* 使用無需身分驗證即可存取的參考 URL。如果生成失敗，請檢查`failCode`和`failMsg`。
* 檢查您的 API 金鑰是否有身分驗證錯誤，以及您的餘額是否有餘額不足錯誤。
* 放棄速率限制。不要因為任務仍在處理中而重新提交已接受的任務；繼續輪詢其任務 ID。

請參閱 [錯誤代碼](https://apixo.ai/docs/zh-hant/api-reference/errors.md) 以了解共享 API 錯誤。

## MCP 整合

[完整模型架構](https://apixo.ai/docs/models/schemas/image/grok-imagine-image-2-0.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)
* [Grok Imagine Image](https://apixo.ai/docs/zh-hant/models/image/grok-imagine-image.md)
* [Webhook](https://apixo.ai/docs/zh-hant/api-reference/webhooks.md)
* [定價](https://apixo.ai/docs/zh-hant/pricing.md)
* [嘗試Grok Imagine Image 2.0](https://apixo.ai/models/grok-imagine-image-2-0)


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