No Public URL? Poll for Results

After you create a task, there are two ways to get the result:

  1. Callbacks (webhook): when the task finishes, the system notifies your server; see Callbacks (Webhook).
  2. Polling: you query the task status yourself at regular intervals.

If you can receive webhooks, use webhooks. Callbacks don't count against your request limits at all, you're notified as soon as the task finishes, and you don't need to work out a polling interval.

Use the polling approach below only if your program runs locally, on an internal network, or in another environment that can't be reached from outside and therefore can't receive callbacks.

Why you hit 429

Each key has a limit on the number of requests per minute and per day (see Check Usage Limits), and querying task status counts toward it.

A common approach is to run a loop for each task that queries its own GET /api/tasks/{publicId} every few seconds. With 5 tasks at once, each polling once per second, that's 300 requests per minute. A trial key's limit of 20 requests per minute is used up within seconds, after which both creating tasks and queries return:

{ "message": "Too Many Attempts." }
  1. Query everything at once instead of each task separately. Use GET /api/tasks?per_page=N to get the N most recent tasks in one request (N up to 100), then match public_id in your program to find the tasks you need. No matter how many tasks are running, each poll counts as only one request.
  2. After creating a video, wait 30 seconds before the first query, then query every 10 seconds. Videos usually take a few minutes to complete, so querying too early just wastes requests. Once every 10 seconds is 6 requests per minute, which leaves room under a trial key's 20-per-minute limit for creating tasks.
  3. Check X-RateLimit-Remaining on every response. This is the number of requests you can still send; when it reaches 0, stop and don't send more.
  4. When you get 429, wait the number of seconds in Retry-After before retrying; don't resend immediately. An immediate resend will be blocked too and keeps using up your requests.

Finished tasks stay in the list for 7 days, so tasks that just completed or failed will always show up. You can stop querying tasks whose status is 20 (completed), 30 (failed), or 40 (canceled). For the meaning of each status, see Query Tasks and Download Results.

Example

The following Python example tracks multiple tasks with a single list query:

import time
import requests

API = "https://yuyuvideo.com/api"
HEADERS = {"X-Api-Key": "sk-live-xxxx"}
FINISHED = {20, 30, 40}

def wait_for(public_ids):
    pending = set(public_ids)
    results = {}
    time.sleep(30)  # wait 30 seconds after creating the video
    while pending:
        r = requests.get(f"{API}/tasks", params={"per_page": 100}, headers=HEADERS)
        if r.status_code == 429:
            time.sleep(int(r.headers.get("Retry-After", 10)))
            continue
        r.raise_for_status()
        for task in r.json()["data"]:
            if task["public_id"] in pending and task["status"] in FINISHED:
                results[task["public_id"]] = task
                pending.discard(task["public_id"])
        if pending and int(r.headers.get("X-RateLimit-Remaining", 1)) == 0:
            time.sleep(60)  # this minute's requests are used up
        elif pending:
            time.sleep(10)  # then query every 10 seconds
    return results

If more than 100 tasks are in progress at once and they don't fit on one page, pass the page parameter to get the next page, or create tasks in batches.

Response headers

For full details on the X-RateLimit-Remaining, Retry-After, and other headers used above, see "Response headers" in Authentication.