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

# Hent et FLUX-bilderesultat

> Poll en innebygd FLUX-bildeoppgave etter dens toppnivå-ID, og hent URL-adressen til det genererte bildet når statusen er Ready.

## Oversikt

Send den overordnede oppgaven `id` som returneres av `POST /flux/v1/\{model\}`, som spørringsparameteren `id`. Fortsett å polle innenfor en avgrenset klienttidsavbruddsperiode til oppgaven lykkes eller mislykkes.

| Status                             | Klienthandling                                                              |
| ---------------------------------- | --------------------------------------------------------------------------- |
| `Ready`                            | Les `result.sample`, og last deretter ned eller overfør bildet umiddelbart. |
| `Error`, `Failed`, eller `Failure` | Stopp og vis oppgavefeilen.                                                 |
| `Task not found`                   | Stopp; ID-en er ukjent eller ikke lenger tilgjengelig.                      |
| `Request Moderated`                | Stopp; forespørselen ble avvist av moderering.                              |
| `Content Moderated`                | Stopp; det genererte innholdet ble avvist av moderering.                    |
| Enhver annen status                | Fortsett å polle til klienttidsavbruddet er nådd.                           |

Det vellykkede resultatet kan inneholde `sample`, `seed`, `prompt` og `start_time`. Noen felt kan utelates, så krev bare `result.sample` etter at oppgaven er `Ready`.

<Warning>
  `result.sample` er midlertidig. Ikke bruk en resultat-URL fra en leverandør som en varig applikasjonsressurs.
</Warning>


## OpenAPI

````yaml api/openapi/image/flux/get-flux-query.openapi.json GET /flux/v1/get_result
openapi: 3.1.0
info:
  title: FLUX image result API
  version: 1.0.0
servers:
  - url: https://api.cometapi.com
security:
  - bearerAuth: []
paths:
  /flux/v1/get_result:
    get:
      summary: Get a FLUX image result
      description: >-
        Poll a native FLUX task with the top-level `id` returned by `POST
        /flux/v1/{model}`. `Ready` is the successful terminal state. Stop on
        `Error`, `Failed`, `Failure`, `Task not found`, `Request Moderated`, or
        `Content Moderated`; continue polling other states only within a bounded
        client timeout.
      operationId: flux_query
      parameters:
        - name: id
          in: query
          required: true
          description: Top-level task ID returned by the FLUX create endpoint.
          schema:
            type: string
          example: <task_id>
      responses:
        '200':
          description: Current FLUX task state and, when ready, the generated image result.
          content:
            application/json:
              schema:
                type: object
                required:
                  - id
                  - status
                properties:
                  id:
                    type: string
                    description: FLUX task ID.
                  status:
                    type: string
                    description: >-
                      Task state. `Ready` is successful. Known failure states
                      include `Error`, `Failed`, `Failure`, `Task not found`,
                      `Request Moderated`, and `Content Moderated`.
                  result:
                    type:
                      - object
                      - 'null'
                    description: >-
                      Generation result. It can be null or omitted while the
                      task is still running.
                    properties:
                      seed:
                        type: integer
                        description: >-
                          Seed reported for the completed generation when
                          available.
                      prompt:
                        type: string
                        description: >-
                          Prompt reported for the completed generation when
                          available.
                      sample:
                        type: string
                        description: >-
                          Temporary URL of the generated image. Download or
                          transfer it promptly.
                      start_time:
                        type: number
                        description: Provider task start time when returned.
                  progress:
                    type:
                      - number
                      - 'null'
                    description: Task progress when returned.
                  details:
                    description: Additional task details when returned.
                  preview:
                    description: Task preview data when returned.
              examples:
                Pending:
                  summary: Task still running
                  value:
                    id: <task_id>
                    status: Pending
                    result: null
                Ready:
                  summary: Task completed
                  value:
                    id: <task_id>
                    status: Ready
                    result:
                      seed: 424242
                      sample: https://cdn.example.com/generated/flux-image.png
      x-codeSamples:
        - lang: Shell
          label: Poll until ready
          source: |
            curl "https://api.cometapi.com/flux/v1/get_result?id=<task_id>" \
              -H "Authorization: Bearer $COMETAPI_KEY"
        - lang: Python
          label: Poll until ready
          source: |
            import os
            import time
            import requests

            task_id = "<task_id>"
            headers = {"Authorization": "Bearer " + os.environ["COMETAPI_KEY"]}
            failure_statuses = {
                "Error",
                "Failed",
                "Failure",
                "Task not found",
                "Request Moderated",
                "Content Moderated",
                "failed",
                "failure",
            }

            for _ in range(60):
                response = requests.get(
                    "https://api.cometapi.com/flux/v1/get_result",
                    params={"id": task_id},
                    headers=headers,
                    timeout=30,
                )
                response.raise_for_status()
                task = response.json()
                status = task.get("status")
                print(status)

                if status == "Ready":
                    print(task["result"]["sample"])
                    break
                if status in failure_statuses:
                    raise RuntimeError(f"FLUX task failed: {status}")

                time.sleep(5)
            else:
                raise TimeoutError("FLUX task did not finish in time")
        - lang: JavaScript
          label: Poll until ready
          source: >
            const taskId = "<task_id>";

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

            const failureStatuses = new Set([
              "Error",
              "Failed",
              "Failure",
              "Task not found",
              "Request Moderated",
              "Content Moderated",
              "failed",
              "failure",
            ]);


            for (let attempt = 0; attempt < 60; attempt += 1) {
              const response = await fetch(
                `https://api.cometapi.com/flux/v1/get_result?id=${encodeURIComponent(taskId)}`,
                { headers, signal: AbortSignal.timeout(30_000) },
              );
              if (!response.ok) throw new Error(await response.text());

              const task = await response.json();
              console.log(task.status);

              if (task.status === "Ready") {
                console.log(task.result.sample);
                break;
              }
              if (failureStatuses.has(task.status)) {
                throw new Error(`FLUX task failed: ${task.status}`);
              }
              if (attempt === 59) throw new Error("FLUX task did not finish in time");

              await new Promise((resolve) => setTimeout(resolve, 5000));
            }
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: Bearer token authentication. Use your CometAPI key.

````