2026-11-24 · 4 min read
How to Caption Videos in Deno
Deno's built-in fetch and permission model make it a tidy fit for API work. Captioning a video with caption.sh is three HTTPS calls, no npm packages and no shims.
Submit the job
POST the video URL to the API. Documented styling options go in the nested options object.
const KEY = Deno.env.get("CAPTION_API_KEY")!;
const API = "https://api.caption.sh";
async function submit(videoUrl: string) {
const res = await fetch(API + "/v1/videos", {
method: "POST",
headers: {
Authorization: "Bearer " + KEY,
"Content-Type": "application/json",
},
body: JSON.stringify({
source_url: videoUrl,
options: { caps: true, position: "bottom" },
}),
});
return (await res.json()).job_id;
}Run the script with --allow-net and --allow-env so fetch and Deno.env get their permissions. One job accepts a video up to 10 minutes long; split longer sources into segments first.
Poll on a deadline
Jobs move through queued, awaiting_upload, and running, then land on done or error. Poll with a deadline and cover all three exits.
async function waitForJob(jobId: string, budgetMs = 600_000) {
const deadline = Date.now() + budgetMs;
while (Date.now() < deadline) {
const st = await fetch(API + "/v1/jobs/" + jobId, {
headers: { Authorization: "Bearer " + KEY },
}).then((r) => r.json());
if (st.status === "done") return;
if (st.status === "error") throw new Error("caption job failed");
await new Promise((r) => setTimeout(r, 10_000));
}
throw new Error("timed out waiting for " + jobId);
}A 429 response means the transcription throughput ceiling is saturated. Wait and retry; it clears on its own.
Fetch the finished video
The result endpoint answers with a redirect to the file. Use redirect: "manual" and read the Location header yourself.
async function resultUrl(jobId: string) {
const res = await fetch(API + "/v1/jobs/" + jobId + "/result", {
headers: { Authorization: "Bearer " + KEY },
redirect: "manual",
});
return res.headers.get("Location");
}Results are stored for 24 hours, then deleted, so download the file in the same run with --allow-write. Pricing is prepaid at $0.10 per minute of video.
Where this fits
- Fresh scripts and edge-adjacent tooling where you want TypeScript without a build step.
- Scheduled jobs on Deno Deploy or a cron runner.
- Pairs naturally with serverless stacks that already run Deno.
Burned-in captions cannot be toggled, resized, or translated by the viewer. Where accessibility matters, keep the platform's own caption track enabled alongside the burn-in.