> ## 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/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，每个 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/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/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/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/models.md)
* [图像模型](https://apixo.ai/docs/zh/models/image)
* [Grok Imagine Image](https://apixo.ai/docs/zh/models/image/grok-imagine-image.md)
* [Webhook](https://apixo.ai/docs/zh/api-reference/webhooks.md)
* [定价](https://apixo.ai/docs/zh/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.