alecor.net

Search the site:

2010-12-03

How to retrieve list of public repos from user using Github's API

Summary:

How to programmatically retrieve the list of repos for a given user using the Github's API.

Example of doing this in Python using the requests library:

Prerequisite

Generate a personal access token on Github.

  1. Go to your Github account settings
  2. Select "Developer settings"
  3. Then "Personal access tokens" and
  4. Generate a new token with "repo" scope.

Using the API

Once you have your personal access token, you can use the following code to retrieve a list of all public repositories for a user:

import requests

username = "YOUR_GITHUB_USERNAME"
token = "YOUR_PERSONAL_ACCESS_TOKEN"

url = f"https://api.github.com/users/{username}/repos?type=public"
headers = {"Authorization": f"Token {token}"}

response = requests.get(url, headers=headers)
repos = response.json()

for repo in repos:
    print(repo["name"])

Replace YOUR_GITHUB_USERNAME with your Github username, and YOUR_PERSONAL_ACCESS_TOKEN with the personal access token you generated.

This code sends a GET HTTP request to the Github API to retrieve a list of public repositories for the specified user. The response is in JSON format, which we can parse using the json() method. The code then loops through the list of repositories and prints the name of each repository.

Nothing you read here should be considered advice or recommendation. Everything is purely and solely for informational purposes.