This project provides a Cloudflare Worker API endpoint to fetch YouTube video transcripts.
It uses youtubei.js
to interact with YouTube's internal API and Hono as the routing framework.
- Node.js (v18 or later recommended)
- npm (or yarn/pnpm)
- A Cloudflare account
- Wrangler CLI installed and configured (you can also use the version installed by npm/yarn from the project dependencies).
-
Clone the repository (if you haven't already):
git clone <your-repo-url> cd youtube-transcript-api
-
Install dependencies:
npm install
(or
yarn install
orpnpm install
)
To run the worker locally for development, use the following command:
npm run dev
This will start a local server, typically on http://localhost:8787
. Wrangler will output the exact URL.
- GET /
- Fetches the transcript for a given YouTube video ID or URL.
- Query Parameter:
id
(required) - The YouTube video ID (e.g.,dQw4w9WgXcQ
) or full YouTube URL (e.g.,https://www.youtube.com/watch?v=dQw4w9WgXcQ
). - Success Response (200 OK):
{ "videoTitle": "Video Title Here", "transcript": [ { "text": "Hello and welcome...", "offset": 0.5, "duration": 3.2 }, { "text": "In today's video...", "offset": 3.7, "duration": 2.8 } // ... more segments ] }
- Error Responses:
400 Bad Request
: If theid
query parameter is missing or invalid.404 Not Found
: If no transcript is available for the video.500 Internal Server Error
: For other server-side issues (e.g., problems fetching from YouTube, JSON parsing errors).
Example Usage (with wrangler dev
running):
http://localhost:8787/?id=dQw4w9WgXcQ
Or using a video URL:
http://localhost:8787/?id=https://www.youtube.com/watch?v=o_XVt5rdpFY
To deploy the worker to your Cloudflare account:
- Ensure you have logged in with Wrangler (
wrangler login
). - Run the deploy command:
npm run deploy
Wrangler will build and deploy your worker. After deployment, it will output the URL for your live worker.
If you need to add environment variables or bind services (like KV, R2, D1), you can configure them in the wrangler.toml
file.
If you add bindings (KV, Durable Objects, etc.) to wrangler.toml
, you can generate corresponding TypeScript types by running:
npm run cf-typegen
This helps with type safety when interacting with Cloudflare resources.
We also build tools to help you get the most out of YouTube videos:
-
YoutubeVideoTranscripts.com: A website to get the transcript of any YouTube video for free. Watch the video while reading the transcript, search within the transcript, and download it for offline use.
-
Youtube Transcript Plus (Chrome Extension): Enhance your YouTube experience directly in your browser. This Chrome Extension provides a full transcript reading experience while watching, allows you to chat with the video using AI, and can generate chapter summaries using AI. A must-have for serious study on YouTube.