DEV Community

Xiao Lin
Xiao Lin

Posted on

One API Key, Many Routes: Calling AI Video Generation Models in Python (with Automatic Failover)

If you're building any kind of content pipeline in 2026 — product videos, social clips, demo reels — AI video generation is now the cheapest way to get usable footage. But wiring it into your backend has a few gotchas that most tutorials skip: model routing, idempotency, and what happens when a generation fails halfway.

In this post I'll show the exact Python flow I use, against a multi-route video generation API — one key, dozens of models, and multiple independent routes per model.

Why "multi-route" matters for video generation

Most API providers give you one endpoint per model. AIShow takes a different approach: the same video model can be served by several independent routes (different upstream providers), and every route is addressable by its own model code.

That gives you two superpowers as a backend developer:

  1. Price arbitrage — identical model, different routes, different per-second pricing. Pick the cheapest route that supports your resolution.
  2. Automatic failover — if one route is down or times out, the platform switches to the next route for the same logical model. You don't re-submit or re-pay, and one "stuck" provider never poisons your queue.

In practice you rarely pick routes manually: you submit to a model, and the platform routes it. But every route has its own visible model code, which is great for cost dashboards.

The three-call pattern

All async video generation APIs follow the same shape: submit → poll → fetch.

1. List available models

curl -s https://www.aishow.uno/api/v1/models \
  -H "Authorization: Bearer YOUR_API_KEY"
Enter fullscreen mode Exit fullscreen mode

You get back every model with its kind (video / image / music / chat), price tiers, and supported durations. Video model codes look like aishow-<model>-<resolution>-<id> — one code per route/resolution combo, so you always know exactly what you're paying for.

2. Submit a video task

import requests, uuid

BASE = "https://www.aishow.uno/api/v1"
HDRS = {"Authorization": "Bearer YOUR_API_KEY"}

def submit_video(model_code: str, prompt: str, seconds: int = 5):
    r = requests.post(f"{BASE}/generate", headers=HDRS, json={
        "kind": "video",
        "model": model_code,
        "prompt": prompt,
        "params": {"duration": seconds},
        "idempotency_key": str(uuid.uuid4()),
    }, timeout=30)
    r.raise_for_status()
    return r.json()["task_id"]
Enter fullscreen mode Exit fullscreen mode

The idempotency_key is essential: network retries with the same key never double-charge you.

3. Poll for a terminal state

import time

def wait_video(task_id: str, timeout: int = 900):
    deadline = time.time() + timeout
    while time.time() < deadline:
        t = requests.get(f"{BASE}/task", headers=HDRS,
                         params={"id": task_id}, timeout=15).json()
        if t["status"] == "success":
            return t["result_url"]
        if t["status"] == "failed":
            raise RuntimeError(t.get("error", "generation failed"))
        time.sleep(10)
    raise TimeoutError("video task timed out")
Enter fullscreen mode Exit fullscreen mode

Only treat success / failed as terminal — everything else means "keep waiting".

The draft-first trick that cuts costs 5-10x

Video generation pricing scales with duration and resolution. So don't render 15 seconds of 1080p just to "see if the idea works":

  1. Submit a 5-second draft on a cheap route → check composition & motion
  2. Re-submit the same prompt on an HD route for the final render

Since each route/resolution is its own model code, switching is literally changing one string.

What happens on failure

Generation does fail sometimes (complex prompts, busy routes). Make sure your provider auto-refunds failed tasks — the platform I use credits the points back automatically, so a retry is free to just re-submit. If your provider doesn't do this, build compensation logic into your pipeline.

Wrapping up

  • Async video generation = submit → poll → fetch. Template it once, reuse for image/music by changing kind
  • Multi-route platforms give you per-route pricing and automatic failover without extra code
  • Always use: idempotency keys, terminal-state polling, refund-aware retries
  • Draft-first rendering is the single biggest cost lever

The API I used here: AIShow — Markdown docs at openapi_md.php?raw=1, LLM-friendly if you want your agent to wire it up for you.

Happy to answer questions in the comments.

Top comments (0)