How to get YouTube video thumbnail using Node.js ?
Last Updated :
24 Jul, 2024
The following approach covers how to get the YouTube video thumbnails in nodeJs. We will use the youtube-thumbnail node-package to achieve so. This package will help us for getting the YouTube video thumbnail with the help of the video URL or watch code.
Use the following steps to install the module and get the YouTube video thumbnail in node.js:
Step 1: Creating a directory for our project and making that our working directory.
$ mkdir youtube-extract-gfg
$ cd youtube-extract-gfg
Step 2: Use the npm init command to create a package.json file for our project.
$ npm init
or
$ npm init -y /* For auto add the required field */

Note: Keep pressing enter and enter “yes/no” accordingly at the terminus line.
Step 3: Installing the Express.js and youtube-thumbnail module. Now in your youtube-extract-gfg(name of your folder) folder type the following command line:
$ npm install express youtube-thumbnail

Step 4: Creating index.js file, our Project structure will look like this.

Step 5: Creating a basic server. Write down the following code in the index.js file.
index.js
const express = require('express');
const app = express();
app.get('/' , (req , res)=>{
res.send("GeeksforGeeks");
});
// Server setup
app.listen(4000 , ()=>{
console.log("server is running on port 4000");
});
Output: We will get the following output on the browser screen.
GeeksforGeeks
Step 6: Now let's implement the functionality by which we get the YouTube video thumbnail.
index.js
const express = require('express');
const youtubeThumbnail = require('youtube-thumbnail');
const app = express();
// Basic Server
app.get('/' , (req , res)=>{
res.send("GeeksforGeeks");
});
// YouTube thumbnail request handler
app.get('/:watchCode' , (req , res) => {
var watchCode = req.params.watchCode;
var url = `https://www.youtube.com/watch?v=${watchCode}`;
var data = youtubeThumbnail(url);
var thumbnail = data.high.url;
res.send(`<img src="${thumbnail}" alt="Thumbnail" />`);
});
// Server setup
app.listen(4000 , ()=>{
console.log("server is running on port 4000");
});
Step 7: Run the server using the following command.
node index.js
Output:
Similar Reads
How to get YouTube channel id using Node.js ? The following approach covers how to get the YouTube channel Id using NodeJS. We will use the @gonetone/get-youtube-id-by-url node-package to achieve so. This package will help us for getting the YouTube channel id by using the channel URL.Use the following steps to install the module and get the Yo
2 min read
How to download YouTube Video Thumbnail using HTML CSS JavaScript & PHP ? In this project, you will learn how to download a YouTube video's Thumbnail. We will use simple HTML, CSS, JavaScript and PHP for this purpose. The basic idea is to use a URL of a YouTube video, that is provided by the user as input, fetch its thumbnail image and download it. Approach:Create an inde
4 min read
How To Make Thumbnail For YouTube On YouTube, where there are millions of videos competing for attention, having an attractive thumbnail is important. Think of a thumbnail as the preview image that encourages people to click on your video. It's like the cover of a book or poster of a movie â if it looks interesting, people are more
5 min read
How to add Youtube Videos in Next.js ? In this article, we are going to learn how we can add Youtube Video in NextJs. NextJS is a React-based framework. It has the power to Develop beautiful Web applications for different platforms like Windows, Linux, and mac. The linking of dynamic paths helps in rendering your NextJS components condit
2 min read
How To Edit Videos with YouTube Studio Ever wondered about editing videos using YouTube Studio well it is possible as YouTube Studio allows you to edit your video with the exciting tools present in YouTube Studio. Let's dive into YouTube Studio and explore how to use YouTube Studio to edit videos. Tips for editing your videos for YouTube
6 min read