Create a Playlist

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

Playlists on YouTube are collections of videos. We can manually create a playlist, or YouTube can create one automatically by identifying different types of videos on a specific channel, such as lists of liked or uploaded videos. The playlists resource stores information about these playlists.

In this lesson, we’ll explore the playlists resource, which offers multiple methods like list, insert, update, and delete, with which we can interact with playlists on YouTube.

Create a playlist

Within the Playlists resource, we'll use the insert method to create a playlist to YouTube by sending a POST request to the following URL:

https://www.googleapis.com/youtube/v3/playlists

Here are some important parameters that we can use to call the endpoint:

Request parameters

Name

Type

Category

Description

part

String

Required

This parameter serves two functions. It specifies the properties that will be set by the user and the properties that will be included in the API response. These are contentDetails, id, localizations, player, snippet, and status.

onBehalfOfContentOwner

String

Optional

Used to identify that the authenticated user is making changes to the content on behalf of the content owner.

snippet.title

String

Required

Used to pass the title the user wants to assign to the playlist.

snippet.description

String

Optional

Used to pass the description the user wants to assign to the playlist.

We need to add a title and description for our playlist in the snippet.title and snippet.description fields in the below widget. Click the “Run” button to add the title and description of the playlist.

Press + to interact
import fetch from 'node-fetch';
const endpointUrl = new URL('https://www.googleapis.com/youtube/v3/playlists');
// Define Header Parameters here
const headerParameters = {
Authorization: 'Bearer {{ACCESS_TOKEN}}',
Accept: 'application/json',
};
const queryParameters = new URLSearchParams({
part: 'snippet',
});
// Define Body Parameters her
const bodyParameters = JSON.stringify({
snippet: {
channelId: '{{CHANNEL_ID}}',
title: 'This is a sample title for a playlist',
description: 'This is a sample description for a playlist',
},
});
const options = {
method: 'POST',
headers: headerParameters,
body: bodyParameters,
};
async function createPlaylist() {
try {
endpointUrl.search = queryParameters;
const response = await fetch(endpointUrl, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
createPlaylist();

Let’s understand how the above code works by breaking it down:

  • Lines 11–13: We define a variable with the name of queryParameters in which we define the part parameter as a query parameter.

  • Lines 16–22: We define a variable with the name of bodyParameters in which we set the values of the channelId, title, and description parameters.

In case of the successful execution of the above code, it will return details about the playlist. Some of the important response fields are explained below:

Response fields

Name

Type

Description

kind

String

Reflects the resource type of the API call.

etag

ETag

Contains the resource ETag.

id

String

Contains the ID of the playlist, which is being updated.

snippet.description

String

Contains the description of the playlist.

snippet.title

String

Contains the title of the playlist.

channelTitle

String

Contains the title of the channel.

Update a playlist

To update a playlist, we’ll use the update method of the playlists resource by sending a PUT request on the following endpoint URL:

https://www.googleapis.com/youtube/v3/playlists

This endpoint mainly uses the same parameters used for the previous endpoint. We just have to provide the id parameter to specify the playlist that needs to be updated.

Let’s update our playlist’s title and description in the widget below. Click the “Run” button to update the playlist.

Press + to interact
import fetch from 'node-fetch';
const endpointUrl = new URL('https://www.googleapis.com/youtube/v3/playlists');
// Define Header Parameters here
const headerParameters = {
Authorization: 'Bearer {{ACCESS_TOKEN}}',
Accept: 'application/json',
};
const queryParameters = new URLSearchParams({
part: 'snippet',
});
// Define Body Parameters her
const bodyParameters = JSON.stringify({
id: '{{PLAYLIST_ID}}',
snippet: {
channelId: '{{CHANNEL_ID}}',
title: 'This is a updated title for the playlist',
description: 'This is a updated description for the playlist',
},
});
const options = {
method: 'PUT',
headers: headerParameters,
body: bodyParameters,
};
async function updatePlaylist() {
try {
endpointUrl.search = queryParameters;
const response = await fetch(endpointUrl, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
updatePlaylist();

We define an id parameter to identify the playlist that we want to update. For this endpoint, the HTTP request type is PUT. All of the remaining code is similar to the code for inserting a playlist. 

In case of the successful execution of the above code, it will return the same details about the playlist that we received in the insert method.

List the playlist

Now that we’ve updated our playlist, it’s time for us to verify whether it’s been updated or not. To do this, we’ll use the list method of the Playlists function. In order to retrieve the user playlist, we’ll send the GET request to the following URL:

https://www.googleapis.com/youtube/v3/playlists

Here are some important parameters that we can use to call the endpoint:

Request parameters

Name

Type

Category

Description

channelId

String

Optional

Used to filter the playlist of a specific channel.

id

String

Optional

Used to pass the playlist IDs that the user wants to list.

mine

String

Boolean

Used if the user wants to retrieve their own playlists.

Click the “Run” button in the code widget below to retrieve the playlist.

Note: This method can be used in multiple ways, but in this section, we only use the list method to fetch user playlists. To use this endpoint, a user must provide one of the parameters from channelId, id, or mine.

Press + to interact
// Importing libraries here
import fetch from 'node-fetch';
// Define endpoint URL here
const endpointUrl = new URL('https://www.googleapis.com/youtube/v3/playlists');
// Define Header Parameters here
const headerParameters = {
Authorization: 'Bearer {{ACCESS_TOKEN}}',
Accept: 'application/json',
};
// Define Query Parameters here
const queryParameters = new URLSearchParams({
part: 'snippet,contentDetails,status',
id: '{{PLAYLIST_ID}}',
});
// Setting API call options
const options = {
method: 'GET',
headers: headerParameters,
};
// Function to make API call
async function getPlaylist() {
try {
endpointUrl.search = queryParameters;
const response = await fetch(endpointUrl, options);
// Printing response
printResponse(response);
} catch (error) {
// Printing error message
printError(error);
}
}
// Calling function to make API call
getPlaylist();

Let’s take a look at the following code explanation for the above code:

  • Line 5: We define a variable named endpointUrl in which we pass the endpoint URL.

  • Lines 14–17: We define a variable named queryParameters in which we define the playlist ID.

Some of the response fields are as follows:

Response fields

Name

Type

Description

id

String

Contains the ID of the playlist that is being updated.

items[]

List

Contains the list of the playlist.

snippet.description

String

Contains the description of the playlist.

snippet.title

String

Contains the title of the playlist.

snippet.channelId

String

Contains the ID of the channel.