Skip to main content

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

API key, base URL, authentication

Flux generation uses a model name in the URL path:
https://api.cometapi.com/flux/v1/flux-dev
Poll the result endpoint with the task ID:
https://api.cometapi.com/flux/v1/get_result?id=<task_id>
Authenticate with a Bearer token:
Authorization: Bearer $COMETAPI_KEY

Code examples

Use the tabs below for copyable examples in cURL, Python, and Node.js.
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"
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")
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));
}

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

ParameterUse
model path segmentFlux model variant in the URL path, such as the reference example flux-dev.
promptRequired text prompt.
width / heightOutput dimensions in pixels. The API reference notes model-specific ranges.
seedOptional reproducibility control.
output_formatRequested output image format when supported by the selected Flux model.
webhook_urlOptional completion notification URL for supported Flux workflows.

Troubleshooting / FAQ

Keep polling with a bounded retry loop and a timeout. Store the task ID so the job can be checked later.
Download result.sample soon after the task becomes Ready, then store the image in your own storage.
Flux parameter support varies by model variant. Start with prompt, width, and height, then check the Flux API reference before adding optional fields.

Next steps