Add a playlistItems Resource
Learn about the playlistItems resource and how it is used to identify a resource included in a playlist.
We'll cover the following
The playlistItems
resource is mainly used to manage the content of the playlists that users create. The playlist
resource, on the other hand, only manages the playlist itself. It doesn’t allow its user to add content to it. In order to do that, the user has to use the playlistItems
resource.
In this lesson, we’ll explore the playlistItems
resource, which offers multiple tools, like list
, insert
, update
, and delete
. We’ll interact with a playlist on YouTube through these methods.
Insert a playlist item
In this section, we’ll use the insert
method of the playlistItems
resource to add a playlist item in a YouTube playlist by sending a POST
request to the following URL:
https://www.googleapis.com/youtube/v3/playlistItems
Here are some important parameters that we can use to call the endpoint:
Request parameters
Name | Type | Category | Description |
| String | Required | This parameter serves two functions. It specifies the properties that will be set by the user as well as the properties that will be included in the API response, which are |
| String | Optional | Used to identify that the authenticated user is making changes to the content on behalf of the content owner. |
| String | Required | Used to pass the playlist ID of the content that the user wants to update. |
| String | Required | Used to identify the resource type of the resource used in the API call. |
| String | Required | Used to provide the resource ID that the user wants to add to their playlist. |
Click the “Run” button to add our video to the playlist.
import fetch from 'node-fetch';const endpointUrl = new URL('https://www.googleapis.com/youtube/v3/playlistItems');// Define Header Parameters hereconst headerParameters = {Authorization: 'Bearer {{ACCESS_TOKEN}}',Accept: 'application/json',};const queryParameters = new URLSearchParams({part: 'snippet',});// Define Body Parameters herconst bodyParameters = JSON.stringify({snippet: {playlistId: '{{PLAYLIST_ID}}',resourceId: {kind: 'youtube#video',videoId: '{{VIDEO_ID}}',},},});const options = {method: 'POST',headers: headerParameters,body: bodyParameters,};async function insertPlaylistItem() {try {endpointUrl.search = queryParameters;const response = await fetch(endpointUrl, options);printResponse(response);} catch (error) {printError(error);}}insertPlaylistItem();
Let’s understand how the above code works by breaking it down:
Lines 11–13: We define a variable with the name
queryParameters
in which we set the value of thepart
parameter assnippet
. We do this to indicate that the body provided a snippet object.Lines 16–24: We define a variable with the name
bodyParameters
in which we set the values of the snippet properties.
Some important response fields are as follows:
Response fields
Name | Type | Description |
| String | Reflect the resource type of the API call. |
| ETag | Contains the resource ETag. |
| String | Contains the ID of the playlist item that is being added to the playlist. |
| Object | Contains the thumbnail details of the resource. |
| String | Contains the title of the playlist. |
| String | Contains the ID of the channel. |
Update a playlist items
After inserting a playlist item into a playlist, we can also make changes to that playlist item. For that purpose, we’ll use the update
method of the playlistItems
resource by sending a PUT
request to the following URL:
https://www.googleapis.com/youtube/v3/playlistItems
This endpoint mainly uses the same parameters used for the previous endpoint. We have to provide the id
parameter to specify the playlist item that needs updating.
We need to add a title for our playlist item in the resourceId.title
parameter in the below widget.
Click the “Run” button to add the title of the playlist item.
import fetch from 'node-fetch';const endpointUrl = new URL('https://www.googleapis.com/youtube/v3/playlistItems');// Define Header Parameters hereconst headerParameters = {Authorization: 'Bearer {{ACCESS_TOKEN}}',Accept: 'application/json',};const queryParameters = new URLSearchParams({part: 'snippet',});// Define Body Parameters hereconst bodyParameters = JSON.stringify({id: '{{PLAYLIST_ITEM_ID}}',snippet: {playlistId: '{{PLAYLIST_ID}}',resourceId: {kind: 'youtube#video',videoId: '{{VIDEO_ID}}',title: 'This is a updated title for the playlist item',},},});const options = {method: 'PUT',headers: headerParameters,body: bodyParameters,};async function updatePlaylistItem() {try {endpointUrl.search = queryParameters;const response = await fetch(endpointUrl, options);printResponse(response);} catch (error) {printError(error);}}updatePlaylistItem();
We define an id
parameter to identify the playlist item that we want to update. The method type is PUT
for this endpoint. All of the remaining code is similar to the code used for inserting a playlist item.
In the case of successful execution of the above code, it will mainly return the same response fields that we received in the insert method.
List the playlist items
Now that we’ve updated our playlist items, it’s time for us to verify whether they’ve been updated or not. To do that, we’ll use the list
method of the PlaylistItems
resource. In order to retrieve the playlist items of a playlist, we’ll send the GET
request to the following URL:
https://www.googleapis.com/youtube/v3/playlistItems
Here are some important parameters that we can use to call the endpoint:
Request parameters
Name | Type | Category | Description |
| String | Optional | Used to list all the playlist items of any specific playlist. |
| String | Optional | Used to list the details of a single playlist item defined by its ID. |
Note: In this lesson, we are only listing a specific playlist item. But the
playlistItems
resource also offers multiple listing methods.
Click the “Run” button in the code widget below to retrieve the playlist items.
// Importing libraries hereimport fetch from 'node-fetch';// Define endpoint URL hereconst endpointUrl = new URL('https://www.googleapis.com/youtube/v3/playlistItems');// Define Header Parameters hereconst headerParameters = {Authorization: 'Bearer {{ACCESS_TOKEN}}',Accept: 'application/json',};// Define Query Parameters hereconst queryParameters = new URLSearchParams({part: 'snippet,contentDetails',id: '{{PLAYLIST_ITEM_ID}}',});// Setting API call optionsconst options = {method: 'GET',headers: headerParameters,};// Function to make API callasync function fetchPlaylistItemList() {try {endpointUrl.search = queryParameters;const response = await fetch(endpointUrl, options);// Printing responseprintResponse(response);} catch (error) {// Printing error messageprintError(error);}}// Calling function to make API callfetchPlaylistItemList();
Let’s take a look at the following code explanation of the above code:
Line 5: We define a variable named
endpointUrl
in which we passed the endpoint URL.Lines 14–17: We define a variable named
queryParameters
in which we define theid
parameter, which is used to pass the playlist ID that the user wants to list.
Some of the response fields are as follows:
Response fields
Name | Type | Description |
| String | Reflects the resource type of the API call. |
| ETag | This field contains the resource ETag. |
| String | Contains the ID of the playlist item that is being updated. |
| String | Contains the description of the playlist item. |
| String | Contains the title of the playlist item. |
| String | Contains the ID of the channel. |
| List | Contains the list of the playlist item. |