# Grok Imagine Image 2.0 · API 接入

API 模型 ID 为 `grok-imagine-image-2.0`。本文说明平台的实际契约，调用前请核对实时目录中的可用性。

## 请求示例

平台地址在构建时填入，运行前设置 API_KEY 为服务端密钥。

```bash
curl "https://newrouters.com/v1/tasks" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  --data '{
  "model": "grok-imagine-image-2.0",
  "input": {
    "resolution": "1k",
    "aspect_ratio": "auto",
    "output_format": "jpeg",
    "prompt": "A small red house in a quiet garden"
  }
}'
```

## 查询结果

创建响应表示任务已受理，不是最终结果。用返回的 ID 替换 TASK_ID，轮询至 succeeded 或 failed。

```bash
curl "https://newrouters.com/v1/tasks/TASK_ID" \
  -H "Authorization: Bearer $API_KEY"
```

成功查看 result，失败查看 error。上游结果未知时保留原任务 ID，不盲目重复提交付费生成。

## 参数说明

| 字段 | 类型 | 要求 | 默认值 | 限制 |
| --- | --- | --- | --- | --- |
| `images` | array | 可选 | — | — |
| `prompt` | string | 必填 | — | 最大长度 100000 |
| `resolution` | enum | 可选 | `"1k"` | `1k`, `2k` |
| `aspect_ratio` | enum | 可选 | `"auto"` | `auto`, `1:1`, `16:9`, `9:16`, `4:3`, `3:4`, `3:2`, `2:3`, `2:1`, `1:2`, `19.5:9`, `9:19.5`, `20:9`, `9:20` |
| `output_format` | enum | 可选 | `"jpeg"` | `png`, `jpeg`, `webp` |








<!-- official-reference:start -->
## 官方来源与模型身份

核对日期: 2026-09-27 · 平台模型: `grok-imagine-image-2.0` · 官方对应: `grok-imagine-image-2.0`

- [grok-imagine-image-2.0 official reference](https://docs.x.ai/developers/models/grok-imagine-image-2.0)
- [xAI Imagine API](https://docs.x.ai/developers/model-capabilities/imagine)
- [xAI JSON image editing](https://docs.x.ai/developers/model-capabilities/images/editing)
<!-- official-reference:end -->

## 计费与排错

接入时核对当前可用性、契约和账号价格。排错时保留请求 ID 与消费凭据。 [完整 API 参考](/zh/docs/api)

## 多语言请求示例

### JavaScript

```javascript
const apiBaseUrl = "https://newrouters.com";
const apiKey = process.env.API_KEY;
if (!apiKey) throw new Error("Set API_KEY in your server environment");

const response = await fetch(apiBaseUrl + "/v1/tasks", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "Authorization": "Bearer " + apiKey
  },
  body: JSON.stringify({
  "model": "grok-imagine-image-2.0",
  "input": {
    "resolution": "1k",
    "aspect_ratio": "auto",
    "output_format": "jpeg",
    "prompt": "A small red house in a quiet garden"
  }
})
});

const result = await response.json();
if (!response.ok) throw new Error(JSON.stringify(result));
console.log(result);
```

### Python

```python
import json
import os
from urllib.request import Request, urlopen

api_base_url = "https://newrouters.com"
api_key = os.environ["API_KEY"]
body = json.loads("{\"model\":\"grok-imagine-image-2.0\",\"input\":{\"resolution\":\"1k\",\"aspect_ratio\":\"auto\",\"output_format\":\"jpeg\",\"prompt\":\"A small red house in a quiet garden\"}}")

request = Request(
    api_base_url + "/v1/tasks",
    data=json.dumps(body).encode("utf-8"),
    headers={
    "Content-Type": "application/json",
    "Authorization": "Bearer " + api_key
    },
    method="POST",
)
with urlopen(request) as response:
    print(json.load(response))
```

## 响应字段节选（示意值）

### HTTP 202

```json
{
  "id": "TASK_ID",
  "status": "pending",
  "model": "grok-imagine-image-2.0",
  "result": null,
  "error": null
}
```

### HTTP 200

```json
{
  "id": "TASK_ID",
  "status": "succeeded",
  "model": "grok-imagine-image-2.0",
  "result": {
    "assets": [
      "https://example.com/output.png"
    ]
  },
  "error": null
}
```


