How to Use the GitHub API to List Repositories (2023)

As a software engineer, you probably use GitHub quite often, be it as part of your day job, while working on your solo projects, or when contributing to open source. Generally speaking, GitHub's UI is clean, intuitive, and nice to use. However, if you need to integrate GitHub with the applications you write, using GitHub manually just won't cut it. That's where GitHub's REST API comes in handy.

This post is a hands-on guide to the GitHub API. More specifically, you'll learn how to use the API to list repositories. We'll walk you through several scenarios, providing code samples and request examples in each one. By the end of the post, you'll feel comfortable when it comes to fetching repositories from GitHub.

Requirements

There are a few boxes you have to check if you want to follow along with the tutorial. First of all, I assume you're comfortable with the concept of RESTful APIs and have some experience working with APIs in general. I also expect you to be familiar with the workings of GitHub, but I do not assume prior experience with the GitHub API.

Where the post asks for coding samples, I'll use C#, but just because that happens to be my favorite language. You don't need to be familiar with C# to be able to follow along.

Finally, I assume three further things:

  • You're comfortable working with the command line.
  • cURL is installed on your system.
  • You know the basics of how to use it. With all of that out of the way, let's get started.

How To List All Public Repositories Belonging to a User?

Let's start with the most basic scenario: listing all repositories belonging to a given user. First, let's retrieve a single user hitting the appropriately named user's endpoint:

curl https://api.github.com/users/schacon

The request above retrieves information about Scott Chacon, a co-founder of GitHub. Here's the response body (edited for brevity's sake):

{
"login": "schacon",
"id": 70,
"node_id": "MDQ6VXNlcjcw",
"avatar_url": "https://avatars.githubusercontent.com/u/70?v=4",
"gravatar_id": "",
"repos_url": "https://api.github.com/users/schacon/repos",
"created_at": "2008-01-27T17:19:28Z",
"updated_at": "2022-04-21T13:16:25Z"
}

As you can see, one of the properties of the JSON response is "repos_urls," whose value is https://api.github.com/users/schacon/repos. Let's hit that endpoint now:

(Video) [Tutorial] - How to use GitHub REST API for Beginners | 2021

curl https://api.github.com/users/schacon/repos

That works, but the response is too long to include here. Here's an excerpt as captured using my browser:

How to Use the GitHub API to List Repositories (1)

So, to list all public repos from a user, send a GET request to https://api.github.com/users/<USER-NAME>/repos, replacingwith the actual user from whom you want to retrieve the repositories.

How To List All Public Repositories Belonging to an Organization?

Listing the repositories belonging to an organization is just as easy. Here's the template for the endpoint you should hit:

https://api.github.com/orgs/<ORGANIZATION-NAME/repos

For instance, to list repositories belonging to the Node.js organization, you'd do this: curl https://api.github.com/orgs/nodejs/repos

How Do You List Repositories for the Authenticated User?

The scenarios I covered up until now are limited to fetching private repositories. And that makes perfect sense: It'd be quite a security breach if you could access Scott Chacon's—or anyone else's—private repositories. But if you want to list all of your repositories, there's an endpoint for that: https://api.github.com/user/repos.

Pay close attention: Now it's "user," without the "s" at the end. Let's hit it and see what happens: curl https://api.github.com/user/repos

(Video) Get GitHub repositories using REST API and JQuery

This is the response:

{
"message": "Requires authentication",
"documentation_url": "https://docs.github.com/rest/reference/repos#list-repositories-for-the-authenticated-user"
}

As you can see, it didn't work. The message says quite clearly that we need authentication. If you want, use cURL again, but this time with the --head option, so you can see the response headers. The first thing you'll notice is the 404 Unauthorized status code. To get authenticated, you'll first need an authentication token. For that, you can follow the directions here. Give your token privileges over private repositories, like in the following image:

How to Use the GitHub API to List Repositories (2)

For security reasons, it's recommended you set an expiration date for your token. After it's generated, copy its value and store it somewhere safe.

Now you're ready to repeat your request, this time including the token. Use the following template, replacing username with your user name and token with the value of the personal access token you just generated: curl -u username:token https://api.github.com/user/repos You can enhance the results by using parameters. For instance, let's use visibility to get only private repositories (the possible values are public, private, and all): curl -u username:token https://api.github.com/user/repos?visibility=private You're not restricted to fetching only repositories that belong to you. You can also retrieve repos in which you're a collaborator or a member of the organization. For that, you'd use the affiliation parameter: curl -u username:token https://api.github.com/user/repos?affiliation=collaborator The possible values for this parameter are owner, collaborator, and organization_member.

A More Advanced Scenario

Up until now, you've seen how to retrieve repositories belonging to other users and to organizations. You've also seen how to get the repos you own or have access to. But what if you don't know to whom the repo belongs? What if you only remember a few details about the repository, such as its main language or some word from its title?

Well, you could retrieve all repositories and then search through the results. You could, for instance, use the /repositories endpoint—which retrieves all public repositories—and then perform your own search. Good luck doing that, though. The results to this endpoint are obviously paginated—there are many millions of repositories on GitHub. That means you need to keep requesting the next page and then the next, but you'll soon reach the rate limits of the API: 60 requests per hour for unauthenticated requests, and 5,000 per hour for authenticated requests.

That's where GitHub's very powerful search API comes in handy.

GitHub’s very powerful search API comes in handy retrieving repositories

(Video) Fetch Repositories from the Github API using React Hooks - React for beginners

List Repositories Using GitHub's Search API

The search API allows you to search for all kinds of GitHub artifacts using a versatile search syntax that allows for ordering, filtering, paging, and more.

Let's see an example: curl https://api.github.com/search/repositories?q=octokit+language:csharp The request above searches for repositories that contain "octokit" somewhere in their information and whose language is C#. Here's what the response could look like:

{
"total_count": 73,
"incomplete_results": false,
"items": []
}

As you can see, this time the payload is different, which makes sense, because this is a search result and not only a listing of entities like before. Here, before the actual items—which have been omitted for brevity—the JSON contains some metadata about the search itself, displaying the total number of results and whether it includes incomplete results.

Let's see another example: curl https://api.github.com/search/repositories?q=node+in:name+language:javascript&sort=stars&order=desc The request above performs a search that looks for repositories with the word "node" in their names and whose language is JavaScript. The results are to be sorted by the number of stars in descending order.

Unsurprisingly, the first result is Node itself:

How to Use the GitHub API to List Repositories (3)

GitHub's search API is quite powerful, and covering it in-depth would be out of the scope of this single post.

Listing Repositories Using a Wrapper Library

Understanding the underlying endpoints is recommended when working with the GitHub API.Most of the time, when developing an integration, you'll be using a wrapper library written for your favorite programming language.

Most of the time when developing an integration you’ll be using a wrapper library written for your favorite programming language

(Video) Using Python Github API & PyGithub module to automate the process of repo creation,deletion,listing

I'll provide a quick example using C# and .NET 6. Let's start by creating a new project:

dotnet new console -o gh-api-democd gh-api-demo

Then I include the package for Octokit.Net, which is the official client library for the GitHub API written in C#: dotnet add package Octokit The next step is then to write the code itself. The complete code is as follows:

// #1using Octokit;// #2Console.WriteLine("Enter the name of the user for which you want to list their repositories:");var username = Console.ReadLine();// #3var github = new GitHubClient(new ProductHeaderValue("MyAmazingApp"));var user = await github.User.Get(username);var repos = await github.Repository.GetAllForUser(user.Login);// #4foreach (var repo in repos.OrderByDescending(x => x.StargazersCount)){ Console.WriteLine(Environment.NewLine); Console.WriteLine("Name: {0}", repo.Name); Console.WriteLine("URL: {0}", repo.HtmlUrl); Console.WriteLine("Stars: {0}", repo.StargazersCount);}

I left numbered comments on the code so I could explain each part:

  1. Here I import the Octokit library that I installed earlier.
  2. Then I display a message asking for a username to be provided. I read and assign the username to a variable.
  3. Here, three things happen:
  • I start a new GitHub client.
  • Using the client, I retrieve the user for the entered username.
  • Finally, I retrieve all (public) repositories for the user.
  1. Then I just loop through the repositories (ordered from the most to the least starred), displaying their name, URL for the GitHub page, and the number of stars.

Conclusion

GitHub is a big part of the daily working lives of many software engineers. So it's not a surprise that many engineers need to integrate their apps with GitHub. Thankfully, GitHub provides one of the nicest RESTful APIs out there. It's easy to get started with, follows good standards, and is, generally speaking, well documented.

In this post, I've walked you through several ways to use the GitHub API to list repositories. By no means do I exhaust the topic, though. If you want to learn more, we encourage you to look at GitHub CLI and how it can be a nice alternative to cURL when making authenticated requests to the API. Additionally, explore the search API: It's powerful and flexible, but learning about the search syntax and options can take some time.

If you enjoy this article, follow @fusebitio on Twitter for the latest developer content on Node.js, JavaScript, and APIs.

Thanks for reading!

This post was written by Carlos Schults. Carlos is a consultant and software engineer with experience in desktop, web, and mobile development. Though his primary language is C#, he has experience with a number of languages and platforms. His main interests include automated testing, version control, and code quality.

FAQs

How do I access my GitHub API repository? ›

Go to Developer Settings ->Personal Access Tokens.

Add a name and select the scope for the API access and click on Create Token. In the next screen, make sure to copy the token and save it in a file. This token will be used in the command line to access GitHub API.

How do I see all git repositories? ›

When you're on a repository page, you'll see an indication that you're searching that repository by default:
  1. To search globally, all you need to do is select the All repositories option:
  2. You may have already noticed that the command bar will also give you these options:
6 May 2013

What can you do with the GitHub API? ›

The Check Runs API enables you to build GitHub Apps that run powerful checks against code changes in a repository. You can create apps that perform continuous integration, code linting, or code scanning services and provide detailed feedback on commits.

How do I use GitHub postman API? ›

Set up authorization
  1. Log in to GitHub and create a Personal Access Token. Give your token the permission scopes you wish to use.
  2. In this collection's "Variables" tab, paste your token in the CURRENT VALUE column of. ...
  3. Enter your GitHub username for the username variable.
  4. Save your changes!

What is repository API? ›

The API repository enables teams and organizations to gather metrics and discern insights through the built-in governance capabilities on the platform. Postman's desktop and web applications connect to the API repository through workspaces for personal, team, or public use.

How do you use an API? ›

How to Use an API
  1. Select an API. First things first, you'll want to find an API you could incorporate into your business. ...
  2. Get an API key. ...
  3. Review the API documentation. ...
  4. Write a request to an endpoint. ...
  5. Connect your app.
28 Apr 2021

How do I find my repository list? ›

List installed repositories in Linux using inxi utility. An another easy way to display the list of Linux repositories is using inxi utility. It will work on most Linux operating systems that supports Inxi. Inxi is a free, open source, and full featured command line system information tool.

How do I list git repositories in terminal? ›

Use the terminal to display the . git directory with the command ls -a . The ls command lists the current directory contents and by default will not show hidden files.

How many GitHub repositories are there? ›

As of June 2022, GitHub reported having over 83 million developers and more than 200 million repositories, including at least 28 million public repositories.
...
GitHub.
Logo
Type of businessSubsidiary
URLgithub.com
RegistrationOptional (required for creating and joining repositories, and browsing all repositories.)
16 more rows

What data can I get from GitHub API? ›

GitHub REST APIs allow users to communicate with GitHub and extract the desired information effectively by using an authenticated access token. With GitHub REST APIs, you can easily create and manage repositories, issues, branches, and more, with only a few lines of code.

What does GitHub API return? ›

The API would return the login of each follower, along with other data about the followers that you don't need. Then, for each follower, you would need to make a request to GET /users/{username}/followers .

Is the GitHub API free? ›

The API can be used for free and you can send up to 60 requests per hour if you're not authenticated (i.e. if you don't provide an API key). For serious data collection, this is not much, so it is recommended to sign up on GitHub and generate a personal access token that acts as API key.

Can I use GitHub as a API? ›

If you want to use the GitHub REST API for personal use, you can create a personal access token. The REST API operations used in this article require repo scope for personal access tokens (classic) or, unless otherwise noted, read-only access to public repositories for fine-grained personal access tokens.

How do I get data from GitHub? ›

How it works
  1. Visit your account settings page.
  2. Click “Start export” in the “Export account data” section. You will receive an email when the export is ready.
  3. Click the link in the email to download the archive.
19 Dec 2018

Is there an API for Git? ›

The Git Database API gives you access to read and write raw Git objects to your Git database on GitHub and to list and update your references (branch heads and tags).

How do I list all repositories on GitHub? ›

So, to list all public repos from a user, send a GET request to https://api.github.com/users/<USER-NAME>/repos , replacing with the actual user from whom you want to retrieve the repositories.

What is an API catalog? ›

An API catalog is a library of available APIs, most often shared through your API portal. The API catalog allows you to strategically manage, promote, and share APIs with relevant developers and end-users. Developing a good API catalog is one of the API basics you need to know.

Why we use repository pattern in Web API? ›

With the Repository pattern, we create an abstraction layer between the data access and the business logic layer of an application. By using it, we are promoting a more loosely coupled approach to access our data from the database. Also, the code is cleaner and easier to maintain and reuse.

What are the 4 types of API? ›

There are four principal types of API commonly used in web-based applications: public, partner, private and composite. In this context, the API "type" indicates the intended scope of use.

What is GitHub API? ›

Github APIs( or Github ReST APIs) are the APIs that you can use to interact with GitHub. They allow you to create and manage repositories, branches, issues, pull requests, and many more. For fetching publicly available information (like public repositories, user profiles, etc.), you can call the API.

How do you use API for beginners? ›

APIs provide a way for an app to interact with each other. For example, one app could request data from another app and get data in return.
...
You will need a few items to get started with your test API:
  1. A domain name.
  2. A place to host the code, like GitHub or SourceForge.
  3. An HTTP server so that you can run your code locally.
19 Jul 2022

Where are my GitHub repositories? ›

Find an existing repo

You can find repos in two ways: Type “14ers-git” in the github.com search bar to find the repository.

Where are GitHub repositories stored? ›

Within a repository, Git maintains two primary data structures, the object store and the index. All of this repository data is stored at the root of your working directory in a hidden subdirectory named . git.

How do I find my GitHub repository path? ›

On the GitHub website, click on you repository of interest. Locate the green button named Code and click on it. The GitHub URL will appear.

How do I get a list of remote repositories? ›

You can list the remote branches associated with a repository using the git branch -r, the git branch -a command or the git remote show command. To see local branches, use the git branch command. The git branch command lets you see a list of all the branches stored in your local version of a repository.

How can I see all apt repositories? ›

list. d/ directory. Alternatively, you can use apt-cache command to list all repositories. This command will provide also provide more details about the repository.

How do I view repositories on GitHub desktop? ›

On GitHub.com, navigate to the main page of the repository. Above the list of files, click Code. Click Open with GitHub Desktop to clone and open the repository with GitHub Desktop.

What is the largest Git repository? ›

The distinction of “largest Git repository” goes to Microsoft, who announced today that their Windows Git repository has: approximately 3.5M files that. results in a Git repo of about 300 gigabytes in size.

What are the two main types of repositories? ›

There are exactly two types of repositories: local and remote: the local repository is a directory on the computer where Maven runs. It caches remote downloads and contains temporary build artifacts that you have not yet released.

What is the most popular repository on GitHub? ›

freeCodeCamp

freeCodeCamp is arguably the biggest repository on GitHub, and it's easy to see why. In a platform where newcomers arrive to learn the ways of the code, it offers them away, for free. As the name indicates, freeCodeCamp is a friendly, safe environment for coding enthusiasts to learn the craft.

Can you pull data from an API? ›

An API is useful for pulling specific information from another program. If you know how to read the documentation and write the requests, you can get lots of great data back.

Can API be used to collect data? ›

An API is a back-end interface through which third-party developers may connect new add-ons to an existing service. The API is also an interface for researchers to collect data off a given social media service for empirical analysis.

Can I scrape data from GitHub? ›

You can read more in the Blog Writeup. As of May 2021, Flat Data scraping is officially supported by GitHub, check them out.

What is the difference between API and Webhooks? ›

An application programming interface (API) is a software interface that serves as a bridge between computers and applications. A webhook is a way for one application to deliver data to another app in real-time.

Is API just backend? ›

Often, an API is considered a backend "component". And, for instance, a database can also be treated as another backend component. The APIs you are likely referring to here are web APIs, so could be considered as backend components.

Which API is best for backend? ›

A guide to Top APIs for Backend
  • Backendless.
  • JHipster.
  • Tradematic Cloud API.
  • Appery.io.
  • Spring Boot.
  • Kii.
  • Appcelerator.
  • CakePHP.

What is GitHub API rate limit? ›

GitHub Apps that are installed on an organization within an enterprise on GitHub.com are subject to a limit of 15,000 requests per hour per organization that has installed the app.

Will GitHub be free forever? ›

If you subscribe to the GitHub Free plan, you can start using the available features and it is free forever. This plan is not a trial version but rather a brief look at the software and all that it can offer. In the Free plan, you will get access to: Unlimited public/ private repositories.

Is API a push or pull? ›

Pull vs Push APIs

In a pull API, the client requests the information. In a push API, the server sends the information as it becomes available. Pull architecture is request driven: the client sends the request, and the server responds accordingly.

Can JSON be an API? ›

JSON:API is a specification for how a client should request that resources be fetched or modified, and how a server should respond to those requests. JSON:API can be easily extended with extensions and profiles.

Which tool is best for API? ›

Top 15 API Testing Tools on the Market
  • Katalon Studio. ...
  • Postman.
  • Apigee.
  • JMeter. ...
  • Rest-assured.
  • Assertible.
  • Soap UI.
  • Karate DSL.

How do I use Python API to extract data from GitHub? ›

Let us try to obtain all contributors based on the sum of their contributions across all repositories.
  1. Step 1: You need to copy & paste your token and create your headers.
  2. Step 2: Extract All Repos' Names.
  3. Step 3: The image below shows the “repos” structure and we need to extract “full_name” from each repo's information.

What is the difference between git pull and git fetch? ›

Git Fetch is the command that tells the local repository that there are changes available in the remote repository without bringing the changes into the local repository. Git Pull on the other hand brings the copy of the remote directory changes into the local repository.

How do I pull data from GitHub to excel? ›

You can then work with live GitHub data in Excel.
  1. In Excel, open the Data tab and choose From Other Sources -> From Microsoft Query.
  2. Choose the GitHub DSN. ...
  3. In the Query Wizard, expand the node for the table you would like to import into your spreadsheet. ...
  4. The Filter Data page allows you to specify criteria.

How does GitHub Webhook work? ›

Webhooks allow you to build or set up integrations, such as GitHub Apps or OAuth Apps, which subscribe to certain events on GitHub.com. When one of those events is triggered, we'll send a HTTP POST payload to the webhook's configured URL.

How do I deploy API to GitHub? ›

Go to https://github.com/ and log in.
...
Create a Git Repository
  1. Click + and then select New repository.
  2. In the Repository name field, enter myNodeApp, select Public, and then click Create repository.
  3. Extract the contents of the employee-app. zip file.
  4. Open a Git Bash window and go to the employee-app directory.

Is an API an application? ›

API is the acronym for Application Programming Interface, which is a software intermediary that allows two applications to talk to each other. Each time you use an app like Facebook, send an instant message, or check the weather on your phone, you're using an API.

How do I view an API? ›

The easiest way to start using an API is by finding an HTTP client online, like REST-Client, Postman, or Paw. These ready-to-use tools help you structure your requests to access existing APIs.

How do I find my GitHub API URL? ›

URL: enter as https://api.github.com/user/repos as URL. Request Name: give a user-friendly name to this request in Paw, like "GitHub User Repositories"

How do I view a GitHub repository on my website? ›

Under your repository name, click Settings. In the "Code and automation" section of the sidebar, click Pages. Under "GitHub Pages", select the GitHub Pages visibility drop-down menu, then click a visibility. To see your published site, under "GitHub Pages", click your site's URL.

How do I connect to a GitHub repository server? ›

  1. Create a new repository on your GitHub Enterprise Server instance. ...
  2. Open Terminal .
  3. Change the current working directory to your local project.
  4. Use the init command to initialize the local directory as a Git repository. ...
  5. Add the files in your new local repository.

How do I run API commands? ›

Wait for the response.
  1. Find the URI of the external server or program. To make an API call, the first thing you need to know is the Uniform Resource Identifier (URI) of the server or external program whose data you want. ...
  2. Add an HTTP verb. ...
  3. Include a header. ...
  4. Include an API key or access token. ...
  5. Wait for a response.
20 Sept 2021

How do I access API files? ›

Using the File System Access API #
  1. Read a file from the local file system # ...
  2. Write the file to the local file system # ...
  3. Specifying a suggested file name and start directory # ...
  4. Specifying the purpose of different file pickers # ...
  5. Stored file or directory handles and permissions #

Does GitHub have a REST API? ›

GitHub provides two APIs: a REST API and a GraphQL API. You can interact with both APIs using GitHub CLI, curl, the official Octokit libraries, and third party libraries.

What is GitHub repository URL? ›

A remote URL, for example, https://github.com/user/repo.git. For example: $ git remote add origin https://github.com/USER/REPO>.git # Set a new remote $ git remote -v # Verify new remote > origin https://github.com/USER/REPO.git (fetch) > origin https://github.com/USER/REPO.git (push)

How can I see a repository? ›

You can inspect a Git repository by using the git status command. This command allows you to see which changes have been staged, which haven't, and which files aren't being tracked by Git. You should try and remember that status output does not show you any information regarding the committed project history.

How do I get my GitHub repository to show up on Google search? ›

  1. go to google search console Google Search Console.
  2. add new "property" with "URL prefix" as suggested.
  3. add the meta tag on your github-pages site (html-head) as described, looks like <meta name="google-site-verification" content="...">
  4. Click on "request indexing"

How do I find files in repository? ›

In git Repo Click on "Go to file". It will show "repo/". Enter specific file name.

How do I connect to a repo server? ›

Connecting to a repository
  1. Enter the server name or IP address into the Host name box.
  2. In the Port box, enter the communication port (generally 53400) at which the repository server may be found, and Click OK. ...
  3. Select the desired server from the Server drop-down list.

How do I SSH to a Git repository? ›

Procedure
  1. From the GitHub home page of the Git repository, click the Clone or download button to get the SSH Git repository URL. A Clone with HTTPs window opens.
  2. On the Clone with HTTPs window, click the Use SSH link. The Clone with SSH window displays.
  3. On the Clone with SSH window, you can see the SSH information.

How do I connect to existing Git repository? ›

Add an existing project to GitHub steps
  1. Create a GitHub repository for the existing project.
  2. Copy the GitHub URL for the new repo to the clipboard.
  3. Perform a git init command in the root folder of the existing project.
  4. Add all of the existing project's files to the Git index and then commit.
28 May 2022

Videos

1. GitHub API, How to get data from a repo inside the terminal
(Freison Castro)
2. Fetching Repos from GitHub API - Day 6
(Ben Awad)
3. How To Get The User Repo From Github Api Using React JS | Github User API Tutorial in Hindi - Gagan
(Coding Monks | Newton School)
4. GitHub API Tutorial for getting total repository downloads and more
(GoXR3Plus Studio)
5. EASY Python Project AUTOMATION **Create new repos using GITHUB API
(Curious Coding)
6. Consuming The Github Users API using ReactJs
(TsbSankara)
Top Articles
Latest Posts
Article information

Author: Tish Haag

Last Updated: 01/11/2023

Views: 5909

Rating: 4.7 / 5 (67 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Tish Haag

Birthday: 1999-11-18

Address: 30256 Tara Expressway, Kutchburgh, VT 92892-0078

Phone: +4215847628708

Job: Internal Consulting Engineer

Hobby: Roller skating, Roller skating, Kayaking, Flying, Graffiti, Ghost hunting, scrapbook

Introduction: My name is Tish Haag, I am a excited, delightful, curious, beautiful, agreeable, enchanting, fancy person who loves writing and wants to share my knowledge and understanding with you.