Skip to main content
You want a Next.js route that takes a YouTube link and returns the transcript. Scraping captions from a Vercel function gets blocked, and many videos have no captions at all. This route sends the link to Importly, which uses the captions when there are some and speech-to-text when there aren’t, and returns the result in the same request.

The route

app/api/youtube-transcript/route.ts
Put the key in .env.local and in your host’s environment variables as IMPORTLY_API_KEY, without a NEXT_PUBLIC_ prefix, so it never reaches the browser. Create it on the API key page.

What comes back

source is platform_captions when the video’s own captions were used and speech-to-text otherwise. Segment times are in seconds, ready for subtitles, chapters or search. Shorts and youtu.be links work the same way.

Errors to handle

Polling, a form and webhooks

The Next.js recipe adds the polling route, a client form and a webhook for saving transcripts to your database. Options such as language and your own speech-to-text key are in Transcripts.
A transcript from captions costs $0.003. Speech-to-text is $0.01 per minute. Try it free, no card required. See Pricing & credits.