PlaylistItems

Learn about the playlistItem resource and how it is used to identify a resource included in a playlist.

The playlist resource contains information about the playlist itself, but not its contents. These are stored in the playlistItem resource, which includes details about the playlist item itself and additional information about how the video appears in the playlist, such as its position in the list.

Supported methods

The API supports the following methods for the playlistItem resource:

  • list: Returns a list of playlist items specified in the API call that matches the request parameters.
  • insert: Adds a resource to a playlist.
  • delete: Deletes a playlist item.

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

The list method

It returns a collection of playlist items. The playlist items are retrieved using the playlistId parameter, which specifies the playlist ID. In the following example, we’ll retrieve the list of videos in a playlist owned by the Educative Sessions YouTube channel.

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

Run the following code to get a response with the list of videos in the given playlist.

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.playlistItems().list(
part="snippet,contentDetails",
maxResults=25,
playlistId="PL5Pdj6odxsXTpqCSiP8GLtL4pvak29Fxy",
fields="kind,items(kind,id,snippet(channelId,title,description,channelTitle,playlistId,resourceId),contentDetails)"
)
response = request.execute()

In the response object, we get the ID of the playlist item, its title, description, and channel title in the snippet object. In the contentDetails object, we have the ID of the video.

You can replace the playlist ID in the code above with another playlist ID to retrieve the list of videos in that playlist.

Note: For the following methods, it is assumed that you have a playlist created in your channel. If not, you can go ahead and create one! You can do so manually or create one using the insert method discussed in the previous lesson.

The insert method

It adds a new resource to a playlist. Let’s see this method in action with the help of an example. Here, we will add a video from the Educative Sessions YouTube channel to the specified playlist. To execute this API request, you need to add the playlist ID (one from your account) and the video ID in the request parameters.

Note: Make sure you have a playlist already created in your channel for this example to work. Replace the playlist ID in the API key box below with your own.

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.

Note: Make sure that you’re signed in using the Google account which was used to set up the project in the API Console.

You will 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”.

Once you’re done, you’ll see the API response object in the terminal.

{{CLIENT_SECRET}}
Adding a video resource to a playlist

Note: In the response, you’ll get the ID of the playlist item. It will be the value of the id key as shown in the image below:



Copy and save this ID because we’ll need it in the next section.

When the video is added to the specified playlist, you’ll get a JSON response with information about the playlist item. It will contain the ID of the playlist item, along with a snippet.

The delete method

This method deletes the specified playlist item. We’ll use the id parameter to specify the ID of the playlist item to be deleted.

Note: We saved the playlist item ID in the previous section. Now you can go ahead and paste it in the PLAYLIST_ITEM_ID field in the API key box below.

Run the code and follow the steps to delete the specified playlist item.

{{CLIENT_SECRET}}
Deleting a playlist item

Once you’ve received the deletion message in the terminal, head over to your channel to verify that the specified video has been deleted.