File Under: APIs, Multimedia

YouTube Tutorial Lesson 2 – The Data API

In the first lesson of this tutorial, we looked at the YouTube Player API, a handy tool which allows you to customize, skin and otherwise control the playback of YouTube videos you embed on your website.

If you’re just joining us, or if you’re looking for instructions on how to customize the playback of your embedded videos, then we’d suggest you have a look at Lesson 1.

For those who want to dig deeper, welcome! It’s time to explore the YouTube Data API, which you can use to request and store info about movies you’d like to display on your site. You can pull in not only the videos themselves from YouTube, but also important metadata like the video title, tags, description, duration and so on.


  1. Pick a Library
  2. Get Started
  3. Working with the YouTube Data API
  4. Getting a List of Your Uploaded Videos
  5. Conclusion

Pick a Library

A variety of client libraries are available for the YouTube Data API, including:

We’ll be using Python for this tutorial. If you’re a PHP whiz or an old-school Java-head, you needn’t worry — the general concepts we’re going to cover will be the same no matter which language you use.

Get Started

Let’s say you frequently post movies to YouTube and you’re tired of cutting and pasting the embed code to get them to show up on your site.

Using the YouTube Data API and some quick Python scripts, we can grab all our movies — along with the metadata associated with the clips — and automatically add them our database. For instance, if you followed along with Webmonkey’s Django tutorial series, this technique would be a handy way to add YouTube to your list of data providers.

To get started, go ahead and download the Python YouTube Data Client Library. Follow the instructions for installing the Library as well as any required dependencies (in this case, ElementTree, which is only necessary if you aren’t running Python 2.5).

Just to make sure you’ve got everything set up correctly, fire up a terminal window, start Python and try importing the modules we need:

>>> import

>>> import

If those imports worked, you’re ready to start grabbing data.

Working with the YouTube Data API

The first thing we need to do is construct an instance of the YouTube Data service. Enter this code at the prompt:

yt_service =

That’s a generic object with no authentication, so we can only retrieve public feeds. But for our purposes, that’s all we need. First, let’s write a function that can parse the data we’ll be returning.

Create a new text file named and paste in this code:



class YoutubeClient:

    def __init__(self):

        self.yt_service =

    def print_items(self, entry):

        print 'Video title: %s' %

        print 'Video published on: %s ' % entry.published.text

        print 'Video description: %s' %

        print 'Video category: %s' %[0].text

        print 'Video tags: %s' %

        print 'Video flash player URL: %s' % entry.GetSwfUrl()

        print 'Video duration: %s' %

        print '----------------------------------------'

    def get_items(self, feed):

       for entry in feed.entry:


Obviously, if you want to store the data you’re about to grab, you will need to rewrite the print_items function to do something other than just print out the data. But for the sake of example, and because there are a near infinite number of ways your database could be structured, we’ll just stick with a simple print function for now.

So make sure is on your PythonPath and fire up Python again, then input these lines:

>>> from youtube_client import YoutubeClient

>>> client = YoutubeClient()

>>> client.get_items(client.yt_service.GetMostLinkedVideoFeed())

The last line should produce a barrage of output as the client prints out a list of most linked videos and all of the associated data for each clip. To get that list, we just used one of the YouTube service modules’ built-in methods, GetMostLinkedVideoFeed().

While that’s all well and good if you want the most linked videos on the entire YouTube site, what if you want something more specific? Say, your uploaded videos?

To get that list, we’re going to use another method of YouTube service module: the GetYouTubeVideoFeed() method.

Getting a List of Your Uploaded Videos

First, find the video feed URL for your YouTube account, which should look something like this:

Next, plug that into our already running client with these two lines:

url = ''


You should see a list of all your recently uploaded videos, along with all the metadata we plugged into our print_items() function.


Hopefully, this lesson has given you some insight into how YouTube’s Data API operates. We’ve really just scratched the surface — dozens of methods are available to retrieve all sorts of data — see the [Python YouTube Data API guide] for more details.

While we’ve used Python, the methods and techniques are essentially the same for all the client libraries, so you should be able to interact with YouTube using a language you’re comfortable with.

Obviously you’ll want to adjust the print_items() function we demonstrated to do something more useful than just printing the results. Like what, you ask?

If you’re using Django, you could create a model to hold all the data and then use the model’s get_or_create() method to plug the data in via print_items(). For full automation, you could also write a shell script to call the methods we used above and attach the script to a cron job.

Anyway, there you have it — an easy way to add YouTube videos to your own personal site with no manual labor on your end.

From the YouTube series

Lesson 1: The Player API
Lesson 2: The Data API