Scribbles and Bits

by Derek Gathright

Creating an API Service With YQL

I spent a few days last week in New York City at Yahoo’s Open Hack All-stars event. At this hack day, I was mentoring a team of 3 students from the University of Texas who set out to create a hack that allows you to control a media experience on your TV by using your iPad.

For this hack, they needed to talk to search APIs from 4 different services (Youtube, Justin.tv, Flickr, Netflix), parse the results, and display a thumbnail for each item with a link to play/view it. Traditionally, this would be a rather bulky iPad application where you’d have to include all the code and logic to communicate with the various JSON, XML, & ATOM service APIs, parse the results, combine them, and finally render the content. Likely, the HTTP calls would be synchronous, which would certainly present some issues as you get to 5+ APIs and you have to wait for one response to return before making the next.

Alternatively, you could create an API service that will do all of this for you. When that option was presented, I immediately realized YQL would be perfect for this task. Why?

  • It can communicate with any HTTP-based APIs, asyncronously, so your response time is always as fast as the slowest API you have to talk to
  • Use custom JavaScript to parse the results and form the return set
  • Reduces the number of requests your client makes to a single HTTP request

So, I strapped on the headphones and began coding. A few hours later, here’s the result. It’s YQL datatable that heavily uses the <execute> feature, which allows you to run arbitrary JavaScript. Within <execute>, you get a simple library that allows you to do things like parse JSON, make HTTP calls, and create XML structures with E4X. The datatable code is pretty straight-forward really. Here’s the service to talk to, the URLs to send the search query to, and the callback to parse each result set. Now go!

The beauty of this YQL datatable is that you have now created a fully-functional high-performance API server without the need for a server of your own to run it on.

Here’s a JSFiddle of the script in action. Click the play button to see the combined search results.

You can also toy around with the query in the YQL console here.

If you are interested in learning more fun stuff you can do with YQL, here’s another post, How-to: Secure OAuth in JavaScript