Phase 3: Video Generation · 55 min · Python · fal.ai API · Replicate API
Video Generation API Integration
Video generation is async — you submit a job, wait 30 seconds to 5 minutes, and retrieve the result. Master the polling and webhook patterns.
Hiring signal: Async API integration (polling, webhooks, job management) is a core engineering skill for video generation — interviewers test whether you can handle long-running jobs correctly.
What you will learn
- Implement REST API polling: POST generation request → poll for status → retrieve video URL
- Implement webhook pattern: POST generation request → receive callback when complete
- Handle async job management for long-running generation jobs (30s-5min)
- Implement error handling: rate limits (429), generation failures, content policy rejections
The Problem
A team integrates a video generation API. They send a POST request and wait for the response. After 60 seconds, the HTTP connection times out. They retry. Same timeout. They don't realize video generation is inherently async — the API returns a job ID immediately, and you poll for status or receive a webhook callback when the video is ready. Their synchronous code can't handle a 2-minute generation time.
Async API integration is a core engineering skill for video generation. Interviewers test whether you can handle long-running jobs correctly — polling, webhooks, error handling, and job management.
What you'll build
Implement both REST API polling and webhook patterns for video generation. Handle rate limits (429), generation failures, and content policy rejections. Build a job manager that tracks multiple concurrent video generation jobs.
The Async Pattern
Video generation APIs follow a standard async pattern:
1. POST /generate → Returns job_id immediately (not the video)
2. GET /status/{job_id} → Poll for status (queued → processing → completed/failed)
3. GET /result/{job_id} → Retrieve the video URL when completed
OR
3. POST /webhook → Receive callback when job completes
fal.ai Queue API
fal.ai uses a queue-based async pattern:
import requests
FAL_KEY = "your-key"
FAL_API = "https://queue.fal.run/fal-ai"
# Step 1: Submit job
submit_resp = requests.put(
f"{FAL_API}/kling-video/text-to-video",
headers={"Authorization": f"Key {FAL_KEY}"},
json={"prompt": "a cat watching rain", "duration": "5"},
)
job = submit_resp.json()
request_id = job["request_id"]
logger.info(f"Job submitted: {request_id}")
# Step 2: Poll for status
import time
while True:
status_resp = requests.get(
f"{FAL_API}/kling-video/text-to-video/requests/{request_id}/status",
headers={"Authorization": f"Key {FAL_KEY}"},
)
status = status_resp.json()
logger.info(f"Status: {status['status']}")
if status["status"] == "COMPLETED":
break
elif status["status"] == "FAILED":
raise Exception(f"Job failed: {status}")
time.sleep(5) # Poll every 5 seconds
# Step 3: Retrieve result
result_resp = requests.get(
f"{FAL_API}/kling-video/text-to-video/requests/{request_id}",
headers={"Authorization": f"Key {FAL_KEY}"},
)
video_url = result_resp.json()["video"]["url"]
Replicate Predictions API
Replicate uses a similar pattern:
import replicate
# Submit prediction
prediction = replicate.predictions.create(
model="stability-ai/stable-video-diffusion",
input={"image": image_url},
)
prediction_id = prediction.id
# Poll for status
while prediction.status not in ["succeeded", "failed", "canceled"]:
prediction.reload()
time.sleep(3)
if prediction.status == "succeeded":
video_url = prediction.output
Why can't you use a synchronous HTTP request for video generation?
Video generation takes 30 seconds to 5 minutes. HTTP connections typically time out at 30-60 seconds. Video APIs return a job_id immediately (in <1s), then you poll for status or register a webhook to receive a callback when the video is ready. This async pattern is standard across all video generation APIs (fal.ai, Replicate, OpenAI Sora, Runway).
Unlock the full lesson
You've read the first 2 sections. The rest of this lesson covers Webhook Pattern, Error Handling, Job Manager, Key Takeaways, What's Next — plus a hands-on lab, quiz, and project artifact.
Create a free account to unlock Phase 0 and Phase 1 of every course — no credit card.
Browse all courses · View pricing · DeVenture Academy