How Can I get My Social Preview Image Link of My Github Repo using Git API or Any Logic?

Viewed 2672

I own many github repository and I usually add projects on weekly basis. I am making my own website using github pages, since I can only host static website on Github Pages hence I am Going to Use Github API in Order to Automatically Update My New Projects On My Website. But I also Want to Add a Preview/Sample Image to It.

I Got to Know that there is an Option Named Social Preview Where I can Add an Image of My Repository to be Shown on Social Media.

Although I can Get My all Repo Info From api.github.com but I can't get my Social Media Preview Image Url.

3 Answers

GitHub GraphQL API v4 has a field to query the OpenGraph image URL. It uses the social image set in the repo settings and fallbacks to the user profile image.

Link to docs and search for "openGraphImageUrl".

You can test this out with GitHub's GraphQL API explorer (sign in required) and query:

{
  repository(owner: "vuetifyjs", name: "vuetify") {
    openGraphImageUrl
  }
}

I don't think that it is possible with GitHub API but you may try to parse <meta property="og:image" content="[IMAGE-URL]"/> tag in the HTML of your repository page.

I was facing the same issue but I thought like taking the repository link and appending /settings/og-template string after it. This returns the same image that you added in your social preview. So iterating through every link of the repositories add adding that string should return the corresponding Social Preview TRY IT!

Related