Async Video Generation

Handle long-running video generation with async polling.

Video generation can take several minutes. Synchronous requests risk 502 timeouts from proxies, CDNs, or serverless function limits. Async mode solves this.

The Problem

When using a server proxy (Cloudflare, Vercel, etc.), requests that take longer than the proxy timeout (typically 30-60 seconds) will be killed with a 502 error. Video generation routinely exceeds this.

The Solution: Async Mode

from visgate_sdk import AsyncClient
import asyncio
 
async def generate_video():
    async with AsyncClient() as client:
        result = await client.videos.generate(
            model="fal-ai/veo3",
            prompt="a bird flying over mountains",
            duration_seconds=6.0,
        )
 
        # Check if the video is ready
        if result.video_url:
            print(f"Video ready: {result.video_url}")
        else:
            print("Video is still processing...")
 
asyncio.run(generate_video())

Request Status Properties

PropertyTypeDescription
requestIdstringUnique request identifier
statusstring"pending" | "processing" | "completed" | "failed"
mediaTypestring"image" or "video"
providerstringProvider name
modelstringModel identifier
outputUrlstring | nullOutput URL when completed
errorMessagestring | nullError message if failed
createdAtstringISO timestamp of creation
completedAtstring | nullISO timestamp of completion

Webhooks (Alternative)

Instead of polling, you can provide a webhook URL to get notified when the job completes:

const result = await client.videos.generate("fal-ai/veo3", "ocean waves", {
  preferAsync: true,
  webhookUrl: "https://your-app.com/api/webhook",
  callbackId: "my-custom-id",
});

The webhook will receive a POST request with the completed result and your callbackId.

Proxy Configuration

Make sure your server proxy route allows enough time:

app/api/visgate/[...path]/route.ts
export const maxDuration = 300; // 5 minutes
export { GET, POST, PUT, DELETE, OPTIONS } from "@visgate_ai/server-proxy/next";

On this page