Codeconia -Learn Programming

How to Get YouTube Subscriber , Views and Video Counts using JavaScript

Hello guys,

Today we are going to make a javascript for getting the youtube channel subscriber count, total video views count, and total videos count. so we need an API key for that ..

https://console.developers.google.com/   Go to this website Create a project ->

the click library  -> search for YouTube Data API v3 ->

then Enable it  -> then click create credentials -> select Credential Type Public -> and get the api key

now get your channel ID  and start coding

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>YouTube API</title>
</head>
<body>
    YouTube Subscribers
    <div id="subscriberCount"></div>

    YouTube Video Views
    <div id="viewCount"></div>

    YouTube Video Count
    <div id="videoCount"></div>

    <script>

    
    const APIKey = 'Your API Key here';
    const Userid = 'Your Channel ID here';
    const subscriberCount= document.getElementById('subscriberCount');
    const viewCount = document.getElementById('viewCount');
    const videoCount = document.getElementById('videoCount');

   

    let getdata = () => {

        fetch(`https://www.googleapis.com/youtube/v3/channels?part=statistics&id=${Userid}&key=${APIKey}`)
        .then(response => {
            return response.json()
        })
        .then(data => {
            console.log(data);
            subscriberCount.innerHTML = data["items"][0].statistics.subscriberCount;
            viewCount.innerHTML = data["items"][0].statistics.viewCount;
            videoCount.innerHTML = data["items"][0].statistics.videoCount;
            
        })

    }

   getdata();
    </script>
</body>
</html>

 

 

 

 

 

Ajith Jojo

Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.

Most popular