Hire the author: Lakshay G
Introduction
In this tutorial I’ll cover how to build your own youtube video title updater with Node.js. To build this I will be using Youtube Data API. Head over to this link to view my project’s result. The project’s Github repository is available here.
Motivation
A while back, I watched a Youtube video with the same concept but the person didn’t exactly explain how he did that. So this made me curious and motivated to dig deep inside the Youtube data API.
Goal
My goal with this project is to build this youtube video title updater with the greatest ease, and deploy it free of cost online.
Glossary
Cron: Cron is a time-based job scheduler in Unix-like computer operating systems.
Scheduling: Scheduling is the method by which work is assigned to resources that complete the work.
Project Requirements
- A gmail account is required
- Basic programming fundamentals are a must
- Must have a little bit idea about Node.js and JavaScript
Step by Step Tutorial
- First of all, head over to this Link and follow the first four steps mentioned there.
NOTE: While creating credentials, you need to select DESKTOP Application type (because there is no other application type option available) - After that create a .env file which consists of the VIDEO_ID, client_secret and access token which is coming from the first step.
Refer below to check the .env file sample:- - Now create the server.js file that consists of the authorize and update video title and the get video views functions as shown below:-
Sample .env file below:
OAUTH_TOKEN=/*copy/paste the CONTENTS OF YOUTUBE-NODEJS-QUICKSTART.JSON FILE as it is in json format*/ | |
CLIENT_SECRET=/*copy/paste the CONTENTS OF CLIENT_SECRET.JSON FILE as it is in json format*/ | |
VIDEO_ID=your video Id |
server.js file below:-
require('dotenv').config() | |
var { | |
} = require('googleapis'); | |
var OAuth2 = google.auth.OAuth2; | |
const VIDEO_ID = process.env.VIDEO_ID | |
//main() | |
async function main() { | |
try { | |
const auth = authorize() | |
const videoViews = await getVideoViews(auth) | |
const videoTitle = await updateVideoTitle(auth, videoViews) | |
console.log(videoTitle) | |
} catch (e) { | |
console.error(e) | |
} | |
} | |
function authorize() { | |
const credentials = JSON.parse(process.env.CLIENT_SECRET) | |
var clientSecret = credentials.installed.client_secret; | |
var clientId = credentials.installed.client_id; | |
var redirectUrl = credentials.installed.redirect_uris[0]; | |
var oauth2Client = new OAuth2(clientId, clientSecret, redirectUrl); | |
oauth2Client.credentials = JSON.parse(process.env.OAUTH_TOKEN); | |
return oauth2Client | |
} | |
function getVideoViews(auth) { | |
const service = google.youtube('v3') | |
return new Promise((resolve, reject) => { | |
service.videos.list({ | |
auth: auth, | |
part: 'statistics', | |
id: VIDEO_ID | |
}, function(err, response) { | |
if (err) return reject(err) | |
resolve(response.data.items[0].statistics.viewCount) | |
}) | |
}) | |
} | |
function updateVideoTitle(auth, views) { | |
const service = google.youtube('v3') | |
return new Promise((resolve, reject) => { | |
service.videos.update({ | |
auth: auth, | |
part: 'snippet', | |
resource: { | |
id: VIDEO_ID, | |
snippet: { | |
title: `This Video Has ${new Intl.NumberFormat('en-US').format(views)} Views`, | |
description: `Happy Coding! | ${new Intl.NumberFormat('en-US').format(views)} Views`, | |
categoryId: 27 | |
} | |
} | |
}, function(err, response) { | |
if (err) return reject(err) | |
resolve(response.data.snippet.title) | |
}) | |
}) | |
} | |
main(); |
These functions are pretty much self explanatory. All the parameters are required from the .env file in the first line (using dotenv npm package)
Now run this file using node server.js to check if everything works fine (from turning on youtube data api to updating the title of the video)

4. In order to automate this process of updating the youtube video title using Youtube data API, I will create a cron job.
For this, install two npm packages namely node-cron and shelljs and create the scheduler file as follows:-
let cron = require('node-cron') | |
let shell = require('shelljs') | |
var express = require('express') | |
var app = express() | |
app.get('/', function(req, res) { | |
res.redirect("https://www.youtube.com/watch?v=wYOA4waoWog") | |
}) | |
cron.schedule("8 17,25,34,42,51,59 * * * *", function() { | |
console.log('scheduler running...') | |
if (shell.exec('node server.js').code !== 0) { | |
console.log('Something went wrong ') | |
} | |
}) | |
app.listen(process.env.PORT || 3000, () => console.log(`Server started on port 3000`)); |
Save this file as index.js
Now run node index.js, you will see something like this:

5. Last step is to deploy this project online. There are many places where the project can be deployed, for example: AWS EC2 instance, GCP, Azure, Heroku, Digital Ocean etc.
In order to deploy it free of cost, use heroku and follow the steps:

Learning Tools and Strategies
- Don’t write messy code, everything should be well understood. Doing this will automatically help you when it comes to debugging.
- Do a console.log() at each major step you feel is important, or that you feel may throw some kind of error. This is so that you are able to get the result of each major code-snippet.
- Another thing is to go through the documentation of Youtube data api thoroughly.
- The most important thing to keep in mind is that you need to schedule the cron job in such a way that it runs after every 8 to 9 minutes and not less than that. Because there is a limited daily quota of requests that youtube provides.
Search terms
youtube, video, title, updater, node, youtube api
Workaround
I prefer working in a quiet environment so that I am completely focused
Reflective Analysis
It is a very cool project. At first when I saw this I thought it was a hack or glitch or something of that sort. There are many other things that you can do with the youtube data API too, which means the possibilities for future are vast.
Conclusion
The project gives a detailed idea of how to implement a youtube video title updater using Youtube data API and node.js. There are many other things that you can do with the youtube data API. Do play around with this API.
Citations
I used https://www.flickr.com/photos/korosirego/4481461680 for the featured image
The project is available here.
This a very nice work, though i have few comments:
1. I think it will be best to create an env.sample file to show users what exact variable and variable names they need to create in their own env file.
2. I think you will need to make a note of the change in steps while setting the Google AUTH CLIENT because i noticed some things where missing from their step and i had to reach out to you.
Other than this, the App works pretty fine. Cudos!!1
Thanks
where to get ?
OAUTH_TOKEN=/*copy/paste the CONTENTS OF YOUTUBE-NODEJS-QUICKSTART.JSON FILE as it is in json format*/