Playlists

Learn about the playlist resource and how it is used to retrieve or modify information about a YouTube playlist.

Playlists on YouTube are collections of videos that we can view successively. These playlists can be created manually by users or automatically by YouTube to identify different kinds of videos on a particular channel, such as lists of liked or uploaded videos. The information about these playlists is stored in the playlist resource.

Supported methods

The API supports the following methods for the playlist resource:

  • list: Returns a list of playlists specified in the API call that matches the request parameters. For example, we can retrieve all the playlists owned by a YouTube channel.
  • insert: Creates a new playlist.
  • update: Modifies a playlist by changing its title or description.
  • delete: Deletes a playlist.

Let’s look at each method in detail with the help of a few examples.

The list method

We can use the list method to retrieve a list of playlists owned by a particular YouTube channel, specified by the value of the channelId parameter in the API call. In the following example, we’ll retrieve the playlists owned by the Educative Sessions YouTube channel.

Note: We’ll retrieve the snippet and contentDetails parts for this method. We will also filter the API response using the fields parameter.

Let’s run the following code to get a response with the list of playlists owned by the specified channel.

Press + to interact
scopes = ["https://www.googleapis.com/auth/youtube.readonly"]
api_service_name = "youtube"
api_version = "v3"
DEVELOPER_KEY = "{{API_KEY}}"
youtube = googleapiclient.discovery.build(
api_service_name, api_version, developerKey = DEVELOPER_KEY)
request = youtube.playlists().list(
part="snippet,contentDetails,status",
channelId="UCT_8FqzTIr2Q1BOtvX_DPPw", # this is the channelId of "Educative Sessions" YouTube channel
maxResults={{MAX_RESULTS}},
fields="kind,items(id,snippet(channelId,title,description,channelTitle),contentDetails)"
)
response = request.execute()

In the response object, we get the playlist ID, title, description, and channel title in the snippet object. In the contentDetails object, we have the itemCount, which is the number of videos in that particular playlist.

You can replace the channel ID in the code above with your channel’s ID to retrieve the list of playlists on your channel instead.

Note: For the following methods, it is assumed that you do not have a playlist in your channel.

The insert method

We can make a new playlist in our channel using the insert method. By specifying the required snippet.title parameter and optionally the other parameters such as snippet.status and snippet.description in the request body, a playlist is created with the specified details.

Let’s see this method in action with the help of an example. Here, we’ll create a playlist with its privacy status set to public. We’ll set its title and description, and also set some tags for this playlist.

Note: On line 20 in the code below, you can change the default playlist title with something of your choice.

After you click the “Run” button, you’ll see a prompt in the terminal that says “Please visit this URL to authorize this application.” Copy this URL and paste it into a browser tab.

You’ll be asked to permit the API request by giving some permissions. Finally, copy the generated code and paste it into the terminal next to the prompt “Enter the authorization code.”

Finally, you’ll see the API response object in the terminal.

{{CLIENT_SECRET}}
Creating a playlist in the user's channel

When the playlist is created, you will get a JSON response with information about the playlist. It will contain the ID of the playlist, along with a snippet and status object. Go to the YouTube Library and scroll to the “Playlists" section to see the newly created playlist.

The update method

As the name suggests, the update method modifies the specified playlist. We will require the id parameter to specify the ID of the playlist and snippet.title to specify the playlist’s title. These two properties are required, and all other properties are optional.

Note: We’ll update the playlist that we created in the previous section, but we need the playlist ID to update it. Look at the steps in this lesson if you need to find the playlist ID.

Paste the playlist ID in the PLAYLIST_ID field in the API key box that appears above the code below.

On line 21 in the code below, you can replace the default playlist title with something of your choice.

Let’s run the code and follow the steps to update the playlist.

{{CLIENT_SECRET}}
Updating a playlist

When the playlist is updated, we’ll get a JSON response with updated information about the playlist. The snippet object will show the updated title and description of the playlist. Go to YouTube Library and scroll to the “Playlists” section to see the playlist with an updated name.

The delete method

The delete method for playlists deletes the specified playlist. We’ll use the id parameter to specify the ID of the playlist that is to be deleted.

Note: We’ll delete the playlist that we updated in the previous section. The API key box should have the PLAYLIST_ID that we saved in the previous section. If not, then follow these steps and paste the playlist ID below.

Let’s run the code and follow the steps to delete the specified playlist.

{{CLIENT_SECRET}}
Deleting a playlist

Once you’ve received the deletion message in the terminal, go to your channel to verify that the playlist has been deleted.