> ## Documentation Index
> Fetch the complete documentation index at: https://docs.crun.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# 获取任务信息

> 查询所有模型生成任务的状态与结果。

## API 端点

```
GET https://api.crun.ai/api/v1/client/job/TaskInfo
```

<Info>
  该端点用于查询通过 `/api/v1/client/job/CreateTask` 接口创建的任务执行状态，并获取任务结果。
  它提供统一的查询接口，兼容 *模型* 分类下的**所有模型**，无论底层模型如何，均可一致地进行任务追踪与结果获取。
</Info>

## 查询参数

<ParamField query="task_id" type="string" required>
  创建任务时返回的唯一任务标识符。

  **示例**：`task_12345678`
</ParamField>

## 请求示例

<CodeGroup>
  ```bash cURL theme={null} theme={null}
  curl -X GET "https://api.crun.ai/api/v1/client/job/TaskInfo?task_id=task_12345678" \
  -H "X-API-KEY: YOUR_API_KEY" \
  -H "Content-Type: application/json"

  ```

  ```python Python theme={null} theme={null}
  import requests

  API_KEY = "YOUR_API_KEY"

  headers = {
    "X-API-KEY": API_KEY,
    "Content-Type": "application/json"
  }
  params = {
    "task_id": "task_12345678",
  }

  GET_TASK_STATUS_URL = "https://api.crun.ai/api/v1/client/job/TaskInfo"

  response = requests.get(GET_TASK_STATUS_URL, params=params, headers=headers)
  print(response.json())
  ```

  ```javascript Node.js theme={null} theme={null}
  const API_KEY = "YOUR_API_KEY";
  const taskId = "task_12345678";

  const url = new URL("https://api.crun.ai/api/v1/client/job/TaskInfo");
  url.searchParams.append("task_id", taskId);

  fetch(url.toString(), {
    method: "GET",
    headers: {
        "X-API-KEY": API_KEY,
        "Content-Type": "application/json",
    },
  })
    .then(res => res.json())
    .then(data => {
        console.log(data);
    })
    .catch(err => {
        console.error("请求失败：", err);
    });
  ```
</CodeGroup>

<ResponseExample title="响应示例">
  ```json 任务运行中 theme={null}
  {
    "code": 200,
    "message": "success",
    "data": {
      "task_id": "d6956973-2f17-43d4-8514-af6cc3a2f55d",
      "provider": "Google",
      "model_version": "nano-banana",
      "status": "running",
      "param": {
        "model": "google/nano-banana",
        "callback_url": null,
        "input": {
          "prompt": "A scene of urban fantasy art. A dynamic graffiti art character. A teenager, painted with ...",
          "output_format": "png",
          "aspect_ratio": "16:9"
        }
      },
      "create_at": 1768900378,
      "result": null,
      "credits": 3,
      "duration_s": null,
      "complete_at": null,
      "source": "api"
    }
  }
  ```

  ```json 任务成功 theme={null}
  {
    "code": 200,
    "message": "success",
    "data": {
      "task_id": "d6956973-2f17-43d4-8514-af6cc3a2f55d",
      "provider": "Google",
      "model_version": "nano-banana",
      "status": "success",
      "param": {
        "model": "google/nano-banana",
        "callback_url": null,
        "input": {
          "prompt": "A scene of urban fantasy art. A dynamic graffiti art character. A teenager, painted with ...",
          "output_format": "png",
          "aspect_ratio": "16:9"
        }
      },
      "create_at": 1768900378,
      "result": {
        "code": 200,
        "message": "generation success",
        "media_urls": [
          "https://example.com/example.png"
        ]
      },
      "credits": 3,
      "duration_s": 137,
      "complete_at": 1768900515,
      "source": "api"
    }
  }
  ```

  ```json 任务失败 theme={null}
  {
    "code": 200,
    "message": "success",
    "data": {
      "task_id": "d6956973-2f17-43d4-8514-af6cc3a2f55d",
      "provider": "Google",
      "model_version": "nano-banana",
      "status": "failed",
      "param": {
        "model": "google/nano-banana",
        "callback_url": null,
        "input": {
          "prompt": "A scene of urban fantasy art. A dynamic graffiti art character. A teenager, painted with ...",
          "output_format": "png",
          "aspect_ratio": "16:9"
        }
      },
      "create_at": 1768900378,
      "result": {
        "code": 501,
        "message": "generation failed"
      },
      "credits": 0,
      "duration_s": 137,
      "complete_at": 1768900515,
      "source": "api"
    }
  }
  ```
</ResponseExample>

## 响应格式

<ResponseField name="code" type="integer">
  响应状态码。200 表示请求已成功处理（任务已找到并返回，与执行结果无关）。
</ResponseField>

<ResponseField name="message" type="string">
  响应消息，通常为 `"success"`。
</ResponseField>

<ResponseField name="data" type="object">
  包含所有任务信息的任务数据对象。

  <Expandable title="data 属性">
    <ResponseField name="task_id" type="string">
      任务的唯一标识符。
    </ResponseField>

    <ResponseField name="provider" type="string">
      模型提供商名称。

      **示例**：`Google`、`Wan`、`Bytedance`
    </ResponseField>

    <ResponseField name="model_version" type="string">
      创建任务时使用的具体模型版本。
    </ResponseField>

    <ResponseField name="status" type="string">
      任务当前状态。详见下方[任务状态枚举](#任务状态枚举)。

      **可能的值**：`pending`、`running`、`success`、`failed`
    </ResponseField>

    <ResponseField name="param" type="object">
      创建任务时使用的原始参数。

      <Expandable title="param 属性">
        <ResponseField name="model" type="string">
          创建任务时使用的模型标识符。
        </ResponseField>

        <ResponseField name="callback_url" type="string | null">
          为该任务配置的 Webhook 回调 URL。
        </ResponseField>

        <ResponseField name="input" type="object">
          模型特定的输入参数，其结构因所选模型而异。
        </ResponseField>
      </Expandable>
    </ResponseField>

    <ResponseField name="result" type="object | null">
      任务生成结果。
      仅当任务状态为 `success` 或 `failed` 时返回有效值；否则返回 `null`。

      <Expandable title="result 属性">
        <ResponseField name="code" type="integer">
          任务生成结果状态码。`200` 表示生成成功，非 200 值表示生成失败。
        </ResponseField>

        <ResponseField name="message" type="string">
          任务成功或失败的说明信息。
        </ResponseField>

        <ResponseField name="media_urls" type="array<string>">
          任务生成的媒体 URL 列表。
          仅当任务状态为 `success` 时返回。
        </ResponseField>

        <ResponseField name="usage" type="object">
          可选的 token 用量信息。可能包含 `prompt_tokens`、`completion_tokens`、`total_tokens` 等字段。
        </ResponseField>
      </Expandable>
    </ResponseField>

    <ResponseField name="credits" type="number">
      任务消耗的积分，任务状态是`pending` 或 `running` 时表示预扣积分，任务状态是 `success` 或 `failed` 时表示实际消耗积分。
    </ResponseField>

    <ResponseField name="create_at" type="integer">
      任务创建时间的 Unix 时间戳（秒）。
    </ResponseField>

    <ResponseField name="complete_at" type="integer | null">
      任务完成时间的 Unix 时间戳（毫秒）。
      任务未完成时为 `null`。
    </ResponseField>

    <ResponseField name="duration_s" type="integer | null">
      任务执行时长（秒）。任务未完成时为 null。
    </ResponseField>

    <ResponseField name="source" type="string">
      任务创建来源。
    </ResponseField>
  </Expandable>
</ResponseField>

## 任务状态枚举

| 状态        | 描述         | 操作建议                      |
| --------- | ---------- | ------------------------- |
| `pending` | 任务已入队，等待处理 | 继续轮询                      |
| `running` | 任务正在处理中    | 继续轮询                      |
| `success` | 任务成功完成     | 访问 result.media\_url 获取结果 |
| `failed`  | 任务失败       | 访问 result 对象查看错误码和错误信息    |

## 获取任务结果的最佳实践

<AccordionGroup>
  <Accordion title="推荐轮询间隔">
    * **轮询间隔**：建议轮询间隔设置为 15 至 30 秒
    * **动态调整间隔**：对于长时间运行的视频生成任务，建议动态增加轮询间隔
    * **延迟首次轮询**：首次状态检查前至少等待一个轮询间隔，以减少不必要的请求
    * **最大轮询时长**：超过 15-20 分钟后应停止并进行排查

    <Warning>
      过于频繁的轮询可能导致请求被限速。生产环境建议使用回调方式。
    </Warning>
  </Accordion>

  <Accordion title="使用回调代替轮询">
    对于生产环境应用，我们强烈建议在创建任务时使用 `callback_url` 参数：

    * **无需轮询**：服务器自动接收通知
    * **降低 API 成本**：避免持续的轮询请求
    * **性能更优**：任务完成后立即收到通知
    * **减少延迟**：完成与通知之间无延迟

    <Tip>
      推荐使用此方式
    </Tip>
  </Accordion>

  <Accordion title="处理已完成的任务">
    当 `status` 为 `success` 时：

    1. 从响应的 `result` 字段获取任务结果
    2. 从 `result.media_urls` 中提取生成的媒体 URL
    3. 将媒体文件下载至您自己的存储空间
    4. 将结果元数据持久化至您的存储或数据库

    <Warning>
      **重要提示**：生成的媒体 URL 通常在 14 天后永久删除
    </Warning>
  </Accordion>
</AccordionGroup>

## 常见错误码

| 错误码   | 描述                | 解决方案                     |
| ----- | ----------------- | ------------------------ |
| `401` | 未授权 - API 密钥无效或缺失 | 检查您的 API 密钥              |
| `404` | 任务未找到             | 验证 task\_id 是否正确         |
| `429` | 超出请求频率限制          | 降低请求频率                   |
| `500` | 服务器内部错误           | 几分钟后重试                   |
| `501` | 生成失败              | 查看 `result` 对象获取错误码和错误信息 |

## 频率限制

* **最大查询速率**：每账号每秒最多 15 次请求
* **推荐间隔**：每次轮询间隔 15-30 秒

## 相关资源

<CardGroup cols={2}>
  <Card title="模型概览" icon="store" href="/zh/models/quickstart">
    探索所有可用模型
  </Card>

  <Card title="获取账户额度" icon="coins" href="/zh/common-api/get-account-credits">
    查看您的剩余积分
  </Card>
</CardGroup>


## OpenAPI

````yaml zh/models/common/get-task-info.json get /api/v1/client/job/TaskInfo
openapi: 3.0.0
info:
  title: 获取任务状态
  description: 查询所有模型生成任务的状态与结果。
  version: 1.0.0
  contact:
    name: 技术支持
    email: support@crun.ai
servers:
  - url: https://api.crun.ai
    description: API 服务器
security:
  - ApiKeyAuth: []
paths:
  /api/v1/client/job/TaskInfo:
    get:
      summary: 查询所有模型生成任务的状态与结果。
      operationId: get-task-info
      parameters:
        - name: task_id
          in: query
          required: true
          description: 用于获取任务结果的任务 ID。
          schema:
            type: string
            minLength: 36
            maxLength: 36
            example: task_1234567
      responses:
        '200':
          description: 任务信息获取成功或任务未找到。
          content:
            application/json:
              schema:
                allOf:
                  - $ref: '#/components/schemas/ApiResponse'
                  - type: object
                    properties:
                      data:
                        $ref: '#/components/schemas/TaskResponseModel'
        '401':
          description: API Key 缺失
          content:
            application/json:
              schema:
                allOf:
                  - $ref: '#/components/schemas/ApiResponse'
              example:
                code: 401
                message: API key missing
                data: null
        '403':
          description: API Key 已禁用
          content:
            application/json:
              schema:
                allOf:
                  - $ref: '#/components/schemas/ApiResponse'
              example:
                code: 403
                message: API key disabled
                data: null
        '404':
          description: 任务未找到
          content:
            application/json:
              schema:
                allOf:
                  - $ref: '#/components/schemas/ApiResponse'
              example:
                code: 404
                message: Task not found
                data: null
        '500':
          $ref: '#/components/responses/Error'
components:
  schemas:
    ApiResponse:
      type: object
      properties:
        code:
          type: integer
          description: |-
            响应状态码

            - **200**: 成功 - 请求已成功处理
            - **401**: 未授权 - 身份凭证缺失或无效
            - **402**: 额度不足 - 账户余额不足以完成本次操作
            - **404**: 未找到 - 请求的资源或端点不存在
            - **422**: 验证错误 - 请求参数未通过验证
            - **429**: 请求限速 - 该资源的请求次数已超出限制
            - **455**: 服务不可用 - 系统正在维护中
            - **500**: 服务器错误 - 处理请求时发生意外错误
            - **501**: 生成失败 - 内容生成任务失败
            - **505**: 功能已禁用 - 所请求的功能当前已被禁用
        message:
          type: string
          description: 响应消息
        data:
          nullable: true
      required:
        - code
        - message
    TaskResponseModel:
      type: object
      description: 任务记录及结果信息。
      properties:
        task_id:
          type: string
          description: 任务 ID
          example: task_1234567
        provider:
          type: string
          description: 模型提供商
          example: bytedance
        model_version:
          type: string
          description: 模型版本
          example: seedream-4.5
        status:
          type: string
          description: 任务状态
          enum:
            - pending
            - running
            - success
            - failed
          example: success
        param:
          type: object
          description: 创建任务时使用的原始参数。
        create_at:
          type: integer
          description: 任务创建时间戳（秒）
          example: 1715750400
        result:
          type: object
          nullable: true
          description: 任务结果
          properties:
            code:
              type: integer
              description: 任务生成结果状态码。
            message:
              type: string
              description: 任务成功或失败的说明信息。
            media_urls:
              type: array
              description: 任务成功时返回的生成媒体 URL 列表。
              items:
                type: string
            usage:
              type: object
              description: >-
                可选的 token 用量信息。该字段为字典（Dict[str, Any]），可包含
                prompt_tokens、completion_tokens、total_tokens 等字段。
              additionalProperties: true
        credits:
          type: number
          description: >-
            任务消耗的积分，任务状态是`pending` 或 `running` 时表示预扣积分，任务状态是 `success` 或
            `failed` 时表示实际消耗积分。
          example: 3
        duration_s:
          type: integer
          nullable: true
          description: 任务执行时长（秒）
          example: 12
        complete_at:
          type: integer
          nullable: true
          description: 任务完成时间戳（秒）
          example: 1715750412
        source:
          type: string
          description: 任务来源
          enum:
            - api
            - playground
          example: api
      required:
        - task_id
        - provider
        - model_version
        - status
        - param
        - create_at
        - source
  responses:
    Error:
      description: 服务器错误
  securitySchemes:
    ApiKeyAuth:
      type: apiKey
      in: header
      name: x-api-key
      description: |-
        所有 API 均需通过 API Key 进行身份验证。

        获取 API Key：
        1. 访问 [API Key 管理页面](https://crun.ai/zh/user-api-key) 获取您的 API Key

        使用方式：
        将以下内容添加至请求头：

        x-api-key: YOUR_API_KEY

        注意事项：
        - 请妥善保管您的 API Key，切勿与他人共享
        - 若怀疑 API Key 已泄露，请立即在管理页面进行重置

````