NightDev Community Forums

Create a command to get YouTube Subscribers

So after a good amount of research, I found out that nobody had created (or at least documented) a good way to get YouTube Subscribers in a Nightbot command. So as any good coder/programmer/engineer does, I created a way. And honestly I’m pretty proud of it.

Here are the steps to create this command.

First of all there is no YouTube nightbot API it seems that covers getting this information. But it just so happens that you can create a Google API Key, and then from there use the Google API to get a small JSON document that has this number.

The link to this instructions for this is right here:

Once you do that, then you have to create the URL, I found this URL and changed it to my liking:

https://www.googleapis.com/youtube/v3/channels?part=statistics&id=CHANNEL_ID&fields=items/statistics/subscriberCount&key=API_KEY

Replace the Channel ID and the API key as follows (both are given in the instructions above).

Once you first go to this link in your browser, it will tell you that you need to follow a link to enable Google API Data… Something or another (it was quite a few hours ago that I did this).

Once you go to the link and enable it, refresh the page and then you should get some good old JSON.

Okay so now we need to parse that information so that we just get the subscriber number.

What I found the best way to do this, is to use $(urlfetch json) to parse the code. Now that you have that, you want to put that in a variable and then use it somehow. Here is my final command:

!addcom !youtube $(eval subs = JSON.parse( ‘$(urlfetch https://www.googleapis.com/youtube/v3/channels?part=statistics&id=CHANNELID&key=APIKEY )’ ); 'I have a Youtube channel! Currently we have ’ + subs.items[0].statistics.subscriberCount + ’ subscribers! Check it out at: https://www.youtube.com/user/channel ')

And voila! You now have a way to inject the amount of subscribers into your nightbot commands! Thank you for reading


Edited: Changed the Link to use Channel ID and to use $(urlfetch JSON) (Channel ID is going to be more reliable as that works better with youtube).
Thank you to u/SecretOil for the JSON and RokettoJanpu on NightDev Community for your input!

In the past, there have been mentions of adding a $(youtube) variable to fetch information such as this, but this has been, as far as I know, put on the back burner. I appreciate someone taking the time to explain how to interact with YouTube’s API to create your own subscriber count command. However, there are a few things I’d like to point out:


Data requests with YouTube’s v3 API work best when supplied with channel IDs instead of usernames. Using usernames generally works nicely for users with a custom channel URL but will return empty responses for many other channels, specifically those that don’t have custom URLs. Therefore for this command to work for all channels I would use this URL where CHANNEL_ID is replaced with the channel ID and API_KEY is replaced with your API key:

https://www.googleapis.com/youtube/v3/channels?part=statistics&id=CHANNEL_ID&fields=items/statistics/subscriberCount&key=API_KEY

Using $(urlfetch) nested inside $(eval) to decode YouTube API’s response and output just the subscriber count is a nice way to go about this. It would work fine in a command for a Twitch channel. However, if you were to try making this command for a YouTube channel, you would run into an issue considering the 200 character limit on YouTube commands.


Taking all this into consideration, I wrote a script that gets a channel’s subscriber count that you can easily use by setting up the following command with CHANNEL_ID replaced with the YouTube channel ID you are looking up:

!addcom -cd=5 !youtube $(urlfetch https://rokbot.xyz/ytsubs.php?id=CHANNEL_ID)

If you want this command to always fetch the channel owner’s subscriber count, you could replace CHANNEL_ID with $(channelid), or if you want the command to fetch the command user’s subscriber count, replace it with $(userid)

1 Like

Thank you! This was just a rudimentary command, and on my reddit post I created somebody suggested using $(urlfetch json URL) to parse the JSON in a much more simple way. I will be editing this post to include that soon :slight_smile: . I didn’t know you had that script though. Thank you for that!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.