Upload Videos

Learn how to upload a video and set a thumbnail using the YouTube Data API.

The videos resource is used to perform multiple operations on a YouTube video. In this lesson, we’ll learn two important endpoints for the videos resource. In addition, we’ll learn how to upload a video and add a thumbnail to a video using the YouTube Data API.

Upload a video

In this section, we’ll upload the video on our YouTube channel using the insert method of the videos resource. To upload a video, we’ll send a POST request to the following URL:

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

Request parameters

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

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.

notifySubscribers

Boolean

Optional

Allows YouTube to send a notification to the channel’s subscribers.

onBehalfOfContentOwner

String

Optional

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

Click the “Run” button to upload a video to the YouTube channel.

Note: We have already uploaded a video, Welcome_to_Educative.mp4, to our platform. We can use it to run the insert method.

Press + to interact
import fetch from 'node-fetch';
import fs from 'fs';
const endpointUrl = new URL('https://www.googleapis.com/upload/youtube/v3/videos');
// Define Header Parameters here
const headerParameters = {
Authorization: 'Bearer {{ACCESS_TOKEN}}',
Accept: 'application/octet-stream',
};
// Define Body Parameters here
const videoPath = fs.readFileSync('Welcome_to_Educative.mp4');
const options = {
method: 'POST',
headers: headerParameters,
body: videoPath,
};
// Function to make API call
async function uploadVideo() {
try {
const response = await fetch(endpointUrl, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
uploadVideo();

Note: The code extracts an ID of the video. Click the "Save" button to save the ID so that it can be used later.

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

  • Line 2: We import the fs file system module to read the video file.

  • Line 4: We specify the endpoint URL for the endpoint.

  • Lines 7–10: We define a variable with the name of headerParameters in which we defined the required headers.

  • Line 13: We define a videoPath variable where we give the path of the video that we uploaded to our platform.

Note: The video title is set to "unknown" because we didn't set it in our API call. Don’t worry; we’ll update it in the next lesson.

Response fields

The following table contains some of the most important 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, like title, description, tags, category, and so on.

Set a thumbnail

In this part of the lesson, we’ll use the set method of the thumbnails resource. In order to set a thumbnail on the video, we’ll send a POST request to the following URL:

https://www.googleapis.com/upload/youtube/v3/thumbnails/set

Note: The phone number verification is only required for the thumbnails endpoint, So if you want to use this endpoint, please visit the Verify Phone Number lesson in the Appendix.

Request parameters

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

Name

Type

Category

Description

videoId

String

Required

Represents the video ID against which we will update a thumbnail.

onBehalfOfContentOwner

String

Optional

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

Click the “Run” button to set a thumbnail of the video.

Press + to interact
import fetch from 'node-fetch';
import fs from 'fs';
const endpointUrl = new URL('https://www.googleapis.com/upload/youtube/v3/thumbnails/set');
// Define Header Parameters here
const headerParameters = {
Authorization: 'Bearer {{ACCESS_TOKEN}}',
Accept: 'application/octet-stream',
};
const queryParameters = new URLSearchParams({
videoId: '{{VIDEO_ID}}',
});
const thumbnailPath = fs.readFileSync('Educative-Thumbnail.png');
const options = {
method: 'POST',
headers: headerParameters,
body: thumbnailPath,
};
async function uploadVideoBanner() {
try {
endpointUrl.search = queryParameters;
const response = await fetch(endpointUrl, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
uploadVideoBanner();

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

  • Line 2: We import the fs file system module to read the video file.

  • Line 4: We specify the endpoint URL for the endpoint.

  • Lines 11–13: We define a variable named queryParameters in which we defined the video ID, against which a thumbnail will be uploaded.

  • Line 15: We define a thumbnailPath variable to read the thumbnail Educative-Thumbnail.png in order to upload that thumbnail to the video.

Response fields

In the response field of this endpoint, we’ll primarily receive five objects, which are default, medium, high, standard, and maxres. These will include the following information:

Name

Type

Description

url

String

Contains the thumbnail URL.

width

Integer

Contains the width of the image.

height

Integer

Contains the height of the image.