Common capabilitiesAsynchronous jobs

Asynchronous jobs

Audio, image and video generation endpoints process requests as asynchronous jobs. They return 202 with the job right away, and generation continues in the background.

Job states

StateMeaningWhat to do
queuedThe job is created and credits are reserved; it is waiting to be processedKeep checking
processingGeneration is in progressKeep checking
succeededGeneration succeeded and actual usage is settledFetch the result
failedGeneration failed and reserved credits were refunded automaticallyRead error_code and error_message
cancelledThe job was stoppedWhether credits are refunded is given in the stop job response

progress may be null; this does not mean the job has stalled. Use status to track the job.

Know when a job finishes

There are two ways:

  • Polling: call Retrieve a job until the job reaches succeeded, failed or cancelled. Start with an interval of a few seconds and lengthen it over time.
  • Webhooks: pass webhook_endpoint_id when you submit a generation request, and a signed notification is sent to your endpoint when the job ends. See Webhooks.

Webhooks do not guarantee ordering across jobs. Even with webhooks, keep polling as a fallback in case your endpoint is temporarily unavailable.

Fetch results

After a job succeeds, call Retrieve a job result to get the download URL result_url. It is a short-lived signed URL whose lifetime in seconds is given by expires_in; call the endpoint again for a new URL after it expires. retained_until is when the result file stops being retained.

List jobs returns your account's job history and can filter by type, status and idempotency_key. Each page returns up to limit jobs (1–100, default 20). When more results exist, the X-Next-Cursor response header carries the cursor for the next page; pass it as the cursor parameter.

Retry safely with idempotency keys

Send an Idempotency-Key header with generation requests. Its value is a unique string of up to 128 characters that identifies the business operation, such as an order number in your system.

  • A retry with the same key and the same request parameters returns the job created by the first request, without creating another job or charging again.
  • Using the same key with different request parameters returns 409 IDEMPOTENCY_CONFLICT. Use a new key for every new business operation.
  • Create an audio composition and Resolve an image submission require this header. Other generation endpoints accept requests without it, but such requests cannot be deduplicated, so a retry after a timeout may create another job and reserve credits again.

After a network timeout

A client timeout does not mean the job was not created. Handle it in this order:

  1. If you already have the job ID, retrieve that job.
  2. If you do not, retry with the same request parameters and the same idempotency key, or look the job up in the job list with the idempotency_key parameter.
  3. Submit with a new idempotency key only when this really is a new business operation.

Webhooks are delivered at least once, so the same event may arrive more than once. See Webhooks for deduplication.

On this page