Search Resources by Keyword

Learn about the search resource and how it can retrieve information based on certain search parameters.

The search resource is used to perform search operations on YouTube data. It’s mainly used to filter data from YouTube based on the user’s query, such as videos, channels, and playlists.

Press + to interact

Search by keyword

The YouTube Data API allows us to search data from YouTube by using the following endpoint:

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

Request parameters

Some important request parameters to call the endpoint are as follows:

Name

Type

Category

Description

part

String

Required

Used to specify the comma-separated list of resource properties

forContentOwner

Boolean

Optional

Used to filter those videos that are owned by the user. To use this parameter, the user must set the onBehalfOfContentOwner parameter.

forDeveloper

Boolean

Optional

Used to restrict fetches so that data is only uploaded by the developer application.

forMine

Boolean

Optional

Used to restrict fetches so that data is only uploaded by the authenticated user.

relatedToVideoId

String

Optional

Used to restrict fetches so that it only returns videos that are related to the parameter query value.

q

String

Optional

Used to add a keyword to the search from YouTube.

maxResults

Integer

Optional

Used to fix the number of results that will be fetched.

onBehalfOfContentOwner

String

Optional

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

Note: To call this endpoint, the user can define one parameter from forContentOwner, forDeveloper, forMine, or relatedToVideoId.

Click the “Edit” button in the following widget, enter the keyword that you want to search in the KEYWORD field, and click the “Save” button. After saving the keyword, click the “Run” button to retrieve the search result.

Note: In this lesson, we are only implementing the search by using the query parameter method, but we can also implement the search by using different field parameters.

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

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

  • Lines 14–18: We declare a queryParameters variable in which we define the q parameter and pass the keyword we want to search.

  • Lines 27–38: We define an async function with the name of searchQuery in which we use the fetch() method to make calls to the endpoint.

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 resource, which matches the query parameters.

snippet

Object

Contains some important details about the search result.

items[]

List

Contains the list of responses that matches the search query requirement.