This guide will walk you through creating a Strapi plugin that fetches YouTube video transcripts. If you are wondering how to get a transcript of a YouTube video and learn how to build a Strapi plugin, then this tutorial is for you.
Here is a demo of what we will be building.
You cand find the video tutorial here.
We will start by setting up a new Strapi CMS project. Let's follow the following steps:
npx create-strapi-app@latest get-yt-summary
➜ temp npx create-strapi-app@latest get-yt-summary
Need to install the following packages:
create-strapi-app@5.0.5
Ok to proceed? (y) y
Strapi v5.0.5 🚀 Let's create your new project
We can't find any auth credentials in your Strapi config.
Create a free account on Strapi Cloud and benefit from:
- ✦ Blazing-fast ✦ deployment for your projects
- ✦ Exclusive ✦ access to resources to make your project successful
- An ✦ Awesome ✦ community and full enjoyment of Strapi's ecosystem
Start your 14-day free trial now!
? Please log in or sign up. Skip
? Do you want to use the default database (sqlite) ? Yes
? Start with an example structure & data? No
? Start with Typescript? Yes
? Install dependencies with npm? Yes
? Initialize a git repository? Yes
cd get-yt-summary
yarn develop
After creating the user, you will be redirected to the admin page:
We are now ready to start building our Strapi plugin.
To help us get started with our YouTube transcript generator plugin, we will use the Strapi Plugin CLI SDK. You can learn more about it here.
npx @strapi/sdk-plugin@latest init get-yt-transcript-plugin
[INFO] Creating a new package at: src/plugins/get-yt-transcript-plugin
✔ plugin name … get-yt-transcript-plugin
✔ plugin display name … YT Transcript
✔ plugin description … Get YT transcript.
✔ plugin author name … Paul Brats
✔ plugin author email … paul.bratslavsky@strapi.io
✔ git url …
✔ plugin license … MIT
✔ register with the admin panel? … no
✔ register with the server? … yes
✔ use editorconfig? … yes
✔ use eslint? … yes
✔ use prettier? … yes
✔ use typescript? … yes
Now that we have a plugin, we must configure it to be available in our Strapi application.
config/plugins.ts
:1export default {
2 "get-yt-transcript-plugin": {
3 enabled: true,
4 resolve: "./src/plugins/get-yt-transcript-plugin",
5 },
6};
cd src/plugins/get-yt-transcript-plugin
yarn install
yarn build
yarn watch
yarn develop
We should now see the plugin in the sidebar in the admin panel:We now need to update our routes to allow us to access the plugin's exposed endpoints and use it outside of the admin panel.
Navigate to the plugin folder and let's make the following change in the src/plugin/get-yt-transcript-plugin/server/src/routes/index.ts
file:
Currently, the file looks like this:
1export default [
2 {
3 method: "GET",
4 path: "/",
5 // name of the controller file & the method.
6 handler: "controller.index",
7 config: {
8 policies: [],
9 },
10 },
11];
Currently, this defaults to the admin routes, which are meant to be used inside the admin panel.
In Strapi we have two types of routes:
content-api
: These routes are meant to be used outside the admin panel.admin
: These routes are meant to be used inside the admin panel.Let's update our routes to include both types.
1. Update src/plugins/get-yt-transcript-plugin/server/src/routes/index.ts
:
1import contentApi from "./content-api";
2import admin from "./admin";
3
4export default {
5 "content-api": {
6 type: "content-api",
7 routes: [...contentApi],
8 },
9 admin: {
10 type: "admin",
11 routes: [...admin],
12 },
13};
Now we must create the content-api
and the admin
routes. Let's start with the content-api
routes.
Create a new file called content-api.ts
in the src/plugins/get-yt-transcript-plugin/server/src/routes
folder and add the following:
2. Create src/plugins/get-yt-transcript-plugin/server/src/routes/content-api.ts
:
1export default [
2 {
3 method: "GET",
4 path: "/",
5 handler: "controller.index",
6 config: {
7 policies: [],
8 },
9 },
10];
Now let's create the admin
route.
3. Create src/plugins/get-yt-transcript-plugin/server/src/routes/admin.ts
:
1export default [
2 {
3 method: "GET",
4 path: "/",
5 handler: "controller.index",
6 config: {
7 policies: [],
8 },
9 },
10];
You should now have the following file structure:
Nice. Now, let's test out our plugin in its current state to see if it works.
get-yt-transcript-plugin
.Select the get-yt-transcript-plugin
and then click on the index
endpoint checkbox. Make sure to save.
Here is a screenshot of what it should look like:
Now you should be able to test the endpoint by visiting: http://localhost:1337/api/get-yt-transcript-plugin
You should see the following response:
Now, we need to create the service that will fetch the YouTube transcript.
We will use the youtubei.js
library to fetch the transcript. You can learn more about it here.
yarn add youtubei.js
Let's start by updating the code in the services.ts
file found in the src/plugins/get-yt-transcript-plugin/server/src/services
folder.
2. Create src/plugins/get-yt-transcript-plugin/server/src/services/service.ts
:
1import type { Core } from "@strapi/strapi";
2
3const fetchTranscript = async (
4 url: string
5): Promise<(string | undefined)[] | undefined> => {
6 const { Innertube } = await import("youtubei.js");
7
8 const youtube = await Innertube.create({
9 lang: "en",
10 location: "US",
11 retrieve_player: false,
12 });
13
14 try {
15 const info = await youtube.getInfo(url);
16 const transcriptData = await info.getTranscript();
17 return transcriptData?.transcript?.content?.body?.initial_segments.map(
18 (segment) => segment.snippet.text
19 );
20 } catch (error) {
21 console.error("Error fetching transcript:", error);
22 throw error;
23 }
24};
25
26async function getYouTubeTranscript(videoUrl: string) {
27 const videoId = new URL(videoUrl).searchParams.get("v");
28 const transcript = await fetchTranscript(videoId);
29 return transcript?.join(" ");
30}
31
32const service = ({ strapi }: { strapi: Core.Strapi }) => ({
33 async getYoutubeTranscript(videoId: string) {
34 const youtubeIdRegex = /^[a-zA-Z0-9_-]{11}$/;
35 const isValid = youtubeIdRegex.test(videoId);
36
37 if (!isValid) return { error: "Invalid video ID", data: null };
38
39 try {
40 const baseUrl = "https://www.youtube.com";
41 const path = "/watch";
42 const url = new URL(path, baseUrl);
43 url.searchParams.set("v", videoId);
44
45 const transcript = await getYouTubeTranscript(url.href);
46 return transcript;
47 } catch (error) {
48 return { error: "Error fetching transcript: " + error, data: null };
49 }
50 },
51});
52
53export default service;
The above code defines a service fetching a YouTube video's transcript using the youtubei.js
library. Here's a breakdown of what the code does:
fetchTranscript
function:
undefined
if it is not available.Innertube.create()
with default settings (language: English, location: US, and without retrieving the video player).youtube.getInfo(url)
and then attempts to fetch the transcript with info.getTranscript()
.getYouTubeTranscript
function:
fetchTranscript
to get the transcript, and joins it into a single string.service
function:
service
function exports a Strapi service that fetches YouTube transcripts.getYouTubeTranscript
to retrieve the transcript.Once you have the service ready, we can focus on creating the controller. As we mentioned before when the request is made to a designated endpoint, Strapi will call the appropriate controller, which will then call the service we just created.
In the controller.ts
file found in the src/plugins/get-yt-transcript-plugin/server/src/controllers
folder, add the following code:
Create src/plugins/get-yt-transcript-plugin/server/src/controllers/controller.ts
:
1import type { Core } from "@strapi/strapi";
2
3const controller = ({ strapi }: { strapi: Core.Strapi }) => ({
4 async getYoutubeTranscript(ctx) {
5 ctx.body = await strapi
6 .plugin("get-yt-transcript-plugin")
7 .service("service")
8 .getYoutubeTranscript(ctx.params.videoId);
9 },
10});
11
12export default controller;
We updated the following code to reference the service we just created. Now that the controller is ready, we need to update the routes to point to it before we can test it.
Remember that we created two types of routes: content-api
and admin
. We need to update both of them to point to our new controller.
So make the following changes in both files, content-api.ts
and admin.ts
, found in the src/plugins/get-yt-transcript-plugin/server/src/routes
folder:
1export default [
2 {
3 method: "GET",
4 path: "/yt-transcript/:videoId",
5 handler: "controller.getYoutubeTranscript",
6 config: {
7 policies: [],
8 },
9 },
10];
Now that we have the routes updated, we can test the plugin.
But first, make sure to rebuild the plugin by running the following command in the root of your plugin folder:
yarn build
And restart your Strapi server by running yarn develop
.
After rebuilding and restarting the Strapi server, let's double-check our permissions. Since we updated the controller's name, we need to make sure that the Public
role has access to it.
Here is a screenshot of what it should look like:
Now let's test it out in Postman within VSCode.
I am going to make a GET
request to the following endpoint:
1http://localhost:1337/api/get-yt-transcript-plugin/yt-transcript/ZErV3aNdYhY
Just make sure you add a valid YouTube video ID.
You should see the following response:
Congratulations! You've now created a Strapi plugin that fetches YouTube video transcripts.
In this tutorial, we covered the process of creating our first Strapi plugin and how to get a transcript of a YouTube video by building a custom Strapi plugin.
We explored how to set up a new Strapi project, initialize a plugin, configure it, and implement the necessary components, such as routes, services, and controllers.
This example provides a foundation for building more complex plugins with additional functionality.
You can watch the video for this tutorial below, if you prefer the video version of this content.
If you have any questions or feedback, feel free to comment below. You can also join us for Strapi's "Open Office Hours" on Discord. We are here Monday through Friday.
Morning Session: 4 AM CST (9:00 AM GMT)
Afternoon Session: 12:30 PM CST (6:30 PM GMT)
Also, I am in the process of updating The Epic Next JS with Strapi 5 Blog Series to include this plugin.
As part of the update, we will use this plugin to fetch the transcript for the YouTube video to summarize it using AI.
Stay tuned!