Skip to content

Using Gravatar in Web, Desktop & iOS Applications

Your Gravatar is an image and public profile that follows you from site to site, appearing beside your name when you do things like comment or post. Gravatars help identify your posts on blogs and web forums, so why not a web application?

It turns out that adding Gravatars to your application is very simple using the Gravatar API.*

Build a Reusable Gravatar Connection

First, create a new project in Xojo. You can choose any type of project and there are examples available on my GitHub for Desktop, Web and iOS projects.

Click on the Insert button and select Class to add a new class to your project. Call the class “GravatarConnection” and make its super URLConnection.

Next, add a private function to generate the hash required to lookup a Gravatar:

Private Function GenerateGravatarHash(email As String) As String
      // Generate Gravatar Hash
      Var hash As String = Crypto.Hash(email.Lowercase.Trim, Crypto.HashAlgorithms.MD5)
      hash = EncodeHex(hash).Lowercase
      Return hash
    End Function

As you can see, this function takes the email address, creates an MD5 checksum, encodes that checksum as hex converting it to a lower case string and returning it to the calling method.

Now add a method to get the Profile Data:

Public Sub GetProfile(email As String)
  Me.RequestHeader("User-Agent") = "*"
  Send("GET", "HTTPS://" + GenerateGravatarHash(email) + ".json")
End Sub

Again, this method takes the email address and requests the profile in JSON format from

Next we’ll add three event definitions:

Event Avatar(Value As Picture)
Event Found(FullName As String, Location As String, AvatarURL As String)
Event NotFound()

These events will be raised based on the result of our request.

Now we need to handle the ContentReceived Event with:

Sub ContentReceived(URL As String, HTTPStatus As Integer, content As String) Handles ContentReceived
  If HTTPStatus = 404 Then
    RaiseEvent NotFound
  End If
  If URL.BeginsWith("HTTPS://") Then
    Var pic As Picture = Picture.FromData(content)
    RaiseEvent Avatar(pic)
    Var d As Dictionary = ParseJSON(content)
    Var v() As Variant = d.value("entry")
    Var entry As Dictionary = v(0)
    Var names As Dictionary = entry.Value("name")
    v = entry.Value("photos")
    Var Avatar As Dictionary = v(0)
    RaiseEvent Found(names.Lookup("formatted", ""), entry.Lookup("currentLocation", ""), _
      Avatar.Lookup("value", ""))
  End If
End Sub

Here we either raise the NotFound event or either the Gravatar or Found events, depending on the URL.

This class is freely available at and there are example projects for Desktop, Web & iOS that show how to use the class.

*UPDATE 3/22/21 – Per community feedback, this post has been updated.

Thanks Jeannot and Thom! I would recommend anyone who has downloaded from Git before March 22, 2021, to do so again.

Wayne Golding has been a Xojo developer since 2005 and is a Xojo MVP. He operates the IT Company Axis Direct Ltd which primarily develops applications using Xojo that integrate with Xero Wayne’s hobby is robotics where he uses Xojo to build applications for his Raspberry Pi, often implementing IoT for remote control.