86

I'm trying to get the GitHub user picture (avatar) from users of GitHub.

I've found these API:

https://avatars.githubusercontent.com/<username>
https://avatars.githubusercontent.com/u/<userid>

But I can't find a way to get the avatar from the user email or the user display name. I can't find documentation about that.

Is there some similar URL API to get what I'm looking for?

Fez Vrasta
  • 14,110
  • 21
  • 98
  • 160

5 Answers5

205

You can append .png to the URL for the User's profile to get redirected to their avatar. You can add the query param size to specify a size smaller than the default of 460px wide (i.e. it won't allow larger than 460).

Examples:

https://github.com/twbs.png

https://github.com/npm.png?size=200

https://github.com/github.png?size=40

Paul
  • 2,164
  • 1
  • 14
  • 11
  • 7
    How did you find that out? – Ciro Santilli OurBigBook.com May 18 '16 at 18:10
  • 7
    See here: https://github.com/sindresorhus/refined-github/issues/95#issuecomment-204806394 The entire thread might be of interest, but this comment is the one that has the solution. – Paul May 22 '16 at 02:07
  • 4
    Don't rely on the `size` variable; if you're working on a website, keep your `img` `width` attribute. Sometimes it seems not to display the correct size. – SOFe Sep 02 '17 at 03:53
  • 2
    How about bots? This doesn't show the correct avatar: http://github.com/dependabot-preview.png neither https://github.com/apps/dependabot-preview.png – Bruno Lemos Sep 27 '19 at 19:14
  • It appears that users that haven't uploaded a profile photo, always return a 420x420 image even if the `size` param is sent. For example: https://github.com/asdf.png?size=64 – James Ward Apr 01 '21 at 01:50
  • @Paul thanks Paul the thread is very useful, I am able to generate the avatar – raj03 Jun 01 '21 at 14:28
18

https://developer.github.com/v3/users/#get-a-single-user

Use the /users/:user endpoint. Should be under avatar_url in the returned json.

For example, my avatar_url can be found by hitting this url.

Edit

There is another way I can think of that is kind of roundabout. Since GitHub uses Gravatar, if you know the email associated with the account, do an md5 hash of the lowercase, stripped email address and construct a url like http://www.gravatar.com/avatar/[md5_here].

photoionized
  • 5,092
  • 20
  • 23
  • Thanks but I need directly the image, would be not enough lightweight parse the json... – Fez Vrasta Apr 08 '14 at 09:12
  • additionally, I've not the github username, I've just user display name (Foo Bar, not foobar) and the email (info fetched from a commit detail) – Fez Vrasta Apr 08 '14 at 09:18
  • Off the top of my head I don't believe there will be any fully accurate way of getting a user's github picture then--as git commits can have any email address attached to them, it doesn't have to map to a github user. That being said, I believe my edit about MD5 hashes and gravatar might be your best bet. – photoionized Apr 08 '14 at 09:21
  • I already check that the repository is a GitHub repo and it's good think users use only correct emails for their commits. I can't use Gravatars because lot of users don't use gravatars on GitHub (gravatars are already implemented in my code) – Fez Vrasta Apr 08 '14 at 09:24
  • 1
    Oh, one last thing regarding Gravatar generation, looks like GitHub uses a default size of 460, so you can pass in `?size=460` at the end of your gravatar url and it will give you the same thing as the GitHub url from their api. – photoionized Apr 08 '14 at 09:25
  • Don't believe that there is any other solution that allows you to construct the url directly that I know of, sorry. – photoionized Apr 08 '14 at 17:19
  • Github isn't using gravatars... at least, not for users who upload a github profile photo directly. – broofa Oct 15 '17 at 11:36
  • Your avatar_url uses `avatars0`, I also see `avatars1`.What is the diff? `v4` is the GraphQL Api V4`. – Timo Oct 29 '20 at 18:57
3

This is an old post but nobody has proposed Github Search Users API with scope field :

Or using new Graphql API v4 :

{
  search(type: USER, query: "in:email bmartel", first: 1) {
    userCount
    edges {
      node {
        ... on User {
          avatarUrl
        }
      }
    }
  }
}
Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159
1

Using GraphQL API v4, this will work too

Query (for username)-

{
    user(login: "username") {
        avatarUrl
    }
}

Response -

{
    "data": {
        "user": {
            "avatarUrl": "https://avatars1.githubusercontent.com/u/..."
        }
    }
}
Samkit Jain
  • 1,560
  • 2
  • 16
  • 33
-1

GitHub avatar can be accessed through https://avatars.githubusercontent.com/u/YOUR_USER_ID

Optionally, you can modify the size at the end like so https://avatars.githubusercontent.com/u/YOUR_USER_ID?s=460

AjCodez
  • 360
  • 3
  • 10
  • It looks like YOUR_USER_ID has to be the *numeric* id, not the github name. So this isn't really answering the question I'm afraid. – broofa Oct 15 '17 at 11:34
  • 1
    True story With email, not sure if it's possible. With github username: github.com/USERNAME.png – AjCodez Oct 19 '17 at 17:21