Find Videos with the YouTube Search API

Published in: JavaScript - YouTube

The instant search feature at Zero Dollar Movies uses the YouTube data API to find free movies on the YouTube website.

When the user enters a search query, the script makes a request to the (YouTube data API) for search results. The resultset is returned in JSON format. The code uses YouTube API v2 and, while optional, it may be a good idea to include a developer key in the API requests.

<input id="searchquery" />
<div id="results"></div>

<!-- Include the latest jQuery library -->
<script src=""></script>

<script type="text/javascript">
jQuery(document).ready(function ($) {

  $('#searchquery').keyup(function () {

    // the search term
    var q = $('#searchquery').val().trim();

    // container to display search results
    var $results = $('#results');

    // YouTube Data API base URL (JSON response)
    var url = ""

    // set paid-content as false to hide movie rentals
    url = url + '&paid-content=false';

    // set duration as long to filter partial uploads
    url = url + '&duration=long';

    // order search results by view count
    url = url + '&orderby=viewCount';

    // we can request a maximum of 50 search results in a batch
    url = url + '&max-results=50';

    $.getJSON(url + "&q=" + q, function (json) {

      var count = 0;

      if ( {

        var items =;
        var html = "";

        items.forEach(function (item) {

          // Check the duration of the video,
          // full-length movies are generally longer than 1 hour
          var duration = Math.round((item.duration) / (60 * 60));

          // Filter out videos that aren't in the Film or Movies category
          if ((duration > 1) && (item.category == "Movies" || item.category == "Film")) {

            // Include the YouTube Watch URL
            html += '<p><a href="' + + '">';

            // Add the default video thumbnail (default quality)
            html += '<img src="' + + '/default.jpg">';

            // Add the video title and the duration
            html += '<h2>' + item.title + ' ' + item.duration + '</h2></a></p>';

      // Did YouTube return any search results?
      if (count === 0) {
        $results.html("No videos found");
      } else {
        // Display the YouTube search results

Generate a YouTube Developer Key

You can go to the Google API console to create a developer key for your project. Go to and start a new project. Give your project a unique name and then choose APIs to turn on the YouTube Data API. Next create a new key under Public API access, set the type as Browser key and the website referrer as your website address (to prevent abuse).

YouTube Data API Quota Limits

YouTube Data API v3’s quota is 50,000,000 units per day. The quota cost of making a single search request to YouTube is just 2 units and thus a normal web application is unlikely to exceed the quota anytime soon.

You can also enable billing the Google Cloud console to further increase your quota.

Published in: JavaScript - YouTube

Looking for something? Find here!

Meet the Author

Web Geek, Tech Columnist
Amit Agarwal

Amit Agarwal is a Google Developer Expert in GSuite and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. Read more on Lifehacker and YourStory

Get in touch