My own blog posts about development, tech, finance and other (interesting) stuff.
2021-06-15, Dev, YouTube, API, Channel
YouTube API - Get All Channel Videos
You might think that it's a simple thing to get all the videos from a channel with the YouTube API? It is, if you understand the YouTube data structure and know how to do it. The YouTube API has a /channel endpoint that gets you all the infos about a channel, but not the videos of it.
You could use the /search/list endpoint with channelId-param to get the videos of a channel. But that costs you a lot of quota, this endpoint has a quota cost of 100 units. Usually the endpoints have a cost of 1 unit.
A better way to get the channels videos is to use the /playlistItems endpoint with the right playlistId. It has a quote cost of 1 unit and you could set the maxResults-param to 50 items.
The tricky thing is to get the right playlistId. You have channels with lots of playlist and you have channels with zero playlist. How could you get the videos of this channels? You have to know that every YouTube channel has at least one default playlist called "Uploads". You don't see it on the playlist tab, but you could find it on the videos-tab or on the channels overwiew-tab as dropdown or as a section "Uploads".
To get the id of the uploads-playlist you could always use the API-endpoint /playlists or you click on the "Play all"-button right beside the "Uploads"-section-header. You get to an url that looks like ...
The list-param is your playlist-id, it always starts with UU and is usually the same id as the channel itself with the exception that the channel-id is starting with UC. So if you find the channel id you just have to change the C on the second position to an U and you have the uploads-playlist.
From here it's simple to fetch all the videos with a call to the playlistItems endpoint ...
You get everything you need for a listing, videoId, title, thumbnails, ... if you need more infos about the videos (eg. statistics) you have to fetch it with the /videos endpoint and provide all the ids comma-seperated as id-param ...
That makes it easy to fetch, but cost you the same api quota as if you would do it one by one.