Created
February 24, 2026 04:18
-
-
Save SecurityQQ/4b1851ad25e391b866cb88a85fc2ec93 to your computer and use it in GitHub Desktop.
Minimal example: async render API with SSE streaming (varg render service Phase 4)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Async Render API — Minimal Example | |
| # | |
| # The render service now returns 202 immediately and processes in the background. | |
| # Use polling or SSE to wait for completion. | |
| ## 1. Submit a render (202 instant response) | |
| ```bash | |
| curl -s -X POST https://render-fqb8zg.fly.dev/api/render \ | |
| -H "Content-Type: application/json" \ | |
| -H "Authorization: Bearer $VARG_SERVICE_KEY" \ | |
| -d '{ | |
| "code": "export default <Render><Clip><Image prompt=\"a cat in space\" /></Clip></Render>;", | |
| "gatewayApiKey": "varg_xxx", | |
| "mode": "preview" | |
| }' | |
| ``` | |
| Response (instant, <200ms): | |
| ```json | |
| { "job_id": "5f650644-fdba-47d5-b889-06b6be92826f", "status": "rendering" } | |
| ``` | |
| ## 2a. Option A — Poll for completion | |
| ```bash | |
| curl -s -H "Authorization: Bearer $VARG_SERVICE_KEY" \ | |
| https://render-fqb8zg.fly.dev/api/render/jobs/5f650644-fdba-47d5-b889-06b6be92826f | |
| ``` | |
| While rendering: | |
| ```json | |
| { "job_id": "5f650644-...", "status": "rendering", "created_at": "..." } | |
| ``` | |
| When done: | |
| ```json | |
| { | |
| "job_id": "5f650644-...", | |
| "status": "completed", | |
| "output_url": "https://s3.varg.ai/renders/xxx.mp4", | |
| "files": [{ "url": "...", "mediaType": "image/png", "metadata": { "type": "image", "model": "flux-schnell", "prompt": "a cat in space" } }], | |
| "duration_ms": 2329, | |
| "completed_at": "..." | |
| } | |
| ``` | |
| ## 2b. Option B — SSE (push, no polling) | |
| ```bash | |
| curl -N -H "Authorization: Bearer $VARG_SERVICE_KEY" \ | |
| -H "Accept: text/event-stream" \ | |
| https://render-fqb8zg.fly.dev/api/render/jobs/5f650644-fdba-47d5-b889-06b6be92826f/stream | |
| ``` | |
| Output (server pushes events, connection auto-closes after terminal): | |
| ``` | |
| event: status | |
| data: {"job_id":"5f650644-...","status":"rendering"} | |
| event: status | |
| data: {"job_id":"5f650644-...","status":"completed","output_url":"https://s3.varg.ai/renders/xxx.mp4","files":[...],"duration_ms":2329} | |
| ``` | |
| ## 3. Minimal JS client | |
| ```ts | |
| async function renderVideo(code: string, apiKey: string): Promise<string> { | |
| const SERVICE_KEY = process.env.VARG_SERVICE_KEY!; | |
| // 1. Submit | |
| const res = await fetch("https://render-fqb8zg.fly.dev/api/render", { | |
| method: "POST", | |
| headers: { | |
| "Content-Type": "application/json", | |
| Authorization: `Bearer ${SERVICE_KEY}`, | |
| }, | |
| body: JSON.stringify({ code, gatewayApiKey: apiKey }), | |
| }); | |
| const { job_id } = await res.json(); | |
| // 2. Stream via SSE until done | |
| const sse = await fetch( | |
| `https://render-fqb8zg.fly.dev/api/render/jobs/${job_id}/stream`, | |
| { headers: { Authorization: `Bearer ${SERVICE_KEY}` } }, | |
| ); | |
| const reader = sse.body!.getReader(); | |
| const decoder = new TextDecoder(); | |
| let buf = ""; | |
| while (true) { | |
| const { done, value } = await reader.read(); | |
| if (done) break; | |
| buf += decoder.decode(value, { stream: true }); | |
| for (const line of buf.split("\n")) { | |
| if (!line.startsWith("data: ")) continue; | |
| const event = JSON.parse(line.slice(6)); | |
| if (event.status === "completed") return event.output_url; | |
| if (event.status === "failed") throw new Error(event.error); | |
| } | |
| buf = buf.slice(buf.lastIndexOf("\n") + 1); | |
| } | |
| throw new Error("Stream ended without terminal status"); | |
| } | |
| // Usage | |
| const url = await renderVideo( | |
| 'export default <Render><Clip><Image prompt="a cat in space" /></Clip></Render>;', | |
| "varg_xxx", | |
| ); | |
| console.log("Video:", url); | |
| ``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment