# Grok Imagine Image 2.0 — Scenepond API

模型 ID: `xai/grok-imagine-image/v2.0/text-to-image`

模型开发者: xAI

用途: 文生图

原始页面: https://open.scenepond.ai/zh/models/xai/grok-imagine-image/v2.0/text-to-image#api

核对日期: 2026-10-11

API 地址: `https://open.scenepond.ai/v1`

每次请求使用 Authorization: Bearer 和 Scenepond API Key 认证；密钥保存在服务器。

## Scenepond 异步任务

`POST /v1/tasks`

### 最小调用

#### cURL

```bash
curl --fail-with-body -X POST "https://open.scenepond.ai/v1/tasks" \
  -H "Authorization: Bearer $SCENEPOND_API_KEY" \
  -H "Content-Type: application/json" \
  --data-binary @- <<'JSON'
{
  "model": "xai/grok-imagine-image/v2.0/text-to-image",
  "parameters": {
    "prompt": "A quiet lakeside at sunrise"
  }
}
JSON
```

#### Python

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

payload = json.loads("{\n  \"model\": \"xai/grok-imagine-image/v2.0/text-to-image\",\n  \"parameters\": {\n    \"prompt\": \"A quiet lakeside at sunrise\"\n  }\n}")

request = Request(
    "https://open.scenepond.ai/v1/tasks",
    headers={
        "Authorization": "Bearer " + os.environ["SCENEPOND_API_KEY"],
        "Content-Type": "application/json",
    },
    data=json.dumps(payload).encode("utf-8"),
    method="POST",
)
with urlopen(request) as response:
    result = json.load(response)
print(json.dumps(result, indent=2))
```

#### JavaScript

```javascript
const response = await fetch("https://open.scenepond.ai/v1/tasks", {
  method: "POST",
  headers: {
    "Authorization": `Bearer ${process.env.SCENEPOND_API_KEY}`,
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
  "model": "xai/grok-imagine-image/v2.0/text-to-image",
  "parameters": {
    "prompt": "A quiet lakeside at sunrise"
  }
})
});
if (!response.ok) throw new Error(`API error: ${response.status} ${await response.text()}`);
const result = await response.json();
console.log(result);
```

### 请求参数

- `model` — **必填**; `string`. 使用此处显示的完整公开模型 ID。
  - 约束: `const = "xai/grok-imagine-image/v2.0/text-to-image"`

- `parameters` — **必填**; `object`. 此模型的生成参数。

### parameters 中的字段

仅在提供 parameters 时适用；子字段必填不代表可选的父字段也必填。

- `parameters.output_format` — **可选**; `string`. 生成图片的编码格式。
  - 默认: `"jpeg"`
  - 约束: `enum = ["jpeg","png","webp"]`

- `parameters.quality` — **可选**; `string`. 期望的图片质量等级。
  - 默认: `"medium"`
  - 约束: `enum = ["low","medium"]`

- `parameters.resolution` — **可选**; `string`. 输出分辨率。允许值取决于此模型，并区分大小写。
  - 默认: `"1k"`
  - 约束: `enum = ["1k","2k"]`

- `parameters.aspect_ratio` — **可选**; `string`. 期望的输出宽高比。
  - 默认: `"1:1"`
  - 约束: `enum = ["2:1","20:9","19.5:9","16:9","4:3","3:2","1:1","2:3","3:4","9:16","9:19.5","9:20","1:2"]`

- `parameters.sync_mode` — **可选**; `boolean`. 仅支持 false，此网关使用异步任务。
  - 默认: `false`
  - 约束: `enum = [false]`

- `parameters.prompt` — **必填**; `string`. 描述主体、场景和期望生成的内容。
  - 约束: `maxLength = 8000`

- `parameters.count` — **可选**; `integer`. 本次任务请求生成的图片数量。
  - 默认: `1`
  - 约束: `minimum = 1`; `maximum = 4`


### 响应示例

响应示例仅展示结构；ID、URL 和数值为占位内容。

#### 提交响应

```json
{
  "task_id": "task_EXAMPLE",
  "model": "xai/grok-imagine-image/v2.0/text-to-image",
  "status": "queued",
  "progress": 0,
  "created_at": 1700000000,
  "finished_at": 0,
  "error": null,
  "links": {
    "self": "/v1/tasks/task_EXAMPLE",
    "artifacts": "/v1/tasks/task_EXAMPLE/artifacts"
  }
}
```

#### 已完成任务的状态

```json
{
  "task_id": "task_EXAMPLE",
  "model": "xai/grok-imagine-image/v2.0/text-to-image",
  "status": "succeeded",
  "progress": 100,
  "created_at": 1700000000,
  "finished_at": 1700000060,
  "error": null,
  "links": {
    "self": "/v1/tasks/task_EXAMPLE",
    "artifacts": "/v1/tasks/task_EXAMPLE/artifacts"
  }
}
```

#### 生成文件列表

```json
{
  "task_id": "task_EXAMPLE",
  "artifacts": [
    {
      "key": "image_0",
      "type": "image",
      "mime_type": "image/png",
      "content_url": "https://open.scenepond.ai/v1/tasks/task_EXAMPLE/artifacts/image_0/content",
      "expires_at": 1701209660
    }
  ]
}
```

#### 结果过期响应（API Key 鉴权）

```json
{
  "error": {
    "message": "Artifact content has expired or is no longer available",
    "type": "artifact_expired",
    "code": "artifact_expired"
  }
}
```

### 后续请求

#### 2. 查询任务状态

每隔几秒查询一次，直到 succeeded 或 failed。

`GET /v1/tasks/{task_id}`

##### cURL

```bash
curl --fail-with-body -X GET "https://open.scenepond.ai/v1/tasks/$SCENEPOND_TASK_ID" \
  -H "Authorization: Bearer $SCENEPOND_API_KEY"
```

##### Python

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

request = Request(
    "https://open.scenepond.ai/v1/tasks/" + quote(os.environ["SCENEPOND_TASK_ID"], safe="") + "",
    headers={
        "Authorization": "Bearer " + os.environ["SCENEPOND_API_KEY"],
        "Content-Type": "application/json",
    },
    method="GET",
)
with urlopen(request) as response:
    result = json.load(response)
print(json.dumps(result, indent=2))
```

##### JavaScript

```javascript
if (!process.env.SCENEPOND_TASK_ID) throw new Error("Set SCENEPOND_TASK_ID first");
const response = await fetch("https://open.scenepond.ai/v1/tasks/" + encodeURIComponent(process.env.SCENEPOND_TASK_ID) + "", {
  method: "GET",
  headers: {
    "Authorization": `Bearer ${process.env.SCENEPOND_API_KEY}`,
    "Content-Type": "application/json"
  }
});
if (!response.ok) throw new Error(`API error: ${response.status} ${await response.text()}`);
const result = await response.json();
console.log(result);
```

#### 3. 获取生成结果

任务 succeeded 后列出产物，并使用同一个 API Key 下载各个 content_url。

`GET /v1/tasks/{task_id}/artifacts`

##### cURL

```bash
curl --fail-with-body -X GET "https://open.scenepond.ai/v1/tasks/$SCENEPOND_TASK_ID/artifacts" \
  -H "Authorization: Bearer $SCENEPOND_API_KEY"
```

##### Python

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

request = Request(
    "https://open.scenepond.ai/v1/tasks/" + quote(os.environ["SCENEPOND_TASK_ID"], safe="") + "/artifacts",
    headers={
        "Authorization": "Bearer " + os.environ["SCENEPOND_API_KEY"],
        "Content-Type": "application/json",
    },
    method="GET",
)
with urlopen(request) as response:
    result = json.load(response)
print(json.dumps(result, indent=2))
```

##### JavaScript

```javascript
if (!process.env.SCENEPOND_TASK_ID) throw new Error("Set SCENEPOND_TASK_ID first");
const response = await fetch("https://open.scenepond.ai/v1/tasks/" + encodeURIComponent(process.env.SCENEPOND_TASK_ID) + "/artifacts", {
  method: "GET",
  headers: {
    "Authorization": `Bearer ${process.env.SCENEPOND_API_KEY}`,
    "Content-Type": "application/json"
  }
});
if (!response.ok) throw new Error(`API error: ${response.status} ${await response.text()}`);
const result = await response.json();
console.log(result);
```

#### 4. 到期前下载

将 SCENEPOND_TASK_ID 设为 task_id，将 SCENEPOND_ARTIFACT_KEY 设为 artifacts[].key。请在 expires_at 到期前下载二进制文件。内容接口支持 GET、HEAD 和 Range 请求；仅保存 content_url 无法保留媒体文件。

`GET /v1/tasks/{task_id}/artifacts/{artifact_key}/content`

##### cURL

```bash
curl --fail-with-body --output output.bin -X GET "https://open.scenepond.ai/v1/tasks/$SCENEPOND_TASK_ID/artifacts/$SCENEPOND_ARTIFACT_KEY/content" \
  -H "Authorization: Bearer $SCENEPOND_API_KEY"
```

##### Python

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

request = Request(
    "https://open.scenepond.ai/v1/tasks/" + quote(os.environ["SCENEPOND_TASK_ID"], safe="") + "/artifacts/" + quote(os.environ["SCENEPOND_ARTIFACT_KEY"], safe="") + "/content",
    headers={
        "Authorization": "Bearer " + os.environ["SCENEPOND_API_KEY"],
        "Content-Type": "application/json",
    },
    method="GET",
)
with urlopen(request) as response:
    with open("output.bin", "wb") as output:
        output.write(response.read())
print("Saved output.bin")
```

##### JavaScript

```javascript
import { writeFile } from "node:fs/promises";
if (!process.env.SCENEPOND_TASK_ID) throw new Error("Set SCENEPOND_TASK_ID first");
if (!process.env.SCENEPOND_ARTIFACT_KEY) throw new Error("Set SCENEPOND_ARTIFACT_KEY first");
const response = await fetch("https://open.scenepond.ai/v1/tasks/" + encodeURIComponent(process.env.SCENEPOND_TASK_ID) + "/artifacts/" + encodeURIComponent(process.env.SCENEPOND_ARTIFACT_KEY) + "/content", {
  method: "GET",
  headers: {
    "Authorization": `Bearer ${process.env.SCENEPOND_API_KEY}`,
    "Content-Type": "application/json"
  }
});
if (!response.ok) throw new Error(`API error: ${response.status} ${await response.text()}`);
await writeFile("output.bin", Buffer.from(await response.arrayBuffer()));
console.log("Saved output.bin");
```

### 调用注意事项

- 图片和视频使用同一套任务接口。选择模型后，系统自动选择渠道。
- 每次请求使用 Authorization: Bearer 和 Scenepond API Key 认证；密钥保存在服务器。
- 提交一次并保存 task_id，轮询至 succeeded 或 failed，再通过结果接口下载。
- 请求受理后返回任务 ID；部分任务可能在返回 ID 时已完成。
- 提交受理后返回 HTTP 202，Location 为 /v1/tasks/{task_id}。
- 生成产物保存 14 天。请在 expires_at 前下载；过期后无法恢复。
- POST 超时后请勿自动重提，任务可能已创建并产生费用。
- 参数和限制因模型而异，只接受此模型列出的字段。
- 生成的图片和视频只保存 14 天，请在到期前下载并保存到本地。
- 已存储的结果包含 expires_at，表示该文件到期时间的 Unix 时间戳（秒）。请逐个文件使用此字段，不要按任务创建时间推算。
- 部分历史结果可能没有 expires_at，这不代表永久保存，请尽快下载。
- 到期后，使用 API Key 鉴权且不带 access 查询参数的内容请求返回 HTTP 410，错误码为 artifact_expired。私有访问链接在内容不可用时可能返回 404。过期文件无法继续下载或恢复。
- 请勿公开 content_url。保存链接不等于保存文件，请在 expires_at 到期前将文件内容下载到自己的存储。
