How to Use GitHub API in Python?

By | November 10, 2021
How to Use Github API in Python

GitHub is a repository hosting service that uses Git, which is a version control system (VCS). One of the best things about GitHub is that it offers APIs that allow developers to interact with GitHub repositories.

In this article, we will be focusing on GitHub APIs and how you can use GitHub in Python. To proceed with this guide, we are assuming that you already know the difference between Git and GitHub.

Vamware

How to Use GitHub API in Python?

Here, we will walk you through the Python programs to access GitHub API. We have divided this Python tutorial into 2 sections.

  • In Section 1, we will use the GitHub REST APIs to get the public data from GitHub.
  • In Section 2, we will use the Python PyGithub library and access private data from our GitHub account.

But before discussing the aforementioned sections, let us discuss the libraries that we will be using in this tutorial.

Python Libraries

  • Python requests Library

requests is the Python standard library to handle HTTP requests. We will use the Python requests library to send HTTP requests to access the GitHub REST APIs that are present at  https://api.github.com/.

  • Python PyGithub Library

The PyGithub library is an open-source Python library that can use the GitHub API V3 in Python. We can use this library to login into our GitHub account and access our private data. Also, with the help of this library, we can do all the other things that are possible with GitHub HTTP REST API.

To install the PyGithub library to your Python environment, run the following pip install command on the Command Prompt or Terminal:

pip install PyGithub
  • Python PrettyTable Library (Optional)

PrettryTable library is used to print the data in a tabular format. For this tutorial, the prettytable library is optional, and we will only be using this library to display the data in the tabular format.

To install the PrettyTable library for your Python environment, run the following pip install command:

 pip install prettytable

Now, to proceed with the guide, you need to use one of the Best Python IDEs and Text Editors.

Section 1: GitHub REST APIs with Python

With GitHub HTTP REST APIs we can access the public data or information using the Python requests library. To use the GitHub HTTPs REST APIs, we can send the GET request to the URL https://api.github.com/.

If you visit https://api.github.com/, you will see a list of all GitHub API URLs that you can use to access the Public data from GitHub.

Below is the list of all the GitHub REST APIs URLs.

{
  "current_user_url": "https://api.github.com/user",
  "current_user_authorizations_html_url": "https://github.com/settings/connections/applications{/client_id}",
  "authorizations_url": "https://api.github.com/authorizations",
  "code_search_url": "https://api.github.com/search/code?q={query}{&page,per_page,sort,order}",
  "commit_search_url": "https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}",
  "emails_url": "https://api.github.com/user/emails",
  "emojis_url": "https://api.github.com/emojis",
  "events_url": "https://api.github.com/events",
  "feeds_url": "https://api.github.com/feeds",
  "followers_url": "https://api.github.com/user/followers",
  "following_url": "https://api.github.com/user/following{/target}",
  "gists_url": "https://api.github.com/gists{/gist_id}",
  "hub_url": "https://api.github.com/hub",
  "issue_search_url": "https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}",
  "issues_url": "https://api.github.com/issues",
  "keys_url": "https://api.github.com/user/keys",
  "label_search_url": "https://api.github.com/search/labels?q={query}&repository_id={repository_id}{&page,per_page}",
  "notifications_url": "https://api.github.com/notifications",
  "organization_url": "https://api.github.com/orgs/{org}",
  "organization_repositories_url": "https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}",
  "organization_teams_url": "https://api.github.com/orgs/{org}/teams",
  "public_gists_url": "https://api.github.com/gists/public",
  "rate_limit_url": "https://api.github.com/rate_limit",
  "repository_url": "https://api.github.com/repos/{owner}/{repo}",
  "repository_search_url": "https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}",
  "current_user_repositories_url": "https://api.github.com/user/repos{?type,page,per_page,sort}",
  "starred_url": "https://api.github.com/user/starred{/owner}{/repo}",
  "starred_gists_url": "https://api.github.com/gists/starred",
  "user_url": "https://api.github.com/users/{user}",
  "user_organizations_url": "https://api.github.com/user/orgs",
  "user_repositories_url": "https://api.github.com/users/{user}/repos{?type,page,per_page,sort}",
  "user_search_url": "https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"
}

Get User Data From GitHub

Let us assume that you want to retrieve the user data or your GitHub Public User Profile data using GitHub rest API. The following section will guide you with that.

Python Program to Access User Public Data using GitHub REST API

import requests
from prettytable import PrettyTable

table = PrettyTable()
table.field_names = ["Key", "Value"]

github_username  = "khatrivinay1"

#api url to grab public user data
api_url = f"https://api.github.com/users/{github_username}"

#send get request
response = requests.get(api_url)

#get the data in json or equivalent dict format
data =  response.json()

for key, value in data.items():
    table.add_row([key, value])

print(table)

Output

+---------------------+------------------------------------------------------------------+
|         Key         |                              Value                               |
+---------------------+------------------------------------------------------------------+
|        login        |                           KHATRIVINAY1                           |
|          id         |                             48641471                             |
|       node_id       |                       MDQ6VXNlcjQ4NjQxNDcx                       |
|      avatar_url     |       https://avatars.githubusercontent.com/u/48641471?v=4       |
|     gravatar_id     |                                                                  |
|         url         |            https://api.github.com/users/KHATRIVINAY1             |
|       html_url      |                 https://github.com/KHATRIVINAY1                  |
|    followers_url    |       https://api.github.com/users/KHATRIVINAY1/followers        |
|    following_url    | https://api.github.com/users/KHATRIVINAY1/following{/other_user} |
|      gists_url      |    https://api.github.com/users/KHATRIVINAY1/gists{/gist_id}     |
|     starred_url     | https://api.github.com/users/KHATRIVINAY1/starred{/owner}{/repo} |
|  subscriptions_url  |     https://api.github.com/users/KHATRIVINAY1/subscriptions      |
|  organizations_url  |          https://api.github.com/users/KHATRIVINAY1/orgs          |
|      repos_url      |         https://api.github.com/users/KHATRIVINAY1/repos          |
|      events_url     |    https://api.github.com/users/KHATRIVINAY1/events{/privacy}    |
| received_events_url |    https://api.github.com/users/KHATRIVINAY1/received_events     |
|         type        |                               User                               |
|      site_admin     |                              False                               |
|         name        |                               None                               |
|       company       |                               None                               |
|         blog        |                         techgeekbuzz.com                         |
|       location      |                           Delhi India                            |
|        email        |                               None                               |
|       hireable      |                               None                               |
|         bio         |                                 while Life
                                  |
|                     |                                 {
                                  |
|                     |                                 Coffee 
                                  |
|                     |                                 Code 
                                  |
|                     |                                 Sleep 
                                  |
|                     |                                }                                 |
|   twitter_username  |                               None                               |
|     public_repos    |                                9                                 |
|     public_gists    |                                0                                 |
|      followers      |                                2                                 |
|      following      |                                11                                |
|      created_at     |                       2019-03-17T07:07:43Z                       |
|      updated_at     |                       2021-01-04T11:54:08Z                       |
+---------------------+------------------------------------------------------------------+

From the output, you can see that in response to our request, we get only the public data of our profile.

Get User Public Repositories Data from GitHub

In the above output, you can see that it also returns the repos_url, which represents the REST API URL to all of our public repositories. You can also follow the same link and replace the user name KHATRIVINAY1 with your own and get your public repositories.

Python Program to Access User Public Repositories from GitHub REST API

import requests
from prettytable import PrettyTable

table = PrettyTable()
table.field_names = ["Repository Name", "Created Date"]

github_username  = "khatrivinay1"   #specify your User name

#api url to grab public user repositories
api_url = f"https://api.github.com/users/{github_username}/repos"

#send get request
response = requests.get(api_url)

#get the json data
data =  response.json()

for repository in data:
    table.add_row([repository["name"], repository["created_at"]])

print(table)

Output

+----------------------------+----------------------+
|      Repository Name       |     Created Date     |
+----------------------------+----------------------+
|  10millionCommonpasswords  | 2021-01-30T09:33:59Z |
|         API_Intro          | 2019-11-14T06:36:46Z |
|         classroom          | 2020-06-17T08:54:52Z |
|      Data-Structures       | 2019-09-05T13:48:18Z |
| django-deployment-example  | 2019-08-20T04:34:19Z |
| django-deployment-project1 | 2019-07-24T09:38:15Z |
|     profile-api-django     | 2019-09-03T08:26:25Z |
|        SimpleCodes         | 2020-09-19T05:49:07Z |
|        tshirt-store        | 2020-12-08T13:12:32Z |
+----------------------------+----------------------+

Search Repositories in GitHub

We can also list down the top repositories related to a Programming language or by a user. To search for the repositories, we will use the following URL. https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}

Python Program to Search Public Repositories from GitHub REST API

import requests
from prettytable import PrettyTable

table = PrettyTable()
table.field_names = ["Repository Name", "Created Date","Language", "Stars"]

query= "python"
#first page
page=1

#search for the top repositories
api_url = f"https://api.github.com/search/repositories?q={query}&{page}"

#send get request
response = requests.get(api_url)

#get the json data
data =  response.json()

for repository in data["items"]:
    name = repository["full_name"]
    created_date = repository["created_at"]
    language = repository["language"]
    stars = repository["stargazers_count"]
    
    table.add_row([name, created_date, language, stars ])

print(table)

Output

+-----------------------------------------+----------------------+------------------+-------+
|             Repository Name             |     Created Date     |     Language     | Stars |
+-----------------------------------------+----------------------+------------------+-------+
|           TheAlgorithms/Python          | 2016-07-16T09:44:01Z |      Python      | 98195 |
|           geekcomputers/Python          | 2011-11-30T09:04:08Z |      Python      | 20291 |
|             injetlee/Python             | 2016-06-23T15:15:23Z |      Python      |  6343 |
|             TwoWater/Python             | 2017-06-07T11:52:22Z |       None       |  8514 |
|         Show-Me-the-Code/python         | 2014-11-28T01:39:43Z |       HTML       |  3067 |
|         kubernetes-client/python        | 2016-10-31T20:08:03Z |      Python      |  3407 |
|              xxg1413/python             | 2012-09-06T07:00:59Z |       None       |  2242 |
|    jakevdp/PythonDataScienceHandbook    | 2016-08-10T14:24:36Z | Jupyter Notebook | 27852 |
|            joeyajames/Python            | 2015-06-15T02:32:30Z | Jupyter Notebook |  859  |
|          docker-library/python          | 2014-06-20T22:55:57Z |    Dockerfile    |  1567 |
|             exercism/python             | 2014-02-28T03:48:58Z |      Python      |  899  |
|               poise/python              | 2012-03-14T18:37:54Z |      Python      |  525  |
|           vinta/awesome-python          | 2014-06-27T21:00:06Z |      Python      | 93525 |
|        jackfrued/Python-100-Days        | 2018-03-01T16:05:52Z |      Python      | 99126 |
|       AtsushiSakai/PythonRobotics       | 2016-03-21T09:34:43Z | Jupyter Notebook | 11434 |
| Pierian-Data/Complete-Python-3-Bootcamp | 2018-02-12T19:30:10Z | Jupyter Notebook | 14560 |
|              joe011/python              | 2013-08-07T06:08:24Z |      Python      |  659  |
|        michaelliao/learn-python3        | 2015-05-15T05:29:05Z |       None       |  4978 |
|           521xueweihan/python           | 2015-05-08T04:41:52Z |      Python      |  753  |
|              gxcuizy/Python             | 2018-06-08T01:33:56Z |      Python      |  571  |
|        yidao620c/python3-cookbook       | 2014-08-19T03:13:07Z | Jupyter Notebook |  8928 |
|             zhanghe06/python            | 2015-01-22T08:44:20Z |      Python      |  462  |
|       lining0806/PythonSpiderNotes      | 2015-08-19T03:48:30Z |      Python      |  5245 |
|           faif/python-patterns          | 2012-06-06T21:02:35Z |      Python      | 27223 |
|           Tanu-N-Prabhu/Python          | 2019-04-27T04:49:56Z | Jupyter Notebook |  261  |
|    michaelliao/awesome-python3-webapp   | 2015-05-20T22:09:21Z |       None       |  1990 |
|      wistbean/learn_python3_spider      | 2019-04-02T20:19:54Z |      Python      |  5914 |
|         realpython/python-guide         | 2011-03-15T03:24:20Z |    Batchfile     | 22413 |
|       taizilongxu/interview_python      | 2015-04-05T02:59:59Z |      Shell       | 13533 |
|        Jack-Cherish/python-spider       | 2017-05-05T07:28:13Z |      Python      | 12424 |
+-----------------------------------------+----------------------+------------------+-------+

Here ends Section 1 of this tutorial. To learn more about GitHub REST APIs, you need to go through its official documentation.

Section 2: Python PyGithub Library

In the previous section, we have walked you through the REST API provided by GitHub and discussed how can you write a Python program to access the public information or data from GitHub.

Now let’s move to the Python PyGithub library and learn how to access the private data using this library.

When you want to access your private GitHub Data or repositories, you need to log in to GitHub with your GitHub Login credentials, and this can be done using the Python PyGithub library.

Get All Your Repositories (Public/private) from GitHub

There are three ways to use the Python PyGithub library to access your account.

  • Username and Password 
# using username and password
g = Github("user", "password")
  • Access token
# using an access token
g = Github("access_token")
  • GitHub Enterprise with Custom Hostname 
# Github Enterprise with custom hostname
g = Github(base_url="https://{hostname}/api/v3", login_or_token="access_token")

Note: For this tutorial, we have used the Access Token method to login into the GitHub account. Also, we recommend you to use this method only because you do not want to write and expose your GitHub username and password in the Python script.

To generate an Access token, you can simply visit the following URL:

https://github.com/settings/tokens

When you generate the access token, make sure that you have checked all the required boxes shown below.

Python Program to Access All Repositories on GitHub

from github import Github
from prettytable import PrettyTable

table = PrettyTable()
table.field_names = ["Repository Name", "Private", "Public","Created Date","Language"]

#github generated access token
access_token ="<Enter Your Access Token>"

#login with access token
login  = Github(access_token)

#get the user
user  = login.get_user()

#get all repositories
my_repos = user.get_repos()

for repository  in my_repos:
    name =  repository.name
    private,public = repository.private, not(repository.private)
    created_date = repository.created_at
    language = repository.language
    table.add_row([name, private, public, created_date, language])

print(table)

Output

+----------------------------+---------+--------+---------------------+------------+
|      Repository Name       | Private | Public |     Created Date    |  Language  |
+----------------------------+---------+--------+---------------------+------------+
|  10millionCommonpasswords  |  False  |  True  | 2021-01-30 09:33:59 |    None    |
|         API_Intro          |  False  |  True  | 2019-11-14 06:36:46 |    None    |
|         classroom          |  False  |  True  | 2020-06-17 08:54:52 |    None    |
|        --------------      |   True  | False  | 2020-09-12 14:26:40 |    CSS     |
|      Data-Structures       |  False  |  True  | 2019-09-05 13:48:18 |   Python   |
| django-deployment-example  |  False  |  True  | 2019-08-20 04:34:19 |   Python   |
| django-deployment-project1 |  False  |  True  | 2019-07-24 09:38:15 | JavaScript |
|     ------------------     |   True  | False  | 2020-04-13 18:02:41 |    HTML    |
|   ---------------------    |   True  | False  | 2020-01-25 06:16:09 |   Python   |
|     profile-api-django     |  False  |  True  | 2019-09-03 08:26:25 |   Python   |
|        SimpleCodes         |  False  |  True  | 2020-09-19 05:49:07 |   Python   |
|        ------------        |   True  | False  | 2020-11-25 09:31:26 |    HTML    |
|        tshirt-store        |  False  |  True  | 2020-12-08 13:12:32 |    None    |
|        ------------        |   True  | False  | 2020-04-12 20:20:05 |    None    |
+----------------------------+---------+--------+---------------------+------------+

From the above output, you can see that the Python program has listed all the 13 repositories and 1 project that exists in our GitHub account. For some reason, we have tweaked the output result by replacing the name of private repositories with - symbols.

Create a New GitHub Repository

We can also create a new repository for our GitHub account using the PygGithub library. So, let’s write a Python script that can create a new repository and also push a file into that new repository.

Python Program to Create and Push Files in the new GitHub Repository

from github import Github

#generated access token
access_token ="<Enter Your Access Token>"

#login into github account
login  = Github(access_token)

#get the user
user  = login.get_user()

repository_name= "Demo-Repo"

#create repository
new_repo = user.create_repo(repository_name)

#create new file
new_repo.create_file("New-File.txt", "new commit", "Data Inside the File")

When you execute the above script, it will create a new repository by the name Demo-Repo with a text file New-File.txt.

To check if the repository is created or not, you need to check your Github repositories.

Conclusion

In this Python tutorial, you learned how to use the GitHub REST APIs and Python PyGithub library to access data from GitHub. With the help of GitHub REST APIs, you can only access public data. However, with the Python PyGithub library, you can access public as well as your private data.

Here, we have only provided an overview of the APIs and methods provided by GitHub. However, we recommend you to read the official documentation of GitHub REST API as well as the PyGithub library to get familiar with the different functionalities and methods offered by them.

GitHub REST API Official Documentation

Python PyGithub Library Official Documentation

People are also reading: 

Leave a Reply

Your email address will not be published. Required fields are marked *