How to get the channel ID of a YouTube channel

26 Apr 2024

Earlier I wrote a post about How to create your own self-hosted YouTube. When I wrote the post, you could grab the channel ID rather simple. You just opened a video on the channel, and then clicked the channel name just below the video. Then you would have the channel ID in the URL, like this:
https://www.youtube.com/channel/UCX6OQ3DkcsbYNE6H8uQQuVA
This method seems to have stopped working however, so let's do it another way.

Getting a YouTube channel ID through Google API

I found this StackOverflow question How can I get a channel ID from YouTube?, where an answer details the Google API endpoint that gives the ID. You simply make a GET request to the following URL:
https://www.googleapis.com/youtube/v3/channels?key=YOUR_API_KEY&forUsername=USERNAME&part=id
Replace USERNAME with the channel name, and YOUR_API_KEY with your API key. If you don't have an API key, I have described how to get one here: How to get your YouTube API key.

When doing a GET request on the above URL, you'll get a JSON response in return. It will look similar to this:
{
    'kind': 'youtube#channelListResponse', 
    'etag': '0OJX0B6esbO3cdBfycAScSh5cPM', 
    'pageInfo': {
        'totalResults': 1, 
        'resultsPerPage': 5
    },
    'items': [
        {
            'kind': 'youtube#channel', 
            'etag': 'xcfFMDuM69tPJiFWrWlfpwuDcqA', 
            'id': 'UCjFqcJQXGZ6T6sxyFB-5i6A'
        }
    ]
}
Load the JSON into a variable and you can access the ID like this:
id = response['items'][0]['id']

Using Python to get the Youtube channel ID

Combining the above steps into a Python script will give you the script below. Simply replace YOUR_API_KEY with your own API key, replace CHANNEL_USERNAME with the desired channel name and run the code below with Python. The script will return the channel ID.
import json
import requests

APIKEY = 'YOUR_API_KEY'
USERNAME = 'CHANNEL_USERNAME'
URL = 'https://www.googleapis.com/youtube/v3/channels?key=' + APIKEY + '&forUsername=' + USERNAME + '&part=id'

request = requests.get(URL)
response = json.loads(request.content)
print(response)
print()
print(response['items'][0]['id'])
The Python script above will output something similar to this:
{
    'kind': 'youtube#channelListResponse', 
    'etag': '0OJX0B6esbO3cdBfycAScSh5cPM', 
    'pageInfo': {
        'totalResults': 1, 
        'resultsPerPage': 5
    },
    'items': [
        {
            'kind': 'youtube#channel', 
            'etag': 'xcfFMDuM69tPJiFWrWlfpwuDcqA', 
            'id': 'UCjFqcJQXGZ6T6sxyFB-5i6A'
        }
    ]
}

UCjFqcJQXGZ6T6sxyFB-5i6A
I don't recommend storing your credentials directly in your scripts. Instead I store my credentials in a .env-file. You can read how I use them here: Using .env-files for storing credentials.

Happy coding!

You might also enjoy

Quick and easy image recognition with 9 lines of code in Python

Quick and easy image recognition with 9 lines of code in Python

Published 2024-05-05 — Updated 2024-07-28

Machine Learning

Python

Need a quick and easy image recognition solution in Python? Learn how to create one in 9 lines of code.

Read the post →
How to easily web scrape any website with Python

How to easily web scrape any website with Python

Published 2024-05-03

Datahoarding

Notes

Python

Web development

Learn how to easily web scrape any website using Python. I go through the various techniques I use.

Read the post →
Python notes

Python notes

Published 2024-05-03 — Updated 2024-05-14

Notes

Python

Different tips, tricks and how-to's while developing various scripts in Python.

Read the post →