Benni
Benni

Reputation: 654

How to get the title of a playlist from the YouTube API

I would like to get the title of the playlist from the YouTube API, but I don't know how to do it.

I am already able to get the title of each video in the playlist, but I also wanna get the title of the YT playlist.

This is how I'm currently calling the YT Api:

const baseApiUrl = "https://www.googleapis.com/youtube/v3";
const response = await axios.get(`${baseApiUrl}/playlistItems`, {
            params: {
                part: "snippet",
                maxResults: "50",
                playlistId: playlistId,
                key: apiKey,
                pageToken: nextPageToken,
            },
});

I've also tried to find a solution with the help of their documentation (https://developers.google.com/youtube/v3/docs/playlists/list) but I wasn't able to find anything.

Upvotes: 0

Views: 3805

Answers (1)

stvar
stvar

Reputation: 6955

For to obtain the title of a playlist given by its ID, you'll have to invoke the Playlists.list API endpoint queried with the request parameter id set to the respective playlist's ID.

For example, see this playlist that belongs to the well-known news and information programme DW News, playlist which is subsumed to Brexit issues: Brexit News.

Querying the API with an URL of form:

https://www.googleapis.com/youtube/v3/playlists?key=$YOUTUBE_DATA_APP_KEY&id=PLT6yxVwBEbi22rbp2Mve4yEJVpSJFcC9g&part=id,snippet&fields=items(id,snippet(title,channelId,channelTitle))

will produce the following JSON result:

{
  "items": [
    {
      "id": "PLT6yxVwBEbi22rbp2Mve4yEJVpSJFcC9g",
      "snippet": {
        "channelId": "UCknLrEdhRCp1aegoMqRaCZg",
        "title": "Brexit News",
        "channelTitle": "DW News"
      }
    }
  ]
}

Note that above I used the fields request parameter for to get from the API a reduced set of info of the whole playlist metadata available.

Upvotes: 2

Related Questions