1

I need to fetch the number of public repositories of a user using the Github API. I know that we can get a list of repos using /users/{username}/repos. This query returns a list of repositories. What I want is just the total number of repositories not the entire list. If I try to count the repositories using /users/{username}/repos it works fine for the users that have smaller number of repositories but for users that have a large number of repositories , the API call takes too much time. Is there any way by which I can get just the number of repos and not the entire list?

3 Answers 3

1

You can use this url https://api.github.com/users/{youruser}/repos to fetch the repository's information, it's an array so you can use the .length method to get the amount of repos. Note: only the public repos will be fetched

just notice this will do the job without fetching so much data

const resp  = await fetch("https://api.github.com/users/{username}")
const data = await resp.json();
console.log(data.public_repos)
Sign up to request clarification or add additional context in comments.

Comments

0

Instead of getting the list of repositories, you can get the user object and look at the public_repos field; for example, using the GitHub CLI:

$ gh api users/bewuethr | jq '.public_repos'
21

Comments

0

The total_count field on the first response represents the total count of repositories matching, the response has your answer already.

2 Comments

where can I find this total _count field? Can you provide a small code snippet if possible?
search query with type:user api.github.com/search/users?q=type%3Auser

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.