Search

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

What is a search resource?

The search resource contains information about a YouTube video, channel, or playlist that matches the search parameters, such as the number of subscribers, view count, video count, channel name, or description.

Supported methods

The API supports only the list method for this resource. It returns a list of search results specified in the API call that match the search parameters. We can retrieve the search results based on a keyword or a geographic location. The method also allows us to retrieve results from the YouTube account of the user authorizing the API request.

Let’s use the list method to retrieve data with the help of a few examples.

Example 1: Retrieve search results from a keyword

In this example, we’ll retrieve the first 25 search results that are associated with the keyword “educative.” For demonstration, we’ll only retrieve the snippet part.

We’ll require a keyword to retrieve results associated with it. You can replace the parameter q (at line 11) in the code below. To do so, click the “Edit” button in the following widget, enter the keyword in the KEYWORD field and click the “Save” button.

Note: By default, the KEYWORD has the value educative. You can change it by following the steps discussed above. Let’s also filter the results using the fields parameter. We’ll only retrieve the kind of the response and some nested properties in the snippet object.

Let’s run the following code to get a filtered response with the search results associated with a keyword, along with the snippet parts.

Press + to interact
api_service_name = "youtube"
api_version = "v3"
DEVELOPER_KEY = "{{API_KEY}}"
youtube = googleapiclient.discovery.build(
api_service_name, api_version, developerKey = DEVELOPER_KEY)
request = youtube.search().list(
part="snippet",
maxResults=25,
q="{{KEYWORD}}",
fields="{{FIELDS}}"
)
response = request.execute()

The response will have either videos, channels, or even playlists as search results. You can identify the type of result by looking at the kind of the result.

Example 2: Retrieve videos of an authenticated user

In this example, we’ll search for videos within the authorized user’s videos that match a particular keyword.

Note: We set the forMine parameter to true to search within an authorized user’s videos. The type parameter is also set to video in order to retrieve only the video resource. Finally, we’ll filter the API response by setting the fields parameter.

Remember that we created an OAuth 2.0 client ID and saved the JSON object of the client_secret_CLIENTID.json file. We’ll use that in this example.

Note: You will not see any item in the response if you have not uploaded any videos associated with the keyword.

After clicking the “Run” button, you’ll see a prompt in the terminal that says “Please visit this URL to authorize this application.” Copy this URL and paste it into a browser tab.

Note: Make sure that you are signed in using the Google account which was used to set up the project in the API Console.

You’ll be asked to permit the API request by giving some permissions. Finally, copy the generated code and paste it into the terminal next to the prompt “Enter the authorization code.”

Finally, you’ll see the API response object in the terminal.

{{CLIENT_SECRET}}
Retrieving videos from the authorized user’s uploaded videos