Search Resources by Keyword
Learn about the search resource and how it can retrieve information based on certain search parameters.
We'll cover the following
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.
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 |
| String | Required | Used to specify the comma-separated list of resource properties |
| Boolean | Optional | Used to filter those videos that are owned by the user. To use this parameter, the user must set the |
| Boolean | Optional | Used to restrict fetches so that data is only uploaded by the developer application. |
| Boolean | Optional | Used to restrict fetches so that data is only uploaded by the authenticated user. |
| String | Optional | Used to restrict fetches so that it only returns videos that are related to the parameter query value. |
| String | Optional | Used to add a keyword to the search from YouTube. |
| Integer | Optional | Used to fix the number of results that will be fetched. |
| 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
, orrelatedToVideoId
.
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.
// Importing libraries hereimport fetch from 'node-fetch';// Define endpoint URL hereconst endpointUrl = new URL('https://www.googleapis.com/youtube/v3/search');// Define Header Parameters hereconst headerParameters = {Authorization: 'Bearer {{ACCESS_TOKEN}}',Accept: 'application/json',};// Define Query Parameters hereconst queryParameters = new URLSearchParams({part: 'snippet',maxResults: 25,q: '{{KEYWORD}}',});// Setting API call optionsconst options = {method: 'GET',headers: headerParameters,};// Function to make API callasync function searchQuery() {try {endpointUrl.search = queryParameters;const response = await fetch(endpointUrl, options);// Printing responseprintResponse(response);} catch (error) {// Printing error messageprintError(error);}}// Calling function to make API callsearchQuery();
Let’s take a look at the following code explanation:
Lines 14–18: We declare a
queryParameters
variable in which we define theq
parameter and pass the keyword we want to search.Lines 27–38: We define an
async
function with the name ofsearchQuery
in which we use thefetch()
method to make calls to the endpoint.
Response fields
Name | Type | Description |
| String | Reflects the resource type of the API call. |
| ETag | Contains the resource ETag. |
| String | Contains the ID of the resource, which matches the query parameters. |
| Object | Contains some important details about the search result. |
| List | Contains the list of responses that matches the search query requirement. |