YouTube Data API – Search by Keyword

By Faruque Ahamed Mollick

In this post, I am going to show you how to search for videos using YouTube data API by keyword. Basically, we are going to search by keyword using YouTube data API.

Below is the URL that we have to fetch to get a list of videos for a search keyword:

https://www.googleapis.com/youtube/v3/search?part=snippet&q=music+videos&maxResults=12&order=viewCount&key=[YOUR_API_KEY]

The URL will show the JSON formatted data that contains the list of videos for our keyword. We have requested the videos for the keyword via the “q” URL query. Below is the sample JSON data that returned by the query:



 

stdClass Object
(
    [kind] => youtube#searchListResponse
    [etag] => "XI7nbFXulYBIpL0ayR_gDh3eu1k/B1-fjT_iK_dl-OHmouVDfvMkeiQ"
    [nextPageToken] => CAwQAA
    [regionCode] => IN
    [pageInfo] => stdClass Object
        (
            [totalResults] => 1000000
            [resultsPerPage] => 12
        )

    [items] => Array
        (
            [0] => stdClass Object
                (
                    [kind] => youtube#searchResult
                    [etag] => "XI7nbFXulYBIpL0ayR_gDh3eu1k/RLOGPOWMrcSASLkOXWNqJqA_c6I"
                    [id] => stdClass Object
                        (
                            [kind] => youtube#video
                            [videoId] => kJQP7kiw5Fk
                        )

                    [snippet] => stdClass Object
                        (
                            [publishedAt] => 2017-01-13T05:00:02.000Z
                            [channelId] => UCLp8RBhQHu9wSsq62j_Md6A
                            [title] => Luis Fonsi - Despacito ft. Daddy Yankee
                            [description] => Despacito” disponible ya en todas las plataformas digitales: https://UMLE.lnk.to/DOoUzFp Sigue a Luis Fonsi: Official Site: http://www.luisfonsi.com/ Facebook: ...
                            [thumbnails] => stdClass Object
                                (
                                    [default] => stdClass Object
                                        (
                                            [url] => https://i.ytimg.com/vi/kJQP7kiw5Fk/default.jpg
                                            [width] => 120
                                            [height] => 90
                                        )

                                    [medium] => stdClass Object
                                        (
                                            [url] => https://i.ytimg.com/vi/kJQP7kiw5Fk/mqdefault.jpg
                                            [width] => 320
                                            [height] => 180
                                        )

                                    [high] => stdClass Object
                                        (
                                            [url] => https://i.ytimg.com/vi/kJQP7kiw5Fk/hqdefault.jpg
                                            [width] => 480
                                            [height] => 360
                                        )

                                )

                            [channelTitle] => LuisFonsiVEVO
                            [liveBroadcastContent] => none
                        )

                )

            [1] => stdClass Object
                (
                    [kind] => youtube#searchResult
                    [etag] => "XI7nbFXulYBIpL0ayR_gDh3eu1k/dmtYrGCbU2F36zXOMqj03jMDvNM"
                    [id] => stdClass Object
                        (
                            [kind] => youtube#video
                            [videoId] => JGwWNGJdvx8
                        )

                    [snippet] => stdClass Object
                        (
                            [publishedAt] => 2017-01-30T10:57:50.000Z
                            [channelId] => UC0C-w0YjGpqDXGB8IHb662A
                            [title] => Ed Sheeran - Shape of You [Official Video]
                            [description] => Stream or Download Shape Of You: https://atlanti.cr/2singles ÷. Out Now: https://atlanti.cr/yt-album Subscribe to Ed's channel: http://bit.ly/SubscribeToEdSheeran ...
                            [thumbnails] => stdClass Object
                                (
                                    [default] => stdClass Object
                                        (
                                            [url] => https://i.ytimg.com/vi/JGwWNGJdvx8/default.jpg
                                            [width] => 120
                                            [height] => 90
                                        )

                                    [medium] => stdClass Object
                                        (
                                            [url] => https://i.ytimg.com/vi/JGwWNGJdvx8/mqdefault.jpg
                                            [width] => 320
                                            [height] => 180
                                        )

                                    [high] => stdClass Object
                                        (
                                            [url] => https://i.ytimg.com/vi/JGwWNGJdvx8/hqdefault.jpg
                                            [width] => 480
                                            [height] => 360
                                        )

                                )

                            [channelTitle] => Ed Sheeran
                            [liveBroadcastContent] => none
                        )

                )

            [2] => stdClass Object
                (
                    [kind] => youtube#searchResult
                    [etag] => "XI7nbFXulYBIpL0ayR_gDh3eu1k/V9MFr5FSLw-jmpgxWMTEJw4xjUI"
                    [id] => stdClass Object
                        (
                            [kind] => youtube#video
                            [videoId] => RgKAFK5djSk
                        )

                    [snippet] => stdClass Object
                        (
                            [publishedAt] => 2015-04-07T03:00:03.000Z
                            [channelId] => UCVp3nfGRxmMadNDuVbJSk8A
                            [title] => Wiz Khalifa - See You Again ft. Charlie Puth [Official Video] Furious 7 Soundtrack
                            [description] => The Fate of the Furious: The Album available now - https://atlantic.lnk.to/f8 Download the new Furious 7 Soundtrack Deluxe Version on iTunes here: ...
                            [thumbnails] => stdClass Object
                                (
                                    [default] => stdClass Object
                                        (
                                            [url] => https://i.ytimg.com/vi/RgKAFK5djSk/default.jpg
                                            [width] => 120
                                            [height] => 90
                                        )

                                    [medium] => stdClass Object
                                        (
                                            [url] => https://i.ytimg.com/vi/RgKAFK5djSk/mqdefault.jpg
                                            [width] => 320
                                            [height] => 180
                                        )

                                    [high] => stdClass Object
                                        (
                                            [url] => https://i.ytimg.com/vi/RgKAFK5djSk/hqdefault.jpg
                                            [width] => 480
                                            [height] => 360
                                        )

                                )

                            [channelTitle] => Wiz Khalifa
                            [liveBroadcastContent] => none
                        )

                )
.
.
.
.
          [11] => stdClass Object
                (
                    [kind] => youtube#searchResult
                    [etag] => "XI7nbFXulYBIpL0ayR_gDh3eu1k/pFRLYfPnipmkwnIJ1mWP7pFm83o"
                    [id] => stdClass Object
                        (
                            [kind] => youtube#video
                            [videoId] => 6Mgqbai3fKo
                        )

                    [snippet] => stdClass Object
                        (
                            [publishedAt] => 2016-11-18T14:00:01.000Z
                            [channelId] => UCGnjeahCJW1AF34HBmQTJ-Q
                            [title] => Shakira - Chantaje ft. Maluma
                            [description] => Shakira feat. Maluma - "Chantaje" (Official Music Video) "El Dorado" is available now! iTunes: http://smarturl.it/ElDoradoi Apple Music: ...
                            [thumbnails] => stdClass Object
                                (
                                    [default] => stdClass Object
                                        (
                                            [url] => https://i.ytimg.com/vi/6Mgqbai3fKo/default.jpg
                                            [width] => 120
                                            [height] => 90
                                        )

                                    [medium] => stdClass Object
                                        (
                                            [url] => https://i.ytimg.com/vi/6Mgqbai3fKo/mqdefault.jpg
                                            [width] => 320
                                            [height] => 180
                                        )

                                    [high] => stdClass Object
                                        (
                                            [url] => https://i.ytimg.com/vi/6Mgqbai3fKo/hqdefault.jpg
                                            [width] => 480
                                            [height] => 360
                                        )

                                )

                            [channelTitle] => shakiraVEVO
                            [liveBroadcastContent] => none
                        )

                )

        )

)

 

Now we can get each video and YouTube video page URL.

Let’s see each URL parameter that we have in the YouTube data API URL for search videos:

part: Identifies groups of properties that should be returned for a resource. Here we have used the snippet that returns video title, video description, thumbnails etc. There are other different types of the part parameter provided by YouTube data API like contentDetails, statistics, status etc.

q: The keyword for which the search results will return.

maxResults: It shows M=maximum results per request or per page. In the URL, we have set the maximum results to 12. That means it will return the maximum 12 results for a specific keyword.

key: It contains your YouTube data API key.

 

From the JSON formatted data return by the URL contains several things like video ID, video title, description, three types of thumbnail image (default, medium and high quality) and so on. Even you can send different parameters to the URL to get more.

 

Leave a Reply

Your email address will not be published. Required fields are marked *