> ## 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 API：xAI 文字生成圖片與以圖生圖

> xAI 影像生成 API，用於文生圖和圖生圖的生成，具有快速週轉、靈活的尺寸和提示控制。

## 概覽

模型 ID 仍為 `grok-image`。請使用下列首選的 REST URL。先前的 `/generateTask/grok-image` 與 `/statusTask/grok-image` URL 仍受支援，且參數與定價不變。

Grok Imagine Image 是 xAI 的影像模型，可以根據文字生成影像，也能參考多張圖片生成新圖。在 APIXO Playground 試用後，可依本頁範例呼叫 API。

| 功能 | 值 |
| - | - |
| 模型 ID | `grok-image` |
| 模式 | `text-to-image`, `image-to-image` |
| 提示詞長度 | 1 至 5000 個字元 |
| 參考圖片 | `image-to-image` 的 1-5 個 URL |
| 縱橫比 | `1:1`、`3:2`、`2:3`、`16:9`、`9:16` 對應 `text-to-image` |
| 輸出 | 影像 URL 位於 `resultJson.resultUrls` |

## 端點與身分驗證

API 基底網址：

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

| 方法 | 端點 | 用途 |
| - | - | - |
| `POST` | `/generateTask/grok-imagine-image` | 送出生成任務 |
| `GET` | `/statusTask/grok-imagine-image?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/grok-imagine-image" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "request_type": "async",
    "input": {
      "mode": "text-to-image",
      "prompt": "a cozy cyberpunk cafe with neon lights",
      "aspect_ratio": "3:2"
    }
  }'
```

成功回應：

```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/grok-imagine-image?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": 1767965635929,
    "costTime": 25000
  }
}
```

任務失敗時的回應範例：

```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,
    "costTime": 9039
  }
}
```

等 `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 serene mountain landscape at dawn",
    "aspect_ratio": "16:9"
  }
}
```

### 圖生圖

```json theme={null}
{
  "request_type": "async",
  "input": {
    "mode": "image-to-image",
    "prompt": "blend these references into a cinematic portrait",
    "image_urls": [
      "https://example.com/subject.png",
      "https://example.com/style.png"
    ]
  }
}
```

## 參數

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

指定取得結果的方式。省略此欄位或設為 `async` 時，可透過 `statusTask` 輪詢；設為 `callback` 時，透過 Webhook 接收。

**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 的輸入參數。

**屬性**

**mode** `string` *required*

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

**prompt** `string` *required*

用文字描述想生成的影像或想修改的內容。長度須為 1 至 5000 個字元。提示詞為空時，請求會遭拒。

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

`text-to-image` 的輸出縱橫比。支援的值：`1:1`、`3:2`、`2:3`、`16:9`、`9:16`。不會為 `image-to-image` 轉送此參數。

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

參考影像 URL。`image-to-image` 所需。支援 1-5 個 URL。每個 URL 必須是公開的，且每個陣列項目必須是非空字串。支援的格式為 JPG、JPEG、PNG 與 WEBP，每張影像的最大尺寸為 10 MB。

## 回應格式

### 送出任務時的回應

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

**code** `integer`

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

**message** `string`

狀態說明。

**data.taskId** `string`

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

### 狀態回應欄位

**taskId** `string`

任務的唯一識別碼。

**state** `string`

目前的任務狀態，可能的值為 `pending`、`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/grok-imagine-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",
      "prompt": "a clean product photo of wireless earbuds on soft stone",
      "aspect_ratio": "1:1"
    }
  }'
```

回呼傳遞使用與狀態回應相同的最終有效負載形狀。請參閱 [Webhooks](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)。

以下價格與主要 [API Pricing](https://apixo.ai/pricing/api) 目錄的公共基本價格表（2026 年 10 月 30 日更新）相符。在製定預算前，請確認該頁面的當前費率及任何帳戶折扣。

文字轉影像按請求計費一次，預設回傳兩張影像。請求價格涵蓋這些輸出。圖生圖按生成的影像計費。

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

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

例如，傳回預設兩張影像的文字轉影像請求在任何帳戶折扣之前總共花費 `$0.040`。

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

## 耗時與輪詢

實際延遲可能會因提示複雜性、參考影像存取、佇列負載和結果儲存時間而異。

| 模式 | 一般生成時間 | 建議首次查詢時間 | 輪詢間隔 |
| - | - | - | - |
| `text-to-image` | 平均約 25s | 任務建立 15 秒後 | 3s |
| `image-to-image` | 平均約 25s | 任務建立 15 秒後 | 3s |

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

結果 URL 的有效期限為 15 天。及時下載並儲存重要輸出。

提示可以用英文或中文書寫。更具體的場景、風格、燈光和構圖細節通常會產生更好的效果。預設輸出為PNG；如果您需要透明度，請在提示中描述該要求。

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

## 錯誤與排查

### HTTP 錯誤

| 代碼 | 含義 | 處理方式 |
| - | - | - |
| `400` | 請求主體無效、缺少 `mode`、缺少 `prompt`、無效 `aspect_ratio`、格式錯誤 `image_urls` 或影像無法存取 URL | 修正請求後再試 |
| `401` | 缺少 API 金鑰，或金鑰無效 | 檢查 `Authorization` 請求標頭 |
| `402` | 餘額或配額不足 | 充值或更換帳號或金鑰 |
| `429` | 已達到速率或並行限制 | 以指數退避方式重試 |
| `500` | 內部錯誤或未知任務失敗 | 重試後退或聯絡支援人員（如果問題仍然存在） |
| `502` | 服務或網路錯誤 | 逐步拉長間隔後重試 |

### 驗證說明

| 參數 | 後端行為 |
| - | - |
| `input.mode` | 路由所需。必須是`text-to-image`或`image-to-image`。 |
| `input.prompt` | 必需的字串。不能為空且不能超過 5000 個字元。 |
| `input.aspect_ratio` | 可選用於 `text-to-image`。必須是 `1:1`, `3:2`, `2:3`, `16:9`， 或者 `9:16` 如果提供的話。預設為 `1:1` 為了 `text-to-image`。 |
| `input.image_urls` | `image-to-image` 為必填且非空白。必須是字串陣列，且不能包含超過 5 個影像。 |

### 任務失敗代碼

`failCode` 表示任務失敗的原因，常見值如下。

| 錯誤碼 | 含義 | 處理方式 |
| - | - | - |
| `PromptInvalid` | 提示詞無效或遭拒 | 修改提示詞後再試一次 |
| `SensitiveContent` | 提示或輸入/輸出內容被安全檢查拒絕 | 更改提示或參考影像 |
| `ImageFormatIncorrect` | 無法處理參考影像格式 | 請提供可公開存取的圖片直連網址，並使用常見的圖片格式 |
| `RateLimited` | 請求頻率已達上限 | 以指數退避方式重試 |
| `Timeout` | 任務逾時 | 稍後重試或使用回呼模式 |
| `Unknown error` | 任務因不明原因失敗 | 逐步拉長間隔後重試，或聯絡支援團隊並提供 `taskId` |

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

若要在生成後對影像進行動畫處理，請將成功的 `grok-image` 任務與 [Grok Imagine Video](https://apixo.ai/docs/zh-hant/models/video/grok-imagine-video.md#image-to-video-from-a-grok-imagine-image-task) 結合使用。這會根據影像結果建立一個新影片；它不會擴展現有影片。

## MCP 整合

[完整模型 Schema](https://apixo.ai/docs/models/schemas/image/grok-imagine-image.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 嘗試 Grok Imagine Image](https://apixo.ai/models/grok-image)
* [API定價](https://apixo.ai/pricing/api)


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