Update, List, and Delete Videos
Learn how to edit, list, and delete a video using YouTube Data API.
We'll cover the following
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 |
| 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 user is making changes to the content on behalf of the content owner. |
| String | Required | Used to identify the video in which the user wants to make changes. |
| String | Optional | Used to provide the title of the video to which the user wants to make changes. |
| Integer | Optional | Used to identify the video category. |
Note: To call this endpoint, the
snippet.title
andsnippet.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.
import fetch from 'node-fetch';const endpointUrl = new URL('https://youtube.googleapis.com/youtube/v3/videos',);// Define Header Parameters hereconst headerParameters = {Authorization: 'Bearer {{ACCESS_TOKEN}}',Accept: 'application/json',};const queryParameters = new URLSearchParams({part: 'snippet,status',});// Define Body Parameters hereconst 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 thepart
parameter and set its value tosnippet
andstatus
.Lines 18–25: We define a
bodyParameters
in which we define the video ID that the user wants to update and also define thesnippet.title
parameter in which we defined the video title. After that, we define thesnippet.description
andsnippet.categoryId
and update the description and category of the video to27
, which refers to an educational video.Line 33: We define an
async
function with the name ofupdateVideo()
where we use thefetch()
method to make an API call to update the video.
Some of the 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 unique video ID assigned to the video. |
| Object | Contains the video details. |
| 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 |
| 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 |
| String | Optional | Used to identify the video that the user wants to list. |
| String | Optional | Filters all the user-rated videos. |
| String | Optional | Filters videos based on the user's preferences. For example, |
Note: To call this endpoint, the user must define one value from
myRating
,id
, orchart
.
Click the “Run” button to retrieve the snippet response properties of the video we updated on our YouTube channel.
// Importing libraries hereimport fetch from 'node-fetch';// Define endpoint URL hereconst endpointUrl = new URL('https://www.googleapis.com/youtube/v3/videos');// Define Header Parameters hereconst headerParameters = {Authorization: 'Bearer {{ACCESS_TOKEN}}',Accept: 'application/json',};// Define Query Parameters hereconst queryParameters = new URLSearchParams({part: 'snippet',id: '{{VIDEO_ID}}',});// Setting API call optionsconst options = {method: 'GET',headers: headerParameters,};// Function to make API callasync function listVideo() {try {endpointUrl.search = queryParameters;const response = await fetch(endpointUrl, options);// Printing responseprintResponse(response);} catch (error) {// Printing error messageprintError(error);}}// Calling function to make API calllistVideo();
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 thepart
andid
parameters.
The following table contains some of the important response fields:
Response fields
Name | Type | Description |
| String | Reflects the resource type of the API call. |
| ETag | Contains the resource ETag. |
| String | Used to fetch the next page in the response result. |
| String | Used to fetch the previous page in the response result. |
| Object | Contains the response page information. |
| 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.
// Importing libraries hereimport fetch from 'node-fetch';// Define endpoint URL hereconst endpointUrl = new URL('https://youtube.googleapis.com/youtube/v3/videos');// Define Header Parameters hereconst headerParameters = {Authorization: 'Bearer {{ACCESS_TOKEN}}',Accept: 'application/json',};const queryParameters = new URLSearchParams({id: '{{VIDEO_ID}}',});// Setting API call optionsconst options = {method: 'DELETE',headers: headerParameters,};// Function to make API callasync function deleteVideo() {try {endpointUrl.search = queryParameters;const response = await fetch(endpointUrl, options);// Custom function for printing the API responseprintResponse(`${response.status}: ${response.statusText}`);} catch (error) {// Custom function for printing the error messageprintError(error);}}// Calling function to make API calldeleteVideo();
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 theHTTP
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.
uploadVideo();