Update, List, and Delete Videos

Learn how to edit, list, and delete a video using YouTube Data API.

In this lesson, we’ll learn how we can edit and delete a video using the update, list, and delete methods from the videos resource of the YouTube Data API.

Update a video

In the previous lesson, we uploaded a video without a title. Let’s update the video data by modifying its title and description by using the videos resource’s update method. We’ll send a PUT request to the following URL to update the video:

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

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 as well as the properties that will be included in the API response, which are contentDetails, fileDetails, id, liveStreamingDetails, localizations, player, processingDetails, recordingDetails, snippet, statistics, status, suggestions, and topicDetails.

onBehalfOfContentOwner

String

Optional

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

id

String

Required

Used to identify the video in which the user wants to make changes.

snippet.title

String

Optional

Used to provide the title of the video to which the user wants to make changes.

snippet.categoryId

Integer

Optional

Used to identify the video category.

Note: To call this endpoint, the snippet.title and snippet.categoryId parameters are required if the user wants to update the snippet resources.

We need to add a title and description for our video in the snippet.title and snippet.description parameters in the below widget. Click the “Run” button to update the title and description of the video from the previous lesson.

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

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

  • Lines 13–15: We define a queryPatameter variable in which we define the part parameter and set its value to snippet and status.

  • Lines 18–25: We define a bodyParameters in which we define the video ID that the user wants to update and also define the snippet.title parameter in which we defined the video title. After that, we define the snippet.description and snippet.categoryId and update the description and category of the video to 27, which refers to an educational video.

  • Line 33: We define an async function with the name of updateVideo() where we use the fetch() method to make an API call to update the video.

Some of the important response fields are as follows:

Response fields

Name

Type

Description

kind

String

Reflect the resource type of the API call.

etag

ETag

Contains the resource ETag.

id

String

Contains the unique video ID assigned to the video.

snippet

Object

Contains the video details.

status

Object

Contains the status of a video.

List a video

In this part of the lesson, we’ll use the list method of the videos resource to list the updated video by sending the GET request to the following URL:

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

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

Request parameters

Name

Type

Category

Description

part

String

Required

This field defines a comma-separated list of one or more video resource properties that will appear in the API response, which are contentDetails, fileDetails, id, liveStreamingDetails, localizations, player, processingDetails, recordingDetails, snippet, statistics, status, suggestions, and topicDetails.

id

String

Optional

Used to identify the video that the user wants to list.

myRating

String

Optional

Filters all the user-rated videos.

chart

String

Optional

Filters videos based on the user's preferences. For example, mostPopular will provide the most popular YouTube videos.

Note: To call this endpoint, the user must define one value from myRating, id, or chart.   

Click the “Run” button to retrieve the snippet response properties of the video we updated on our YouTube channel.

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/videos');
// Define Header Parameters here
const headerParameters = {
Authorization: 'Bearer {{ACCESS_TOKEN}}',
Accept: 'application/json',
};
// Define Query Parameters here
const queryParameters = new URLSearchParams({
part: 'snippet',
id: '{{VIDEO_ID}}',
});
// Setting API call options
const options = {
method: 'GET',
headers: headerParameters,
};
// Function to make API call
async function listVideo() {
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
listVideo();

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

  • Line 5: We define the endpointUrl variable in which we pass the URL, which will be used to make an API call.

  • Lines 14–17: We define the queryParameters variable in which we define the part and id parameters.

The following table contains some of the important response fields:

Response fields

Name

Type

Description

kind

String

Reflects the resource type of the API call.

etag

ETag

Contains the resource ETag.

nextPageToken

String

Used to fetch the next page in the response result.

prevPageToken

String

Used to fetch the previous page in the response result.

pageInfo

Object

Contains the response page information.

items[]

List

Contains the list of the result that matches the API call requirements.

Delete a video

In this part of the lesson, we’ll use the delete method of the videos resource by sending a DELETE request to the following URL to delete a video from the YouTube channel.

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

Request parameters

The DELETE request requires only one parameter: the video ID of the video the user wants to delete.

Click the “Run” button to delete the video from our YouTube channel.

Press + to interact
// Importing libraries here
import fetch from 'node-fetch';
// Define endpoint URL here
const endpointUrl = new URL('https://youtube.googleapis.com/youtube/v3/videos');
// Define Header Parameters here
const headerParameters = {
Authorization: 'Bearer {{ACCESS_TOKEN}}',
Accept: 'application/json',
};
const queryParameters = new URLSearchParams({
id: '{{VIDEO_ID}}',
});
// Setting API call options
const options = {
method: 'DELETE',
headers: headerParameters,
};
// Function to make API call
async function deleteVideo() {
try {
endpointUrl.search = queryParameters;
const response = await fetch(endpointUrl, options);
// Custom function for printing the API response
printResponse(`${response.status}: ${response.statusText}`);
} catch (error) {
// Custom function for printing the error message
printError(error);
}
}
// Calling function to make API call
deleteVideo();

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

  • Lines 12–14: We define the queryParameters in which we pass the video ID we want to delete.

  • Lines 16–19: We define the options variable in which we defined the HTTP request type we want to use and the header parameters we defined to make an API call.

Response

In the case of successful execution, the above code returns the response code 204.

Note: Because we deleted the previously uploaded video, we won’t have any videos in the channel. So, click the "Run" button in the widget below to upload a video again to the channel for use in the remaining lessons. Save the video ID again so it can be used throughout the course.

Press + to interact
uploadVideo();