Skip to content

Instantly share code, notes, and snippets.

@jasonrudolph
Last active December 15, 2023 14:52
Show Gist options
  • Star 63 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save jasonrudolph/6057563 to your computer and use it in GitHub Desktop.
Save jasonrudolph/6057563 to your computer and use it in GitHub Desktop.
GitHub Search API: Get the number of stars for a repository

James Sugrue asked, "@GitHubAPI is there a way to find the number of stars for a given repository?"

Example

$ curl -ni "https://api.github.com/search/repositories?q=more+useful+keyboard" -H 'Accept: application/vnd.github.preview'
{
  "total_count": 1,
  "items": [
    {
      "id": 9118195,
      "name": "keyboard",
      "full_name": "jasonrudolph/keyboard",
      "owner": {
        "login": "jasonrudolph",
        "id": 2988,
        "avatar_url": "https://secure.gravatar.com/avatar/592e1e6f041f9a4ec51846fd82013aea?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png",
        "gravatar_id": "592e1e6f041f9a4ec51846fd82013aea",
        "url": "https://api.github.com/users/jasonrudolph",
        "html_url": "https://github.com/jasonrudolph",
        "followers_url": "https://api.github.com/users/jasonrudolph/followers",
        "following_url": "https://api.github.com/users/jasonrudolph/following{/other_user}",
        "gists_url": "https://api.github.com/users/jasonrudolph/gists{/gist_id}",
        "starred_url": "https://api.github.com/users/jasonrudolph/starred{/owner}{/repo}",
        "subscriptions_url": "https://api.github.com/users/jasonrudolph/subscriptions",
        "organizations_url": "https://api.github.com/users/jasonrudolph/orgs",
        "repos_url": "https://api.github.com/users/jasonrudolph/repos",
        "events_url": "https://api.github.com/users/jasonrudolph/events{/privacy}",
        "received_events_url": "https://api.github.com/users/jasonrudolph/received_events",
        "type": "User"
      },
      "private": false,
      "html_url": "https://github.com/jasonrudolph/keyboard",
      "description": "Toward a more useful keyboard",
      "fork": false,
      "url": "https://api.github.com/repos/jasonrudolph/keyboard",
      "forks_url": "https://api.github.com/repos/jasonrudolph/keyboard/forks",
      "keys_url": "https://api.github.com/repos/jasonrudolph/keyboard/keys{/key_id}",
      "collaborators_url": "https://api.github.com/repos/jasonrudolph/keyboard/collaborators{/collaborator}",
      "teams_url": "https://api.github.com/repos/jasonrudolph/keyboard/teams",
      "hooks_url": "https://api.github.com/repos/jasonrudolph/keyboard/hooks",
      "issue_events_url": "https://api.github.com/repos/jasonrudolph/keyboard/issues/events{/number}",
      "events_url": "https://api.github.com/repos/jasonrudolph/keyboard/events",
      "assignees_url": "https://api.github.com/repos/jasonrudolph/keyboard/assignees{/user}",
      "branches_url": "https://api.github.com/repos/jasonrudolph/keyboard/branches{/branch}",
      "tags_url": "https://api.github.com/repos/jasonrudolph/keyboard/tags",
      "blobs_url": "https://api.github.com/repos/jasonrudolph/keyboard/git/blobs{/sha}",
      "git_tags_url": "https://api.github.com/repos/jasonrudolph/keyboard/git/tags{/sha}",
      "git_refs_url": "https://api.github.com/repos/jasonrudolph/keyboard/git/refs{/sha}",
      "trees_url": "https://api.github.com/repos/jasonrudolph/keyboard/git/trees{/sha}",
      "statuses_url": "https://api.github.com/repos/jasonrudolph/keyboard/statuses/{sha}",
      "languages_url": "https://api.github.com/repos/jasonrudolph/keyboard/languages",
      "stargazers_url": "https://api.github.com/repos/jasonrudolph/keyboard/stargazers",
      "contributors_url": "https://api.github.com/repos/jasonrudolph/keyboard/contributors",
      "subscribers_url": "https://api.github.com/repos/jasonrudolph/keyboard/subscribers",
      "subscription_url": "https://api.github.com/repos/jasonrudolph/keyboard/subscription",
      "commits_url": "https://api.github.com/repos/jasonrudolph/keyboard/commits{/sha}",
      "git_commits_url": "https://api.github.com/repos/jasonrudolph/keyboard/git/commits{/sha}",
      "comments_url": "https://api.github.com/repos/jasonrudolph/keyboard/comments{/number}",
      "issue_comment_url": "https://api.github.com/repos/jasonrudolph/keyboard/issues/comments/{number}",
      "contents_url": "https://api.github.com/repos/jasonrudolph/keyboard/contents/{+path}",
      "compare_url": "https://api.github.com/repos/jasonrudolph/keyboard/compare/{base}...{head}",
      "merges_url": "https://api.github.com/repos/jasonrudolph/keyboard/merges",
      "archive_url": "https://api.github.com/repos/jasonrudolph/keyboard/{archive_format}{/ref}",
      "downloads_url": "https://api.github.com/repos/jasonrudolph/keyboard/downloads",
      "issues_url": "https://api.github.com/repos/jasonrudolph/keyboard/issues{/number}",
      "pulls_url": "https://api.github.com/repos/jasonrudolph/keyboard/pulls{/number}",
      "milestones_url": "https://api.github.com/repos/jasonrudolph/keyboard/milestones{/number}",
      "notifications_url": "https://api.github.com/repos/jasonrudolph/keyboard/notifications{?since,all,participating}",
      "labels_url": "https://api.github.com/repos/jasonrudolph/keyboard/labels{/name}",
      "created_at": "2013-03-30T16:01:43Z",
      "updated_at": "2013-07-22T02:01:08Z",
      "pushed_at": "2013-07-14T00:26:07Z",
      "git_url": "git://github.com/jasonrudolph/keyboard.git",
      "ssh_url": "git@github.com:jasonrudolph/keyboard.git",
      "clone_url": "https://github.com/jasonrudolph/keyboard.git",
      "svn_url": "https://github.com/jasonrudolph/keyboard",
      "homepage": "",
      "size": 228,
      "watchers_count": 235,
      "language": null,
      "has_issues": true,
      "has_downloads": true,
      "has_wiki": false,
      "forks_count": 7,
      "mirror_url": null,
      "open_issues_count": 1,
      "forks": 7,
      "open_issues": 1,
      "watchers": 235,
      "master_branch": "master",
      "default_branch": "master",
      "score": 38.069878
    }
  ]
}

Stars and watchers are in a transition period. Until that transition is complete, you get the number of stars using the old terminology (i.e., "watchers_count").

@VeraZab
Copy link

VeraZab commented Apr 22, 2020

this works well for me: curl --silent 'https://api.github.com/repos/plotly/dash' | grep 'stargazers_count'

@ikwyl6
Copy link

ikwyl6 commented Feb 6, 2022

Why do you pass -H 'Accept: application/vnd.github.preview' ?

@port19x
Copy link

port19x commented Apr 30, 2022

For just the number I came up with this
curl -s "https://api.github.com/repos/pystardust/ani-cli" | grep stargazers_count | cut -d : -f 2 | tr -d " " | tr -d ","

@ishandutta2007
Copy link

ishandutta2007 commented Oct 24, 2023

I am trying to embed the same directly in markdown , so I dont have the liberty to write custom js script.

For users I do this,
total stars

as the url is https://api.github-star-counter.workers.dev/user/imartinez
because this guy idealcover has deployed a public cloudflare worker enpoint from this project https://github.com/idealclover/GitHub-Star-Counter

For repos it doesn't work like this,

total stars

with url as https://api.github.com/repos/imartinez/privateGPT because no one has any such worker which returns a json with star.

@ishandutta2007
Copy link

For repos it doesn't work like this, total stars

with url as https://api.github.com/repos/imartinez/privateGPT because no one has any such worker which returns a json with star.

Actually I had a gap in understanding, for repos it is easier.

total stars

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment