Skip to main content

Path Parameters

id
string
required
The task ID returned from the initial image generation request.
If the create response included poll_url, prefer calling that exact URL for polling. Some image tasks may surface a poll_url under /v1/tasks/{id} instead of the image-specific status path.You can also poll public image task IDs such as ldtask_... at /v1/images/{id}. This compatibility alias uses the same authorization, ownership checks, billing headers, terminal snapshots, and response shape as /v1/images/generations/{id}.If the task no longer exists or is no longer available in the public task record, TokenLab returns async_task_not_found with the message Task not found or no longer available.

Response

created
integer
Unix timestamp of creation.
task_id
string
The task identifier.
status
string
Task status: pending, processing, completed, or failed.
data
array
Array of generated images (populated when status is completed).Each object contains:
  • url (string): URL of the generated image
  • revised_prompt (string): The prompt used for generation
error
string
Error message (only present when status is failed).
curl "https://api.tokenlab.sh/v1/tasks/ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \
  -H "Authorization: Bearer sk-your-api-key"
import requests

response = requests.get(
    "https://api.tokenlab.sh/v1/tasks/ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
    headers={"Authorization": "Bearer sk-your-api-key"}
)

data = response.json()
print(f"Status: {data['status']}")

if data["status"] == "completed":
    print(f"Image URL: {data['data'][0]['url']}")
const response = await fetch(
  'https://api.tokenlab.sh/v1/tasks/ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
  {
    headers: {
      'Authorization': 'Bearer sk-your-api-key'
    }
  }
);

const data = await response.json();
console.log(`Status: ${data.status}`);

if (data.status === 'completed') {
  console.log(`Image URL: ${data.data[0].url}`);
}
{
  "created": 1706000000,
  "id": "ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  "task_id": "ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  "poll_url": "/v1/tasks/ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  "status": "pending",
  "data": [
    {
      "url": "",
      "revised_prompt": "a beautiful sunset over mountains"
    }
  ]
}
{
  "created": 1706000000,
  "id": "ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  "task_id": "ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  "poll_url": "/v1/tasks/ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  "status": "completed",
  "data": [
    {
      "url": "https://cdn.example.com/generated/image-abc123.png",
      "revised_prompt": "a beautiful sunset over mountains with vibrant orange and purple colors"
    }
  ]
}
{
  "created": 1706000000,
  "id": "ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  "task_id": "ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  "poll_url": "/v1/tasks/ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  "status": "failed",
  "error": "Content policy violation",
  "data": []
}

Polling Best Practices

Recommended polling interval: 3-5 seconds. Most image generation tasks complete within 30-120 seconds depending on the model and routed provider path.
import requests
import time

def poll_image_task(task_id, api_key, max_wait=300, interval=3):
    """Poll for image generation result with timeout."""
    url = f"https://api.tokenlab.sh/v1/tasks/{task_id}"
    headers = {"Authorization": f"Bearer {api_key}"}

    start_time = time.time()
    while time.time() - start_time < max_wait:
        response = requests.get(url, headers=headers)
        data = response.json()

        if data["status"] == "completed":
            return data["data"][0]["url"]
        elif data["status"] == "failed":
            raise Exception(data.get("error", "Generation failed"))

        time.sleep(interval)

    raise TimeoutError(f"Task {task_id} did not complete within {max_wait}s")

# Usage
image_url = poll_image_task("ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "sk-your-api-key")
print(f"Generated image: {image_url}")