API to get repositories of a user #183182
Replies: 5 comments
-
|
It is very common to get an empty result or a 404 when using this endpoint because GitHub has two very similar-looking APIs that behave differently depending on who you are looking for. The issue is likely that you are using the "Public User" endpoint to try and find "Private" or "Organization" access. Here is the breakdown of how to fix this.
If switching to the /user/repos endpoint fixed your issue and you can now see the repositories, please consider marking this as the accepted answer so it can help others in the community find the right API! |
Beta Was this translation helpful? Give feedback.
-
|
I just want to add a couple more practical tips that helped me debug similar API issues: Testing Token ScopesIf you're still getting empty results, verify your token has the right permissions by checking the response headers: Look for the x-oauth-scopes header in the response - it will show you exactly what permissions your token has. For repository access, you need at least repo or read:org scopes. Filtering by AffiliationThe affiliation parameter is super useful when you want specific types of repos: https://api.github.com/user/repos?affiliation=owner,collaborator,organization_member This will show repos you own, collaborate on, or access through orgs - all in one call. By default, it only shows owned repos. Pagination for Large AccountsIf a user has many repos, GitHub paginates the results (30 per page by default). Add per_page and page parameters: https://api.github.com/user/repos?per_page=100&page=1 The response headers will include Link information for navigating through pages. Common Gotcha: Organisation ReposIf you need to list repos from a specific organisation the user belongs to, use: https://api.github.com/orgs/{org_name}/repos But remember - this only works if the org has made repos visible to members, or if your token has admin:org scope. Great answer - the /users/{username}/repos vs /user/repos distinction trips up so many developers! |
Beta Was this translation helpful? Give feedback.
-
|
simple answer: ❌ GET /users/{username}/repos → Not for access-based repos ✅ GET /user/repos → Correct API for repos a user can access |
Beta Was this translation helpful? Give feedback.
-
|
The List repositories for a user API only returns public repositories unless you authenticate properly. You call the API with an authenticated token The token has the correct scope (repo for classic PAT, or appropriate fine-grained permissions) Also note: This endpoint lists repos owned by the user, not all repos the user has access to via organizations. To get repositories the authenticated user can access, use: So the issue is not missing repos, but using the wrong endpoint or missing authentication/scopes. |
Beta Was this translation helpful? Give feedback.
-
|
The API endpoint you're using (GET /users/{username}/repos) only returns public repositories owned by that user. It won't show: Private repositories (unless you're authenticated and have access) To get repositories a user has access to, try these alternatives: For authenticated user's accessible repos:
To filter by affiliation:
For organization repos a user has access to:
Example with authentication: Could you share which specific scenario you're trying to accomplish? That would help provide a more targeted solution. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Select Topic Area
Question
Body
I am trying the below API to get repositories access to a user, but not getting any response even though the user has repo access. Is something missing here. Is there any other API to get the list?
https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#list-repositories-for-a-user
Beta Was this translation helpful? Give feedback.
All reactions