Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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").

@rr-paras-patel

This comment has been minimized.

Copy link

@rr-paras-patel rr-paras-patel commented Jun 9, 2014

@DONGChuan

This comment has been minimized.

Copy link

@DONGChuan DONGChuan commented Nov 10, 2015

@PatelParas But how could we get directly the number with github api? It really costs sometimes to parse json ...

@eliotsykes

This comment has been minimized.

Copy link

@eliotsykes eliotsykes commented Mar 26, 2016

@DONGChuan - Using jq to extract the watcher_count value for an API request for a single repo:

curl --silent 'https://api.github.com/repos/jasonrudolph/keyboard' -H 'Accept: application/vnd.github.preview' | jq '.watchers_count'

outputs the number of stars:

429
@metrue

This comment has been minimized.

Copy link

@metrue metrue commented Aug 10, 2016

@eliotsykes this solutions is great, but GitHub api call rate limit is 6 per hour per IP if you are not authorized. https://developer.github.com/v3/#rate-limiting

@BobRay

This comment has been minimized.

Copy link

@BobRay BobRay commented Jun 23, 2017

I see there is now a stargazers_count which seems to always equal the watchers_count, but may diverge from it in the future.

BTW, getting an API key to increase the rate limit is free.

@gabrielizalo

This comment has been minimized.

Copy link

@gabrielizalo gabrielizalo commented Aug 1, 2018

Is there an example for GitHub API 4?

@philwareham

This comment has been minimized.

Copy link

@philwareham philwareham commented Apr 24, 2019

Just leaving this example here in case people want to find GitHub star count using GitHub API v4 (GraphQL), as the answer is sometimes not that easy to find...

query {
  repository(owner: your_username, name: your_repo_name) {
    stargazers {
      totalCount
    }
  }
}
@victorbaranov

This comment has been minimized.

Copy link

@victorbaranov victorbaranov commented May 30, 2019

Just leaving this example here in case people want to find GitHub star count using GitHub API v4 (GraphQL), as the answer is sometimes not that easy to find...

query {
  repository(owner: your_username, name: your_repo_name) {
    stargazers {
      totalCount
    }
  }
}

is it possible without GraphQL? (API only )

@VeraZab

This comment has been minimized.

Copy link

@VeraZab VeraZab commented Apr 22, 2020

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

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