> ## 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.

# 查询任务状态 API · 按任务 ID 轮询生成结果

> 使用模型和任务 ID 轮询 APIXO 生成任务，获取状态、失败详情、计费信息和最终结果链接。

在 URL 路径中使用原先提交任务时的模型 ID，将响应中的 `data.taskId` 作为查询参数 `taskId`。要查询批次 ID，请使用[查询批次状态](https://apixo.ai/docs/zh/api-reference/batch-status.md)。

| 返回状态 | 客户端应如何处理 |
| - | - |
| `pending` 或 `processing` | 保留任务 ID，稍后再次轮询。 |
| `success` | 停止轮询，按提交时所用模型的结果格式解析 `data.resultJson`。 |
| `failed` | 停止轮询。记录 `failCode` 和 `failMsg`，并参照[错误码](https://apixo.ai/docs/zh/api-reference/errors.md)判断如何恢复。 |

## 端点

```
GET https://api.apixo.ai/api/v1/statusTask/{model}?taskId={taskId}
```

## 参数

**model** `string` *required*

提交任务时使用的模型 ID，例如 `nano-banana` 或 `flux-2`。

**taskId** `string` *required*

[提交生成任务](https://apixo.ai/docs/zh/api-reference/generate-task.md)端点返回的任务 ID。

## 请求头

**Authorization** `string` *required*

用于 API 身份验证的 Bearer 令牌。格式为 `Bearer YOUR_API_KEY`。

## 响应

```json theme={null}
{
  "code": 200,
  "message": "success",
  "data": {
    "taskId": "task_abc123xyz789",
    "state": "success",
    "resultJson": "{\"resultUrls\":[\"https://cdn.apixo.ai/output/abc123.jpg\"]}",
    "costTime": 12500,
    "createTime": 1704067200000,
    "completeTime": 1704067212500
  }
}
```

### 任务状态

| 状态 | 说明 |
| - | - |
| `pending` | 已接收任务，等待处理 |
| `processing` | AI 模型正在处理任务 |
| `success` | 生成成功 |
| `failed` | 生成失败，请查看错误详情 |

### 响应字段

**taskId** `string`

任务的唯一标识符。

**state** `string`

当前任务状态，可能的值为 `pending`、`processing`、`success`、`failed`。

**resultJson** `string`

包含 `resultUrls` 数组的 JSON 字符串。只有 `state` 为 `success` 时才会返回。使用 `JSON.parse()` 解析。

**costTime** `integer`

处理耗时，单位为毫秒。仅在处理完成时返回。

**createTime** `integer`

任务创建时间戳（Unix 毫秒）。

**completeTime** `integer`

任务完成时间戳（Unix 毫秒）。仅在完成时返回。

**failCode** `string`

错误码。只有 `state` 为 `failed` 时才会返回。

**failMsg** `string`

便于阅读的错误消息。只有 `state` 为 `failed` 时才会返回。

**billing** `object`

可选的最终计费快照。仅当模型在任务成功结束时的响应中提供结算详情时，才会返回此字段。

**billing.settlementState** `string`

计费快照的结算状态。对于已结算的数据，值为 `final`。

**billing.unit** `string`

基于时长计费字段使用的单位，例如 `second`。

## 各状态的响应示例

### 等待处理或处理中

```json theme={null}
{
  "code": 200,
  "message": "success",
  "data": {
    "taskId": "task_abc123xyz789",
    "state": "processing",
    "createTime": 1704067200000
  }
}
```

### 成功

```json theme={null}
{
  "code": 200,
  "message": "success",
  "data": {
    "taskId": "task_abc123xyz789",
    "state": "success",
    "resultJson": "{\"resultUrls\":[\"https://cdn.apixo.ai/output/abc123.jpg\"]}",
    "costTime": 12500,
    "createTime": 1704067200000,
    "completeTime": 1704067212500
  }
}
```

**解析 resultJson**

```javascript theme={null}
const result = JSON.parse(data.resultJson);
const urls = result.resultUrls; // Array of generated content URLs
```

### 失败

```json theme={null}
{
  "code": 200,
  "message": "success",
  "data": {
    "taskId": "task_abc123xyz789",
    "state": "failed",
    "failCode": "CONTENT_VIOLATION",
    "failMsg": "Content violates usage policy",
    "createTime": 1704067200000,
    "completeTime": 1704067205000
  }
}
```

> [!NOTE]
>
> HTTP `200` 且返回 `state: "failed"` 表示 API 请求成功，但任务本身执行失败。请查看 `failCode` 和 `failMsg` 确认原因。所有错误码请参见[错误码](https://apixo.ai/docs/zh/api-reference/errors.md)。

## 结果链接

`resultUrls` 数组包含生成内容的直达链接。

```json theme={null}
{
  "resultUrls": [
    "https://cdn.apixo.ai/output/abc123.jpg",
    "https://cdn.apixo.ai/output/abc124.jpg"
  ]
}
```

> [!WARNING]
>
> 生成的文件默认保留 15 天，实际保留期可能因 API 密钥的存储设置而异。请尽快下载并保存重要结果。

## 示例

> [tab] cURL
>
> ```bash theme={null}
> curl "https://api.apixo.ai/api/v1/statusTask/nano-banana?taskId=task_abc123" \
>   -H "Authorization: Bearer YOUR_API_KEY"
> ```

> [tab] JavaScript
>
> ```javascript theme={null}
> const response = await fetch(
>   'https://api.apixo.ai/api/v1/statusTask/nano-banana?taskId=task_abc123',
>   {
>     headers: { 'Authorization': 'Bearer YOUR_API_KEY' },
>   }
> );
> const { data } = await response.json();
>
> if (data.state === 'success') {
>   const urls = JSON.parse(data.resultJson).resultUrls;
>   console.log('Generated:', urls);
> }
> ```

> [tab] Python
>
> ```python theme={null}
> import json
>
> response = requests.get(
>     'https://api.apixo.ai/api/v1/statusTask/nano-banana',
>     params={'taskId': 'task_abc123'},
>     headers={'Authorization': 'Bearer YOUR_API_KEY'},
> )
>
> data = response.json()['data']
>
> if data['state'] == 'success':
>     urls = json.loads(data['resultJson'])['resultUrls']
>     print(f'Generated: {urls}')
> ```

## 轮询示例

> [tab] JavaScript
>
> ```javascript theme={null}
> async function waitForResult(model, taskId, apiKey) {
>   const maxAttempts = 60;
>   const pollInterval = 3000; // 3 seconds
>
>   for (let i = 0; i < maxAttempts; i++) {
>     const response = await fetch(
>       `https://api.apixo.ai/api/v1/statusTask/${model}?taskId=${taskId}`,
>       { headers: { 'Authorization': `Bearer ${apiKey}` } }
>     );
>
>     const { data } = await response.json();
>
>     switch (data.state) {
>       case 'success':
>         return JSON.parse(data.resultJson).resultUrls;
>       case 'failed':
>         throw new Error(`${data.failCode}: ${data.failMsg}`);
>       default:
>         await new Promise(r => setTimeout(r, pollInterval));
>     }
>   }
>
>   throw new Error('Timeout waiting for result');
> }
> ```

> [tab] Python
>
> ```python theme={null}
> import time
> import json
>
> def wait_for_result(model: str, task_id: str, api_key: str) -> list[str]:
>     max_attempts = 60
>     poll_interval = 3  # seconds
>
>     for _ in range(max_attempts):
>         response = requests.get(
>             f'https://api.apixo.ai/api/v1/statusTask/{model}',
>             params={'taskId': task_id},
>             headers={'Authorization': f'Bearer {api_key}'},
>         )
>
>         data = response.json()['data']
>
>         if data['state'] == 'success':
>             return json.loads(data['resultJson'])['resultUrls']
>         elif data['state'] == 'failed':
>             raise Exception(f"{data['failCode']}: {data['failMsg']}")
>
>         time.sleep(poll_interval)
>
>     raise Exception('Timeout waiting for result')
> ```

## 错误响应

| 代码 | 说明 |
| - | - |
| `401` | API 密钥无效或缺失 |
| `404` | 未找到任务 |
| `429` | 超出速率限制 |

详细的错误处理方法见[错误码](https://apixo.ai/docs/zh/api-reference/errors.md)。


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