Get YouTube Subscriber Count

Learn how to retrieve the subscriber count from any YouTube channel URL using Google Sheets and the YouTube API in this step-by-step tutorial.

So you want to get your YouTube subscriber count. We're going to grab the subscriber count of a YouTube, URL, any URL with a handle in it. We're gonna be able to do at the end of this video. I'm gonna show you every step of the way because there are lots of little steps. I just need to show you where to click. And what to do. Usually this is absolutely free. So if you do get charged, you might be doing too many, but, uh, I'm gonna show you how to do this and I have not been charged yet. So let's go to Extensions app script . And here I have a few steps that we need to do before we do some coding. And in the coding we need to do a function , something like get sub count. Or get subs of some channel, we'll get a channel URL.

We need to get the handle. So that is going to be the at part. We need to send that handle to the YouTube API to get a channel ID because we need a channel I idea that actually starts with like uc. It's a really like code. You can grab this a lot of the times you can find this. This is public information of like, this handle is this code. But when we're. Doing this sort of, uh, research or whatever you're doing this for, we are usually able to get the handle and this YouTube URL much, much easier than finding the id. So we'll just grab the ur, the URL, get the handle, go search for the id, get the id, and then we're gonna use the id, the channel ID to get the sub count through the API again. So let's set up this API.

Over on the left side, you'll see services. Actually, I've already done it, but I'm gonna show, I'm gonna remove it and I'm gonna show you how to find it. So you go over here to services on the left side, click add a service. Scroll down to YouTube data , A PIV three, select it and click add. It's that easy. That's how you activate the YouTube API here. But that's not all. Let's go add our Google Cloud project. We're going to project settings on the left side. We're gonna scroll down and have a Google Cloud platform project, and we need to enter a GCP number if you don't have one, let's do it. We are gonna go to console.cloud.google console.cloud.google.com. Okay. The key here is to be in the exact same account that you are in in your Google Sheet. So for example, I have a lot of Google accounts, and if I

go to console.cloud.google.com, it might sign in a different one, but I just need to make sure in consult.cloud.google.com. I'm in the correct, same domain, same account as my Google sheet. And I can create a new project. I'm gonna create a new project here called yt Sub Count. Need a billing account. It's still free. It's gonna create this, and then I have to select that project. Make sure you select that project. Make sure the name you have given it up here on the top Leftish area is the exact account. Or project that you want. Then I'm gonna grab my project number from this project info over on the left side, and I'm gonna put that inside my project number here and set project. This will ask you to do an OAuth consent screen, so let's go do that.

We're gonna go to navigation APIs and services and go to OAuth consent screen. It also gives you a little link here, but this link, again, if you're in multiple accounts, it's gonna give you the link to the different account. So I like to go here, so I like to just select it myself. I'm gonna get started. This is fairly simple. I'm just gonna call it yt sub count. My email is gonna be my email. Hit next internal. This means you do not get me to get verification for this app. In any way or app script , click next. I'm gonna put my email address. Click next. I agree. Continue create. That's the OAuth consent screen, and it'll say OAuth configuration created Thumbs up. Great.

So go back to our G-C-G-C-P project number and click set project. And you'll see now that it is set. So go back to our editor . We have activated our YouTube API. We have added a Google Cloud project number and OAuth. Now we need to enable the YouTube API go back to our console, dot cloud.google.com on the navigation menu. APIs and services enabled. APIs and services. And then at the top it'll say a little plus sign. Enable APIs and services. Click that, and here we'll search for YouTube. Hit enter and you'll have a few options. You have YouTube reporting, YouTube analytics. We are using YouTube data . A PIV three enable. Just need to know where to click. This isn't fairly difficult yet. You'll see as we get into coding, we need to add a few.

URLs here, but this has now been enabled and we need a very special thing, which is I need, we need to create A API key. So let's say that is done enabling YouTube API, but now we need an API key because we're gonna put it right here. API key equals, and we're gonna be able to have this accessible basically all the time to any functions we write Here. I'm gonna click on credentials. Back over@console.cloud.google.com. I need to create con credentials. API key. You can name it if you want. I have no restrictions and obviously by the time you see this video, I'm gonna roll this and delete this API key. It's not gonna be accessible, but you'll do your own. And I'm not having any restrictions on it. I'm just gonna go create it. Oh, I need to actually select YouTube data API, okay, there and none and create.

So I'm gonna copy this and it gives you some. I, uh, best practices do not like this video. Do not be sharing this. API key with anyone. Uh, they'll have full access to your account. They'll have, be able to use this URL as much as they want. Don't do that. Just put it in your app script . If you are trying to put it in your app script , like hide it or save it in some way, there is a way to use user properties . Go check out other videos that I have and over@bettersheets.co. There are lots of videos on how to use app script and I go deep into it and one of the coolest ways is to use user properties or, or script properties to save this information. We're not gonna go over that in this video. Just put your, your, uh, your API key here for. This example, I'm the only one using this sheet.

I'm the only one that has access to it. Obviously you do 'cause you're watching, but do as I say, not as I do essentially. But actually this is fine to do if you are the only one that has access to this sheet. Alright, so I'm gonna save this API key here as API. Key equals there, so we'll have access to it. So we have done all of the things we need to do in order to start coding. So let's start coding. We want to get this handle and get the id, so we'll say cons, handle equals channel URL, match . And here it's a bit like, uh, rejects. Just need to do youtube.com. Say slash.com/the other slash and at and in here at the end we'll do a slash make sure that's purple.

We will do in brackets slash w dot. This is just saying, Hey, after youtube.com and this at Get whatever is here before the last slash cool. So like just get the handle part of the URL. There's a bit like rejects or regular expressions rejects. Now we want to get the channel, the handle, URL, basically based on the handle. What's the channel id? So the, so this is a bit of a API. URL, so it's google apis.com/youtube/v three slash search. Question mark part equals snippet and type equals channel and Q equals, and then we are adding plus handle one. One thing we also need to do is ampersand key equals, and

you guessed it, plus API key. So we're taking that API key from up here, putting it at the end and saying, Hey, we have access to to this. URL give us the answer of whatever this query is and let's handle the response. It'll be URL fetch app fetch and we'll go handle URL. We'll send that. Essentially this URL fetch is sending that URL getting a response, and what that response is gonna have is a channel ID. Let's get the data first. Jsun pars it. We'll do handle response. Do get content, text, text. Basically this is saying, whatever you are, rep, whatever you're replying with here, give us that text. Text, and then we're gonna parse it with some JSON. So our channel ID is equal to this channel ID data item zero. Basically, the first thing that we get back, we'll get a snippet

and we'll access it by channel id. So now that we have the channel id, all we have to do is go to a different URL of this API. And grab the subscriber count actually gives us a lot of other information, like statistics and stuff. But right now we're only interested in the subscriber count. So this is the first part of. The statistics, and as you can see, it's a little different. This URL is a little different. It's google apis.com/youtube/v three slash channels instead of search question mark part equals statistics that we're what we're looking for. And this ID is gonna be our channel ID that we get from before channel id. And again, we need to add 10%. Equals plus API key.

Let's get the response of this URL fetch app fetch the URL. So we're just saying, Hey, go to this URL, give us back what the answer is. That's gonna be, let's bring this up a little bit to the middle of the screen. Our data is equal to JSON parse, same as before. And we're gonna do response, do get content, text, and then this is our final thing. Based on this data , we don't want to just return all of that data , all of this, all of these statistics. We will return data dot items. The very first thing we get back, zero item, zero first dot statistics. Dot subscriber count, and this is what we will return. So I'm gonna save all of this script here and use this function .

Get subs and C if it works. Instead of these counts, we'll do equals get subs. We'll reference a two. In parentheses, it will say unknown function . I'm gonna show you how to fix that later. Once this starts working, we have an error . Cannot read line zero, line 12. Oh, I think we missed an S here. Items. Yeah, items. There we go. Let's save that. And now we have a number . Wow. So copy paste this down and our reference changes. Very cool, right? They are, uh, formatted a little oddly. So let's format number and use this, but nothing happens. Okay? Let's wrap this with text and do the number format this way.

Hashtag comma three hashtags. And we get some nice formatting there. Perfect. But what if we want to get rid of this underlined red? Get subs. I'll show you that just above the function . Get subs. We're gonna say slash slash slash slash two a asterisk. Oh my god. At custom function . I'm gonna hit save now I'm gonna delete these and write equals. There it is equals get the Gets. You get subs so we can just write it in. It is auto filling. Get subs. That's really cool. Now we're gonna again wrap it with text, hashtag three hashtags. And again, we can copy and paste this as much as we want. We are getting the live, live ish actual subscriber count of YouTube channels

scraping with not that much code. Honestly. We're just accessing these two APIs. Take the hand, get the handle, go. Once we get the Id, go grab the subscriber account. Isn't that cool? But we did have a long journey To get there. We had to activate our YouTube API, add the Google Cloud project number , do that OAuth screen, enable the YouTube API, and create our API key. Again, I'll roll this before you see it, but hopefully you had a really good time getting the subscriber count. You are watching better sheets here on YouTube. Make sure you check out this video or this video and subscribe right now to get more tips, tricks, how tos, get more out of your Google sheets than you ever have before. I'm excited to be making a ton more videos here. Ask me questions down in the comments and I will answer them in future videos.

But for right now, right here, one of these videos is gonna be your next Google sheet.