Search code examples
githubgraphqlgithub-api

How can I get the open graph image for a GitHub repository?


I have been trying to get the open graph images from my repositories through the GraphQL API that GitHub exposes, but I always get my avatar back. I have tried querying the repositories node, the search, and the user node to no avail.

For example, for the query:

query {
  repository(name: "rust-algorithms", owner: "alexfertel") {
    openGraphImageUrl
    nameWithOwner
  }
}

I get:

"repository": {
  "openGraphImageUrl": "https://avatars.githubusercontent.com/u/22298999?s=400&v=4",
  "nameWithOwner": "alexfertel/rust-algorithms"
},

Which you can tell gives an avatar and not the open graph image generated by GitHub for the repository.

Is there a way to get this image that doesn't involve scraping GitHub?


Solution

  • The general format is:

    https://opengraph.githubassets.com/<any_hash_number>/<owner>/<repo>

    Credit here goes to user @msfjarvis on Reddit

    any_hash_number

    We can use any number or string here. This is actually to tell the API that this is the version. It's better to use hash because then it will always give the updated image. But we can use any string like 1, a, or 1a. If we always use 1 or a it will not give updated image.