2026-11-26 · 5 min read
How to Caption Videos in Astro
Astro ships server endpoints from any file in src/pages/api when the site runs with an adapter. That is all you need to caption a video: one endpoint to submit the job and one to check it. The caption.sh API is plain HTTPS, so there is no SDK to install.
Submit the job from an endpoint
POST the video URL to the API. Documented styling options go in the nested options object, and the key stays in a server-only environment variable.
// src/pages/api/caption.ts
import type { APIRoute } from 'astro';
export const POST: APIRoute = async ({ request }) => {
const { url } = await request.json();
const res = await fetch('https://api.caption.sh/v1/videos', {
method: 'POST',
headers: {
Authorization: `Bearer ${import.meta.env.CAPTION_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
source_url: url,
options: { position: 'bottom', highlight: true },
}),
});
const { job_id } = await res.json();
return new Response(JSON.stringify({ job_id }), { status: 200 });
};Poll from a second endpoint
Jobs move through queued, running, and done. A small polling endpoint keeps the browser logic simple.
// src/pages/api/status.ts
import type { APIRoute } from 'astro';
export const GET: APIRoute = async ({ url }) => {
const id = url.searchParams.get('job');
const res = await fetch(`https://api.caption.sh/v1/jobs/${id}`, {
headers: { Authorization: `Bearer ${import.meta.env.CAPTION_API_KEY}` },
});
const job = await res.json();
return new Response(JSON.stringify({ status: job.status }), { status: 200 });
};Poll every few seconds and stop on done or error. When the status is done, GET /v1/jobs/{id}/result answers with a 302 redirect to the finished MP4. Results are stored for 24 hours, then deleted, so download the file and put it in your own storage if you need it longer.
Cost and limits
Captioning costs $0.10 per minute of video, prepaid. A single job can be up to 10 minutes long, so clip longer videos first. If you hit a 429, the transcription throughput ceiling is full: wait and retry.