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

# Flux API quickstart: Generate images with CometAPI

> Create a Flux image task through CometAPI, poll for the generated image, and download the result before the URL expires.

## What you will build

You will create a Flux image generation task with `POST /flux/v1/flux-dev`, poll `GET /flux/v1/get_result`, and download the generated image URL when the task is ready.

## Prerequisites

* A CometAPI API key stored in `COMETAPI_KEY`
* Python 3.10+ with `requests`, or Node.js 18+
* A Flux model path from the Flux API reference or [Models](/overview/models)

## API key, base URL, authentication

Flux generation uses a model name in the URL path:

```text theme={null}
https://api.cometapi.com/flux/v1/flux-dev
```

Poll the result endpoint with the task ID:

```text theme={null}
https://api.cometapi.com/flux/v1/get_result?id=<task_id>
```

Authenticate with a Bearer token:

```text theme={null}
Authorization: Bearer $COMETAPI_KEY
```

## Code examples

Use the tabs below for copyable examples in cURL, Python, and Node.js.

<CodeGroup>
  ```bash cURL theme={null}
  curl https://api.cometapi.com/flux/v1/flux-dev \
    -H "Authorization: Bearer $COMETAPI_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "prompt": "A paper boat floating on calm water at sunrise.",
      "width": 512,
      "height": 512
    }'

  curl "https://api.cometapi.com/flux/v1/get_result?id=<task_id>" \
    -H "Authorization: Bearer $COMETAPI_KEY"
  ```

  ```python Python theme={null}
  import os
  import time
  from pathlib import Path

  import requests

  api_key = os.environ["COMETAPI_KEY"]
  headers = {"Authorization": f"Bearer {api_key}"}

  create_response = requests.post(
      "https://api.cometapi.com/flux/v1/flux-dev",
      headers={**headers, "Content-Type": "application/json"},
      json={
          "prompt": "A paper boat floating on calm water at sunrise.",
          "width": 512,
          "height": 512,
      },
      timeout=60,
  )
  create_response.raise_for_status()
  task_id = create_response.json()["id"]

  for _ in range(30):
      result_response = requests.get(
          "https://api.cometapi.com/flux/v1/get_result",
          headers=headers,
          params={"id": task_id},
          timeout=30,
      )
      result_response.raise_for_status()
      result = result_response.json()
      if result.get("status") == "Ready":
          image_url = result["result"]["sample"]
          image_bytes = requests.get(image_url, timeout=60).content
          Path("flux-result.jpeg").write_bytes(image_bytes)
          break
      if result.get("status") == "Task not found":
          raise RuntimeError("Flux task was not found")
      time.sleep(2)
  else:
      raise TimeoutError("Flux task did not finish in time")
  ```

  ```javascript Node.js theme={null}
  import fs from "node:fs/promises";

  const headers = {
    Authorization: `Bearer ${process.env.COMETAPI_KEY}`,
    "Content-Type": "application/json",
  };

  const createResponse = await fetch("https://api.cometapi.com/flux/v1/flux-dev", {
    method: "POST",
    headers,
    body: JSON.stringify({
      prompt: "A paper boat floating on calm water at sunrise.",
      width: 512,
      height: 512,
    }),
  });

  if (!createResponse.ok) {
    throw new Error(await createResponse.text());
  }

  const { id } = await createResponse.json();

  for (let attempt = 0; attempt < 30; attempt += 1) {
    const resultResponse = await fetch(
      `https://api.cometapi.com/flux/v1/get_result?id=${encodeURIComponent(id)}`,
      { headers: { Authorization: `Bearer ${process.env.COMETAPI_KEY}` } },
    );

    if (!resultResponse.ok) {
      throw new Error(await resultResponse.text());
    }

    const result = await resultResponse.json();
    if (result.status === "Ready") {
      const imageResponse = await fetch(result.result.sample);
      const imageBuffer = Buffer.from(await imageResponse.arrayBuffer());
      await fs.writeFile("flux-result.jpeg", imageBuffer);
      break;
    }
    if (result.status === "Task not found") {
      throw new Error("Flux task was not found");
    }
    await new Promise((resolve) => setTimeout(resolve, 2000));
  }
  ```
</CodeGroup>

## Flow explanation

Flux generation is asynchronous. The create endpoint returns a task `id`. Poll `/flux/v1/get_result?id=<task_id>` until `status` is `Ready`, then download `result.sample`.

Flux result URLs are time-limited. Copy completed images into your own storage when your application needs durable access.

## Common parameters

| Parameter            | Use                                                                           |
| -------------------- | ----------------------------------------------------------------------------- |
| `model` path segment | Flux model variant in the URL path, such as the reference example `flux-dev`. |
| `prompt`             | Required text prompt.                                                         |
| `width` / `height`   | Output dimensions in pixels. The API reference notes model-specific ranges.   |
| `seed`               | Optional reproducibility control.                                             |
| `output_format`      | Requested output image format when supported by the selected Flux model.      |
| `webhook_url`        | Optional completion notification URL for supported Flux workflows.            |

## Troubleshooting / FAQ

<AccordionGroup>
  <Accordion title="The task stays pending">
    Keep polling with a bounded retry loop and a timeout. Store the task ID so the job can be checked later.
  </Accordion>

  <Accordion title="The image URL expired">
    Download `result.sample` soon after the task becomes `Ready`, then store the image in your own storage.
  </Accordion>

  <Accordion title="A Flux parameter fails">
    Flux parameter support varies by model variant. Start with `prompt`, `width`, and `height`, then check the Flux API reference before adding optional fields.
  </Accordion>
</AccordionGroup>

## Next steps

* Read the [Flux generate image API reference](/api/image/flux/flux-generate-image).
* Poll results with [Get Flux image result](/api/image/flux/flux-query).
* Find Flux models in [Models](/overview/models).
* Estimate cost with [Estimate request cost before calling a model](/guides/how-to-estimate-cost-before-calling-a-model).
