2026-11-25 · 5 min read

How to Caption Videos in Next.js

Dark film strip with the glowing white neon caption word NEXT on a black background.
This image was generated with AI.

If your product already runs on Next.js, adding caption burn-in is a pair of route handlers and a scheduled check. The caption.sh API is plain HTTPS, so no SDK is needed.

Submit from a route handler

POST the video URL to the API. Documented styling options go in the nested options object, and the key lives in a server-only environment variable.

// app/api/caption/route.ts
import { NextResponse } from "next/server";

export async function POST(req: Request) {
  const { videoUrl } = await req.json();

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

  // store job.job_id against the upload in your database
  return NextResponse.json({ job_id: job.job_id });
}

One job accepts a video up to 10 minutes long; split longer uploads into segments first.

Poll on a schedule, not in the request

Jobs move through queued, awaiting_upload, and running, then done or error. A Vercel Cron job or a queue worker can check open jobs every minute and update their rows. Cover three exits per job: done, error, or a timeout you choose.

// app/api/caption/poll/route.ts (called by your scheduler)
export async function GET() {
  const openJobs = await getOpenJobs(); // your database

  for (const job of openJobs) {
    const st = await fetch("https://api.caption.sh/v1/jobs/" + job.id, {
      headers: { Authorization: "Bearer " + process.env.CAPTION_API_KEY },
    }).then((r) => r.json());

    if (st.status === "done") await fetchResult(job.id);
    if (st.status === "error") await markFailed(job.id);
  }
  return NextResponse.json({ checked: openJobs.length });
}

A 429 means the transcription throughput ceiling is saturated; wait for the next scheduled run and retry.

Fetch the finished video

The result endpoint answers with a redirect, so call it with redirect: "manual" and read the Location header.

async function fetchResult(jobId: string) {
  const res = await fetch(
    "https://api.caption.sh/v1/jobs/" + jobId + "/result",
    { headers: { Authorization: "Bearer " + process.env.CAPTION_API_KEY },
      redirect: "manual" }
  );
  const url = res.headers.get("Location");
  // download url and store the file in your own blob storage
}

Results are stored for 24 hours, then deleted, so copy the file into your own storage as soon as the job completes. Pricing is prepaid at $0.10 per minute of video.

Burned-in captions cannot be toggled, resized, or translated by the viewer. If your app serves an audience that needs adjustable captions, keep a native caption track available alongside the burn-in.

Back to all articles