YouTube API How-To
What is it
The YouTube API lets you search for videos and display them on your site. For example, I use the API to pull videos tagged dance and display them on Rate My Dance Moves. The YouTube API is currently the 10th most popular API on ProgrammableWeb with 46 mashups.
To get started you need to sign up for a developer account. That will give you the developer id required to use the API.
Using the API
The major API methods are for retrieving lists of videos, which you can do by tag, user, user favorites, and featured. The information returned from the list methods is usually enough to display a video on your site. All you have to do is plug the video id into the web snippit below.
<object width="425" height="350">
<param name="movie" value="http://www.youtube.com/v/ID&autoplay=1″>
</param>
<embed src=”http://www.youtube.com/v/ID&autoplay=1″
type=”application/x-shockwave-flash” width=”425″ height=”350″></embed>
</object>
Most of the information you need is in the video list methods. However, if you want a list of comments or channels for the video you’ll need to call the get_details method.
One complaint I had with list_by_tag is that the results come back ordered by relevance rather than recency. That means if you want to build a self-updating site you need to either regularly crawl the entire result set (results are paged) or sit on one of the RSS Feeds.
API Wrappers
There are good libraries for using the YouTube API with Perl, Ruby, and .NET. If you’re using another language, say Java, PHP or Python, you’ll have to write some code yourself. I’ve included links to example PHP and Python code and listed an example Ruby program that you can use as a template for whatever language you’re writing in.
There’s two competing Perl modules, but Hironori Yoshida’s WebService::YouTube and WebService::Youtube::Feeds seem best based on the recent development activity and strength of documentation. His is the only wrapper which includes support for the feeds, nice since the feeds have functionality thats not in the API (namely list by recency).
Shane Vitarana released a YouTube Ruby Gem after I’d built RateMyDanceMoves. Too bad, since it seems like the most polished of all the wrappers.
Eamonn Flynn has a .NET wrapper for the YouTube API.
There doesn’t seem to be a packaged PHP library but these two tutorials from waxjelly should be enough to get you started.
Simple PHP Script Using the YouTube API with Pagination
A More Complex PHP Script Using the YouTube API with Pagination.
ThinkHole labs has some example YouTube API code for Python users. Apparently using the API can be as simple as passing a dictionary to YouTube’s XML-RPC interface.
Code Example
I’d written my own code before the Ruby library came out. I want to show it here so you can see how simple writing your own code would be.
#!/usr/bin/env ruby
require 'open-uri'
tag = "dancing"
per_page = "100"
def_id = "YOUR_DEV_ID_HERE"
url = "http://www.youtube.com/api2_rest?" \
+ "method=youtube.videos.list_by_tag" \
+ "&tag=#{tag}&per_page=#{per_page}&dev_id=#{dev_id}&page=1"
open(url) do |f|
xml = f.read
end
doc = REXML::Document.new(xml)
elements = doc.root.get_elements("//video")
elements.each do |v|
puts v.get_elements("id").first.get_text.to_s
puts v.get_elements("title").first.get_text.to_s
puts v.get_elements("tags").first.get_text.to_s
puts v.get_elements("thumbnail_url").first.get_text.to_s
end
More Info
The Amazing YouTube Tools Collection has a long collection of YouTube tools, mashups, and plugins.
Tags: .net, api, howto, mashup, perl, php, python, ruby, youtube

November 8th, 2006 at 6:28 pm
An even more polished version of the Ruby YouTube library is coming in a few days, thanks to a few people who sent me patches. I heard about Wesabe on TechCrunch (I think). Good luck with it.
November 8th, 2006 at 7:16 pm
Awesome. I can’t wait to use it.
November 24th, 2006 at 2:23 am
you can add promote your YouTube videos on http://www.youtubeclips.org for quick search YouTube your clips
January 18th, 2007 at 9:31 am
So is there a way to get the latest video of a specific tag either via API or RSS feed?
Thanks in advance.
January 18th, 2007 at 9:42 am
I haven’t seen a documented way. If you figure out how then please let me know.
January 19th, 2007 at 9:29 am
I tried using the the video_uploaded_date parameter via the API and it invalidated the whole query. I hope they soon add that feature to the API.
Thanks for your reply.
February 13th, 2007 at 4:07 am
Hi,
How can we use You Tube API using Java. Can any one give sample code plz..
Regards,
Ananth.P
December 7th, 2007 at 4:28 am
Hi, I’m trying to get more than 20 friends at a time for the friend request. However, no matter how I do it, I can only retrieve 20. Is there a way to get more? Thanks,