File Under: APIs

Using the Flickr API

One the first and most comprehensive application interfaces of the Web 2.0 era, the Flickr API was in no small part responsible for the site’s success.

There were dozens of photo sharing sites clamoring for attention when Flickr first launched, but thanks its flexible API, developers began building and extending the site far beyond the capabilities of others.

The Flickr API exposes almost every piece of data stored on the site, and it offers near limitless possibilities for mashups, data scraping, tracking friends — just about anything you can think of.

For examples of some of the more popular applications using Flickr’s API, check out the various desktop uploaders available on all platforms. Also have a look at some of the tag-based Flickr mashups people have created, like Earthalbum and Flickrmania. Perhaps the most prolific of Flickr API users is John Watson (Flickr user fd) who has an extensive collection of tools and mashups available.

Getting Started

The nice thing about Flickr is that the API is very mature. There are already client libraries available for most languages — several libraries in some cases. That means you don’t need to sit down and work out the details of every method in the API, you just need to grab the library for your favorite programming language.

For the sake of example, I’m going to use a Python library to retrieve all the photos I’ve marked as favorites on Flickr.

First grab Beej’s Python Flickr API library and install it on your Python path (instructions on how to do that can be found in the documentation). I like Beej’s library because it handles the XML parsing without being dependent on any other libraries.

Now let’s write some code.

Accessing Flickr with Python

Fire up a terminal and start Python. Now import the flickrapi and set your API key:

import flickrapi


Now we’re going to create an instance of the flickrapi client:

flickr = flickrapi.FlickrAPI(api_key)

OK, now let’s grab all the photos we’ve marked as favorites. Accessing all the methods of the Flickr API takes the general form:


So to grab our favorites we’ll use:

favs = flickr.favorites_getPublicList(user_id='85322932@N00')

The favs variable now holds our list of images as parsed XML data. To print it, we just loop through and pull out what we want:

for photo in[0].photo:

    print photo['title']

To access the actual images, for instance to generate some HTML, we just need to build a URL:

for photo in[0].photo:

    print '<img src="'+"" % (photo['farm'], photo['server'], photo['id'], photo['secret']) +'" />'


If all you want to do is put images on your website, there’s probably already a widget that can handle the task. And of course you can DIY if you like by extending the method above.

But what if you want to plot all the Favorites we just retrieved on a Google Map? That’s exactly the sort of mashup task where the Flickr API excels.

To make our retrieved photos mappable, we would just need to add a parameter to our original method call. We’d need to tell Flickr to include the photos’ geo coordinates, for instance:

favs = flickr.favorites_getPublicList(user_id='85322932@N00', extras='geo')

Now we can parse through our set and grab the coordinates:

for photo in[0].photo:

    print photo['latitude'] + photo['longitude']

From there, we can pass that over to the Google Maps API and plot the images.

In this particular case, only a couple of the returned photos actually have lat/long info. Flickr’s tagging system lets users add geo coordinates to photos, but most casual users don’t go to the trouble to add the location info to their images. Because of this, it’s a good idea to test for non-zero values before passing the data to the Google Maps API.

Tip: The Flickr API will return data formats other than XML. For instance we could use this method to get a JSON response:

favs = flickr.favorites_getPublicList(user_id='85322932@N00', extras='geo', format='JSON')

The Flickr API exposes nearly every aspect of the site, which makes it both limitless and daunting. Thankfully Flickr has excellent documentation. As for what you can do with the Flickr API, the best mashups seem to start with the thought, “you know what would be cool…”