2026-11-22 · 4 min read

How to Caption Videos from a Cloudflare Worker

AI-generated hero image: a dark 35mm film strip with the word WORKERS glowing in coral orange neon at the center.
This image was generated with AI.

Cloudflare Workers are a good home for glue code that should run close to your users: a webhook receiver, a small API, a form handler. If one of those flows should produce a captioned video, the caption.sh API is a plain HTTPS API, so a Worker can drive it with the built-in fetch and nothing else.

Submit the job

POST the video URL to the API. Documented styling options go in the nested options object.

export default {
  async fetch(request, env) {
    const { videoUrl } = await request.json();

    const res = await fetch("https://api.caption.sh/v1/videos", {
      method: "POST",
      headers: {
        Authorization: "Bearer " + env.CAPTION_API_KEY,
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        source_url: videoUrl,
        options: { caps: true, position: "bottom" },
      }),
    });
    const job = await res.json();
    return Response.json({ job_id: job.job_id });
  },
};

Keep your key in a Workers secret, never in the source. One job accepts a video up to 10 minutes long; split longer sources into segments and submit one job per segment.

Poll without blocking the request

A Worker invocation should not sit open for minutes while a job runs. The simple pattern is two endpoints: one submits, the other checks status on demand. Jobs move through queued, awaiting_upload, running, then done or error.

async function checkJob(jobId, env) {
  const st = await fetch("https://api.caption.sh/v1/jobs/" + jobId, {
    headers: { Authorization: "Bearer " + env.CAPTION_API_KEY },
  }).then((r) => r.json());

  if (st.status === "done") {
    const out = await fetch(
      "https://api.caption.sh/v1/jobs/" + jobId + "/result",
      { headers: { Authorization: "Bearer " + env.CAPTION_API_KEY },
        redirect: "manual" }
    );
    return Response.json({ status: "done", url: out.headers.get("Location") });
  }
  return Response.json({ status: st.status });
}

The result endpoint answers with a redirect to the finished file, so use redirect: "manual" and read the Location header yourself. Results are stored for 24 hours, then deleted; if the file matters, copy it to R2 as soon as the job completes.

Operational notes

  • A 429 response means the transcription throughput ceiling is saturated. Wait and retry; it clears on its own.
  • Pricing is prepaid at $0.10 per minute of video, so captioning a 60 second clip costs about ten cents.
  • For fan-out batches, pair submissions with Queues or a Cron Trigger instead of holding requests open.

Burned-in captions cannot be toggled, resized, or translated by the viewer. Where accessibility is a concern, keep the platform's own caption track enabled alongside the burn-in.

Back to all articles