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

# Lấy một tác vụ tạo ảnh

> Sử dụng CometAPI GET /v1/images/generations/{task_id} để thăm dò một tác vụ tạo ảnh bất đồng bộ tương thích OpenAI và lấy dữ liệu ảnh cuối cùng.

Sử dụng endpoint này sau khi bạn tạo một ảnh bằng `POST /v1/images/generations` và `async: true`. Yêu cầu tạo sẽ trả về `data.task_id`, và endpoint này trả về trạng thái tác vụ cùng với dữ liệu ảnh cuối cùng khi tác vụ thành công.

## Thăm dò một tác vụ ảnh

<Steps>
  <Step title="Tạo tác vụ">
    Gửi `POST /v1/images/generations` với `async: true`, sau đó lưu `data.task_id`.
  </Step>

  <Step title="Thăm dò tác vụ">
    Gọi endpoint này với task ID đã lưu cho đến khi `data.status` là `success` hoặc `failure`.
  </Step>

  <Step title="Đọc dữ liệu ảnh">
    Khi `data.status` là `success`, đọc mục đầu tiên trong `data.data`. Tùy thuộc vào model đã chọn, mục này có thể bao gồm `b64_json`, `url`, hoặc `revised_prompt`.
  </Step>
</Steps>

## Giá trị trạng thái

* `pending`: Tác vụ đang trong hàng đợi hoặc đang tạo.
* `success`: Tác vụ đã hoàn tất và `data.data` chứa dữ liệu ảnh đã tạo.
* `failure`: Tác vụ thất bại. Kiểm tra `data.fail_reason` khi giá trị này được trả về.


## OpenAPI

````yaml api/openapi/image/openai/get-image-generation-task.openapi.json GET /v1/images/generations/{task_id}
openapi: 3.1.0
info:
  title: Image generation task API
  version: 1.0.0
  description: >-
    Retrieve an async image generation task created by `POST
    /v1/images/generations` with `async: true`.
servers:
  - url: https://api.cometapi.com
security:
  - bearerAuth: []
paths:
  /v1/images/generations/{task_id}:
    get:
      summary: Retrieve an image generation task
      description: >-
        Poll an async image generation task by task ID. Use the `task_id`
        returned by an async `POST /v1/images/generations` request.
      operationId: retrieve_image_generation_task
      parameters:
        - name: task_id
          in: path
          required: true
          description: Task ID returned in `data.task_id` by the async create request.
          schema:
            type: string
          example: <task_id>
      responses:
        '200':
          description: Current image generation task state.
          content:
            application/json:
              schema:
                type: object
                required:
                  - code
                  - data
                properties:
                  code:
                    type: string
                    description: >-
                      Request status code. Successful task lookups return
                      `success`.
                    example: success
                  message:
                    type: string
                    description: Optional status message.
                  data:
                    type: object
                    required:
                      - task_id
                      - status
                      - data
                    properties:
                      task_id:
                        type: string
                        description: Task ID for this image generation job.
                      status:
                        type: string
                        description: >-
                          Task state. Poll while the value is `pending`; read
                          `data[]` when the value is `success`.
                        enum:
                          - pending
                          - success
                          - failure
                      data:
                        type: array
                        description: >-
                          Generated image data. The array is empty until the
                          task succeeds.
                        items:
                          type: object
                          properties:
                            url:
                              type: string
                              description: >-
                                Temporary image URL when the selected model
                                supports URL output.
                            b64_json:
                              type: string
                              description: >-
                                Base64-encoded image payload for models that
                                return inline content.
                            revised_prompt:
                              type: string
                              description: Provider-rewritten prompt, when available.
                      fail_reason:
                        type: string
                        description: Failure reason when the task status is `failure`.
                example:
                  code: success
                  data:
                    task_id: <task_id>
                    status: success
                    data:
                      - b64_json: <base64-image-data>
              examples:
                pending:
                  summary: Pending task
                  value:
                    code: success
                    data:
                      task_id: <task_id>
                      status: pending
                      data: []
                success:
                  summary: Finished task
                  value:
                    code: success
                    data:
                      task_id: <task_id>
                      status: success
                      data:
                        - b64_json: <base64-image-data>
      x-codeSamples:
        - lang: Shell
          label: Poll until done
          source: |
            TASK_ID="<task_id>"

            while true; do
              RESPONSE=$(curl -s "https://api.cometapi.com/v1/images/generations/$TASK_ID" \
                -H "Authorization: Bearer $COMETAPI_KEY")
              STATUS=$(printf "%s" "$RESPONSE" | python3 -c "import json,sys; print(json.load(sys.stdin)['data']['status'])")
              echo "status: $STATUS"
              case "$STATUS" in success | failure) break;; esac
              sleep 3
            done

            printf "%s\n" "$RESPONSE"
        - lang: Python
          label: Poll until done
          source: |
            import os
            import time
            import requests

            task_id = "<task_id>"
            headers = {"Authorization": "Bearer " + os.environ["COMETAPI_KEY"]}

            while True:
                task = requests.get(
                    f"https://api.cometapi.com/v1/images/generations/{task_id}",
                    headers=headers,
                ).json()
                status = task["data"]["status"]
                print(status)
                if status in ("success", "failure"):
                    break
                time.sleep(3)

            if status == "success":
                print(task["data"]["data"][0].keys())
        - lang: JavaScript
          label: Poll until done
          source: >
            const taskId = "<task_id>";

            const headers = { Authorization: `Bearer
            ${process.env.COMETAPI_KEY}` };


            let task;

            while (true) {
              const response = await fetch(
                `https://api.cometapi.com/v1/images/generations/${taskId}`,
                { headers },
              );
              task = await response.json();
              const status = task.data.status;
              console.log(status);
              if (status === "success" || status === "failure") break;
              await new Promise((resolve) => setTimeout(resolve, 3000));
            }


            if (task.data.status === "success") {
              console.log(Object.keys(task.data.data[0]));
            }
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: Bearer token authentication. Use your CometAPI key.

````