Documentation/Integration guide/Completion notifications and polling
Integration guide

Completion notifications and polling

Lifecycle states, webhook event notifications, and resilient exponential backoff polling.

4 min read
•Updated October 21, 2026

State Transition Lifecycles

Character creation states are queued, processing, ready, and failed (poll GET /v1/characters/{id}). Image generation states are queued, processing, completed, and failed (poll GET /v1/generations/{id}). Video generation states are queued, processing, completed, and failed (poll GET /v1/video/generations/{id}).

Webhooks vs Direct Polling

Use webhooks for asynchronous completion notifications and GET by resource ID for reconciliation. Polling remains useful after network failures or missed callbacks. A client timeout does not cancel a server job.

Exponential Backoff Best Practices

Always preserve the returned job ID and implement exponential backoff with jitter when polling.

Code Implementation

import os
import time
import requests

BASE = "https://api.apimarketplace.com"
headers = {"Authorization": "Bearer " + os.environ["API_KEY"]}

def poll_generation(generation_id: str, max_wait_seconds: int = 300) -> dict:
    """Robust exponential backoff polling for asynchronous generation jobs."""
    url = f"{BASE}/v1/generations/{generation_id}"
    delay = 2.0
    deadline = time.monotonic() + max_wait_seconds
    
    while time.monotonic() < deadline:
        res = requests.get(url, headers=headers, timeout=30)
        res.raise_for_status()
        data = res.json()
        status = data.get("status")
        
        if status in ("completed", "ready"):
            return data
        if status == "failed":
            error_msg = data.get("errorMessage") or "Generation job failed."
            raise RuntimeError(f"Job {generation_id} failed: {error_msg}")
            
        print(f"Status: {status}... retrying in {delay:.1f}s")
        time.sleep(delay)
        delay = min(delay * 1.5, 10.0)  # Capped at 10s
        
    raise TimeoutError(f"Job {generation_id} timed out after {max_wait_seconds}s")