# Grok Imagine 图片/视频生成 API 对接文档

## 概述

Grok Imagine 图片/视频生成接口，支持文生图、图生图、文生视频、图生视频四种模式。

**Base URL**: `https://api.apiverse.ai`

---

## 认证方式

所有接口均需要在请求头中携带 Token 进行认证：

```
Authorization: Bearer {YOUR_AUTH_TOKEN}
```

## 快速开始

### cURL 示例

**创建 Grok 任务（文生图）**
```bash
curl -X POST "https://api.apiverse.ai/api/v2/open/aigc/grok" \
  -H "Authorization: Bearer your_auth_token_here" \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "A beautiful sunset over the ocean, oil painting style",
    "aspectRatio": "16:9",
    "enablePro": true
  }'
```

**创建 Grok 任务（图生图）**
```bash
curl -X POST "https://api.apiverse.ai/api/v2/open/aigc/grok" \
  -H "Authorization: Bearer your_auth_token_here" \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "Transform the image into watercolor style",
    "genType": "i2i",
    "imageUrls": ["https://example.com/reference.jpg"]
  }'
```

**创建 Grok 任务（文生视频）**
```bash
curl -X POST "https://api.apiverse.ai/api/v2/open/aigc/grok" \
  -H "Authorization: Bearer your_auth_token_here" \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "A couple dancing in a bed",
    "genType": "t2v",
    "mode": "normal",
    "duration": 6,
    "resolution": "480p"
  }'
```

**创建 Grok 任务（图生视频）**
```bash
curl -X POST "https://api.apiverse.ai/api/v2/open/aigc/grok" \
  -H "Authorization: Bearer your_auth_token_here" \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "Make the person in the image dance",
    "genType": "i2v",
    "imageUrls": ["https://example.com/reference.jpg"],
    "mode": "fun",
    "duration": 6
  }'
```

**查询任务状态**
```bash
curl -X GET "https://api.apiverse.ai/api/v2/open/aigc/task_abc123" \
  -H "Authorization: Bearer your_auth_token_here"
```

**查询账户余额**
```bash
curl -X GET "https://api.apiverse.ai/api/v2/open/balance" \
  -H "Authorization: Bearer your_auth_token_here"
```

---

## 接口列表

### 1. 创建 Grok Imagine 任务

**POST** `/api/v2/open/aigc/grok`

创建一个 Grok Imagine 图片或视频生成任务。

**Content-Type**: `application/json`

#### 请求参数

| 参数 | 类型 | 必填 | 说明 |
|-----|------|-----|------|
| prompt | string | 是 | 图片/视频描述提示词 |
| genType | string | 否 | 生成类型：`t2i`(文生图,默认) / `i2i`(图生图) / `t2v`(文生视频) / `i2v`(图生视频) |
| imageUrls | string[] | 条件 | 参考图片 URL 列表，图生图/图生视频时必填 |
| base64File | string | 否 | Base64 编码的图片数据，支持 jpg/png/gif/webp，最大 10MB |
| base64FileList | string[] | 否 | Base64 编码的多张图片数据，每张支持 jpg/png/gif/webp，单张最大 10MB |
| aspectRatio | string | 否 | 宽高比：`2:3` / `3:2` / `1:1` / `16:9` / `9:16`，默认 `1:1` |
| enablePro | boolean | 否 | 质量模式（仅文生图有效），true=高质量，false=速度优先 |
| mode | string | 否 | 运动风格（仅视频有效）：`fun` / `normal`(默认) / `spicy` |
| duration | integer | 否 | 视频时长（仅视频有效）：6/10/15/20/30秒，默认6秒 |
| resolution | string | 否 | 视频分辨率（仅视频有效）：`480p`(默认) / `720p` |
| callbackUrl | string | 否 | 任务完成后的回调通知 URL |

> **说明**：
> - `base64File`、`imageUrls`、`base64FileList` 可同时使用，图片处理顺序为：`base64File`（单张，放最前面）→ `imageUrls`（URL列表）→ `base64FileList`（多张base64，追加到末尾）
> - 图生图/图生视频时必须提供参考图片
> - 视频参数（mode/duration/resolution）仅在 `t2v` 和 `i2v` 模式下生效
> - 创建任务时会预扣费，余额不足将返回错误

#### 请求示例

**文生图：**
```json
{
  "prompt": "A beautiful sunset over the ocean, oil painting style",
  "aspectRatio": "16:9",
  "enablePro": true
}
```

**图生图：**
```json
{
  "prompt": "Transform this image into watercolor style",
  "genType": "i2i",
  "imageUrls": ["https://example.com/reference.jpg"]
}
```

**文生视频：**
```json
{
  "prompt": "A couple dancing in a romantic setting",
  "genType": "t2v",
  "aspectRatio": "16:9",
  "mode": "normal",
  "duration": 10,
  "resolution": "720p"
}
```

**图生视频：**
```json
{
  "prompt": "Make the person wave their hand",
  "genType": "i2v",
  "imageUrls": ["https://example.com/reference.jpg"],
  "mode": "fun",
  "duration": 6,
  "resolution": "480p"
}
```

#### 响应参数

| 参数 | 类型 | 说明 |
|-----|------|------|
| code | int | 状态码，0 表示成功 |
| msg | string | 状态信息 |
| data.taskId | string | 任务 ID，用于查询任务状态 |
| data.status | string | 任务状态，创建时固定为 `processing` |
| data.createdAt | string | 创建时间 |

#### 响应示例

**成功**
```json
{
  "code": 0,
  "msg": "success",
  "data": {
    "taskId": "task_20260422103000_abc12345",
    "status": "processing",
    "createdAt": "2026-04-22 10:30:00"
  }
}
```

**余额不足**
```json
{
  "code": 40001,
  "msg": "余额不足，当前余额: 0.0100 USD，需要: 0.0750 USD",
  "data": null
}
```

---

### 2. 查询任务状态

**GET** `/api/v2/open/aigc/{taskId}`

查询单个任务的执行状态。

#### 路径参数

| 参数 | 类型 | 必填 | 说明 |
|-----|------|-----|------|
| taskId | string | 是 | 任务 ID |

#### 响应参数

| 参数 | 类型 | 说明 |
|-----|------|------|
| code | int | 状态码，0 表示成功 |
| msg | string | 状态信息 |
| data.taskId | string | 任务 ID |
| data.status | string | 任务状态：`processing` / `success` / `failed` |
| data.result | string[] | 生成的图片/视频 URL 列表（成功时返回） |
| data.errorMsg | string | 错误信息（失败时返回） |
| data.createdAt | string | 创建时间 |
| data.updatedAt | string | 更新时间 |

#### 响应示例

**处理中**
```json
{
  "code": 0,
  "msg": "success",
  "data": {
    "taskId": "task_20260422103000_abc12345",
    "status": "processing",
    "createdAt": "2026-04-22 10:30:00",
    "updatedAt": "2026-04-22 10:30:05"
  }
}
```

**成功**
```json
{
  "code": 0,
  "msg": "success",
  "data": {
    "taskId": "task_20260422103000_abc12345",
    "status": "success",
    "result": [
      "https://fc-gw-sh.oss-accelerate.aliyuncs.com/images/2026/04/22/output_001.png"
    ],
    "createdAt": "2026-04-22 10:30:00",
    "updatedAt": "2026-04-22 10:31:30"
  }
}
```

**失败**
```json
{
  "code": 0,
  "msg": "success",
  "data": {
    "taskId": "task_20260422103000_abc12345",
    "status": "failed",
    "errorMsg": "生成失败：内容不符合规范",
    "createdAt": "2026-04-22 10:30:00",
    "updatedAt": "2026-04-22 10:30:45"
  }
}
```

---

### 3. 批量查询任务状态

**POST** `/api/v2/open/aigc/batch`

批量查询多个任务的执行状态（最多 100 个）。

#### 请求参数

| 参数 | 类型 | 必填 | 说明 |
|-----|------|-----|------|
| taskIds | string[] | 是 | 任务 ID 列表，最多 100 个 |

#### 请求示例

```json
{
  "taskIds": ["task_20260422103000_abc12345", "task_20260422103200_def67890"]
}
```

#### 响应示例

```json
{
  "code": 0,
  "msg": "success",
  "data": {
    "tasks": [
      {
        "taskId": "task_20260422103000_abc12345",
        "status": "success",
        "result": ["https://fc-gw-sh.oss-accelerate.aliyuncs.com/images/output_001.png"],
        "createdAt": "2026-04-22 10:30:00",
        "updatedAt": "2026-04-22 10:31:30"
      },
      {
        "taskId": "task_20260422103200_def67890",
        "status": "processing",
        "createdAt": "2026-04-22 10:32:00",
        "updatedAt": "2026-04-22 10:32:05"
      }
    ]
  }
}
```

---

### 4. 查询账户余额

**GET** `/api/v2/open/balance`

查询当前用户的账户余额。

#### 响应参数

| 参数 | 类型 | 说明 |
|-----|------|------|
| code | int | 状态码，0 表示成功 |
| msg | string | 状态信息 |
| data.userId | string | 用户 ID |
| data.balance | string | 可用余额（USD） |
| data.frozenBalance | string | 冻结余额（USD） |
| data.currency | string | 货币类型 |

#### 响应示例

```json
{
  "code": 0,
  "msg": "success",
  "data": {
    "userId": "user@example.com",
    "balance": "19.9250",
    "frozenBalance": "0.0750",
    "currency": "USD"
  }
}
```

---

## 回调通知

当任务完成（成功或失败）时，如果创建任务时提供了 `callbackUrl`，系统会向该 URL 发送 POST 请求。

### 回调请求

**Headers**
```
Content-Type: application/json
X-Funcloud-Event: task.completed
X-Funcloud-Signature: {签名}
```

**Body**
```json
{
  "event": "task.completed",
  "taskId": "task_20260422103000_abc12345",
  "status": "success",
  "result": ["https://fc-gw-sh.oss-accelerate.aliyuncs.com/images/output_001.png"],
  "errorMsg": "",
  "timestamp": "2026-04-22T10:31:30+08:00",
  "signature": "a1b2c3d4e5f6..."
}
```

### 回调响应

接收方应返回 HTTP 2xx 状态码表示成功接收：

```json
{
  "code": 0,
  "msg": "ok"
}
```

---

## 运动风格说明（mode 参数）

| 值 | 说明 |
|-----|------|
| fun | 有趣风格，动作幅度较大，适合卡通/搞笑场景 |
| normal | 正常风格（默认），自然流畅的运动 |
| spicy | 强烈风格，更具戏剧性的动作效果 |

---

## 错误码

| code | 说明 |
|------|------|
| 0 | 成功 |
| 10002 | 参数缺失或格式错误 |
| 10005 | API Key 无效或缺失 |
| 30003 | 任务不存在 |
| 40001 | 余额不足 |
| 90003 | 服务器内部错误 |

---

## 最佳实践

### 1. 轮询策略

建议的轮询间隔：
- 前 30 秒：每 3 秒查询一次
- 30 秒 ~ 2 分钟：每 5 秒查询一次
- 2 分钟后：每 10 秒查询一次

### 2. 使用回调

对于生产环境，建议使用回调通知而非轮询，可以：
- 减少 API 调用次数
- 更快获得结果通知
- 降低服务器压力

### 3. 处理时间参考

- 文生图：通常 10 ~ 30 秒
- 图生图：通常 10 ~ 30 秒
- 文生视频：通常 1 ~ 3 分钟（根据时长）
- 图生视频：通常 1 ~ 3 分钟（根据时长）

### 4. 余额管理

- 创建任务前建议先查询余额，避免因余额不足导致任务失败
- 任务成功后会从冻结余额中扣费
- 任务失败后冻结金额会自动退还到可用余额