Need to generate Gravatar URLs from email addresses? This guide covers everything you need to know about building Gravatar URLs via API, including MD5 hashing, default fallbacks, and implementation examples in multiple languages.
What is Gravatar?
Gravatar (Globally Recognized Avatar) is a service that associates user-uploaded profile pictures with email addresses. When someone signs up for a Gravatar-enabled site, their avatar appears automatically based on their email address.
Gravatar URLs are constructed from MD5 hashes of lowercase, trimmed email addresses, like: https://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346
How Gravatar Works
Gravatar URL generation follows these steps:
- Trim leading and trailing whitespace from the email
- Convert the email to lowercase
- Calculate the MD5 hash of the processed email
- Construct the URL with optional parameters
Using the Gravatar URL API
TinyFn provides a simple endpoint to generate Gravatar URLs:
GET https://api.tinyfn.io/v1/gravatar?email=user@example.com
Headers: X-API-Key: your-api-key
{
"email": "user@example.com",
"hash": "b58996c504c5638798eb6b511e6f49af",
"gravatar_url": "https://www.gravatar.com/avatar/b58996c504c5638798eb6b511e6f49af",
"gravatar_url_with_params": "https://www.gravatar.com/avatar/b58996c504c5638798eb6b511e6f49af?s=200&d=identicon"
}
Parameters
| Parameter | Type | Description |
|---|---|---|
email |
string | The email address (required) |
size |
integer | Image size 1-2048 pixels (default: 80) |
default |
string | Fallback image: 404, mp, identicon, monsterid, wavatar, retro, robohash, blank |
rating |
string | Maximum rating: g, pg, r, x (default: g) |
Code Examples
JavaScript / Node.js
const response = await fetch(
'https://api.tinyfn.io/v1/gravatar?email=user@example.com&size=200&default=identicon',
{ headers: { 'X-API-Key': 'your-api-key' } }
);
const data = await response.json();
console.log(data.gravatar_url_with_params);
// Use in img tag
document.querySelector('.avatar').src = data.gravatar_url_with_params;
Python
import requests
response = requests.get(
'https://api.tinyfn.io/v1/gravatar',
params={'email': 'user@example.com', 'size': 200, 'default': 'retro'},
headers={'X-API-Key': 'your-api-key'}
)
data = response.json()
print(f"Gravatar URL: {data['gravatar_url_with_params']}")
cURL
curl "https://api.tinyfn.io/v1/gravatar?email=user@example.com&size=100" \
-H "X-API-Key: your-api-key"
Default Image Options
| Option | Description |
|---|---|
404 | Return 404 error if no Gravatar exists |
mp | Mystery person silhouette |
identicon | Geometric pattern based on email hash |
monsterid | Generated monster with unique features |
wavatar | Generated face with unique features |
retro | 8-bit arcade style pixelated face |
robohash | Generated robot with unique features |
blank | Transparent PNG image |
Best Practices
- Always set a default: Provide a fallback for users without Gravatars
- Use HTTPS: Always use secure Gravatar URLs
- Set appropriate rating: Use 'g' rating for general audiences
- Cache wisely: Gravatar URLs are deterministic; cache appropriately
Try the Gravatar URL API
Get your free API key and start generating Gravatar URLs in seconds.
Get Free API Key