Accessing Gravatar with a Shell One-Liner
Friday 17th April 2009I’ve been playing around with different ways to use Gravatars recently. The basic concept of Gravatar is very simple – you upload an avatar to an account that is identified by your email address. The avatar can then be downloaded by anyone, anywhere, anytime as long as they have your email address. This allows any type of software that knows people’s email addresses to add a human touch the visual presentation of anything related to that person. Gravatar’s widest usage base is currently in blog comments, and most of the major blogging platforms already have native Gravatar support. The comments on this blog are Gravatar enabled – if you are wondering how to get a custom picture to show up in the comments section below, head over to the Gravatar website and set up a free account.
Accessing the Gravatar API
Using the Gravatar API (if you want to call it that) is very simple. The following URL will give you a JPEG image of my Gravatar.
http://www.gravatar.com/avatar/11c647cf66355ecc10a3a2c91e81e0a3
The jumple of numbers and letters at the end of the URL is an MD5 hash of my email address. Luckily for us, OS X 10.5 comes with md5, a command line utility for calculating MD5 hashes of files. To calculate the hash of a string, you can simply echo the string to stdout, then pipe that into md5. To calculate the MD5 hash for “me@mydomain.com”, you can use the following:
echo -n "me@mydomain.com" | md5
A few quick things to note here:
- When calculating the MD5 hash for the Gravatar API, you have to use all lower-case letters in the email address.
- The
-noption ofechodisables the trailing new-line character thatechonormally spits out.
Download It Using curl
Now we can construct the Gravatar URL, and use curl to download it:
curl http://www.gravatar.com/avatar/`echo -n me@mydomain.com | md5` > gravatar.jpg
That’s all there is to it. When you run this command, a file named “gravatar.jpg” will be generated in your current directory that contains the Gravatar for “me@mydomain.com”. Substitute your own (or someone else’s) email address, and you’ll get the corresponding Gravatar. You can also request the Gravatar to be a certain size, or change the default Gravatar returned for an unknown email address by modifying the URL query as described here.

Friday 24th April 2009 at 1:14 am
As you mentioned, the e-mail address must be in all lower-case. I know I have some addresses with mixed case in my address book because they gave me trouble when I first started working with Gravatars.
You can modify the command to force lower-case like this:
curl http://www.gravatar.com/avatar/
Friday 24th April 2009 at 1:22 am
That command line got mangled so let’s try it again a different way…
Friday 24th April 2009 at 4:21 pm
Thanks, Doug.
Clearly there’s a few issues with the way that MarkDown and HTML interact in my comments section…. I’ll get onto that now.
Thursday 14th May 2009 at 8:41 am
the next time i’m trying to explain the benefits of RESTful services to someone, I’ll just redirect them here.
And by the way, amazing clean blog theme, I really like it.