GitHub avatar URL: every way to get a user's profile picture

github.com/username.png, avatars.githubusercontent.com, the REST API, and what each does when the user doesn't exist.

GitHub is the easiest platform to get an avatar from, which is why so many apps use it. There are a few URL forms, and they behave differently at the edges.

github.com/<username>.png

<img src="https://github.com/torvalds.png?size=120" alt="">

The simplest form. It redirects to avatars.githubusercontent.com and accepts ?size=. It does not need a token. The catch: you can build it for any string, so the only signal that a user does not exist is a failed image load. There is no reason attached, and a typo looks the same as a GitHub outage.

avatars.githubusercontent.com/u/<id>

<img src="https://avatars.githubusercontent.com/u/1024025?s=120" alt="">

This is the canonical CDN URL, keyed by numeric user id rather than login. It survives username changes, which makes it the right thing to store if you have the id (from OAuth or the API). ?s= sets the size. avatars.githubusercontent.com/<username> also works.

The REST API

GET https://api.github.com/users/<username> returns avatar_url and the numeric id. Unauthenticated requests are limited to 60 per hour per IP, so do this on the server, once, and store the id.

By email

GitHub does not expose a public email-to-avatar URL. If the email is a user's commit email, the commit search API can map it to a login, but that needs a token and is rate limited.

One URL that validates

<img src="https://avatarsapi.com/github/torvalds" alt="">

avatarapi fetches GitHub's image, checks it is a real image/* response, and serves it from the same URL with your chosen size (?size=16..1000), cached at the edge for 7 days by default. A user that does not exist returns a 404 with code: "ENOTFOUND" instead of an image, so your onerror placeholder actually fires.

It also does email: https://avatarsapi.com/email/<address> races GitHub and Gravatar, then tries the domain's BIMI logo. See the docs.

Summary

You have Use
Username, quick and unauthenticated github.com/<user>.png
Numeric id avatars.githubusercontent.com/u/<id>
Server-side need for id and other fields REST API
Username or email, with a real 404 for misses avatarsapi.com/github/<user> or /email/<address>
Try it: put https://avatarsapi.com/github/torvalds in an <img src>. No key, no signup. Read the docs or create a Project.