Explore the practical application of the GitHub API through Python in this detailed guide. Understand the methods to automate tasks, manage repositories, and directly interact with GitHub's capabilities from your Python code.
We're going to look at using Python with the GitHub API. Whether you're aiming to automate repository management or integrate GitHub features into your own applications, Python offers a straightforward way to interact with this API.
💡Click for a more detailed post on how to use Python to connect and interact with APIs
Using Python with the GitHub API
To use the GitHub API in Python, you first need to create a GitHub account. This will allow you to generate a personal access token, which you can use to perform API operations such as POST, DELETE, etc. To create a personal access token, you can follow the official guide by GitHub on creating an access token.
Remember to check the right boxes while generating tokens to perform different actions with the repo.
The base URL is this: https://api.github.com/.
Lets retrieve some data using a GET request in Python.
import requests
username = "apify" # github username
url = f"https://api.github.com/users/{username}"
user_data = requests.get(url)
print(user_data.json())
Heres the code output:
{
"avatar_url": "https://avatars.githubusercontent.com/u/24586296?v=4",
"bio": "We're making the web more programmable.",
"blog": "https://apify.com/",
"company": null,
"created_at": "2016-12-15T10:36:52Z",
"email": "hello@apify.com",
"events_url": "https://api.github.com/users/apify/events{/privacy}",
"followers": 279,
"followers_url": "https://api.github.com/users/apify/followers",
"following": 0,
"following_url": "https://api.github.com/users/apify/following{/other_user}",
"gists_url": "https://api.github.com/users/apify/gists{/gist_id}",
"gravatar_id": "",
"hireable": null,
"html_url": "https://github.com/apify",
"id": 24586296,
"location": "The Interweb",
"login": "apify",
"name": "Apify",
"node_id": "MDEyOk9yZ2FuaXphdGlvbjI0NTg2Mjk2",
"organizations_url": "https://api.github.com/users/apify/orgs",
"public_gists": 0,
"public_repos": 108,
"received_events_url": "https://api.github.com/users/apify/received_events",
"repos_url": "https://api.github.com/users/apify/repos",
"site_admin": false,
"starred_url": "https://api.github.com/users/apify/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/apify/subscriptions",
"twitter_username": "apify",
"type": "Organization",
"updated_at": "2023-08-03T08:51:47Z",
"url": "https://api.github.com/users/apify"
}
The output shows all the information about the username, such as bio, email, and public repos.
Next, to retrieve the repository's name, you can use the /repos API endpoint and then use the "name" parameter as a filter.
import requests
base_url = "https://api.github.com"
def get_user_repos(username):
url = f"{base_url}/users/{username}/repos"
response = requests.get(url)
if response.status_code == 200:
repositories_data = response.json()
return repositories_data
else:
return None
username = "apify"
user_repos = get_user_repos(username)
if user_repos:
print(f"Repositories of {username}:")
for repo in user_repos:
print(repo["name"])
else:
print(f"Failed to retrieve repositories.")
The code output looks like this:
Repositories of apify:
act-crawl-url-list
act-crawler-results-to-s3
actor-algolia-website-indexer
actor-beautifulsoup-scraper
actor-chat-with-your-website
actor-content-checker
actor-crawler-cheerio
actor-crawler-puppeteer
actor-example-php
actor-example-proxy-intercept-request
actor-example-python
actor-example-secret-input
actor-imagediff
actor-integration-tests
actor-legacy-phantomjs-crawler
actor-monorepo-example
actor-page-analyzer
actor-proxy-test
actor-quick-start
actor-scraper
actor-scrapy-executor
actor-screenshot-url
actor-selenium-mocha-runner
actor-templates
airbyte
algolite
apify-actor-docker
apify-aggregator-template
apify-cli
apify-client-js
You can also use query parameters to filter your results, as shown below:
import requests
base_url = "https://api.github.com"
def get_user_repos(username):
url = f"{base_url}/users/{username}/repos"
query_params = {
"sort": "updated",
"per_page": 5
}
response = requests.get(url, params=query_params)
if response.status_code == 200:
repositories_data = response.json()
return repositories_data
else:
return None
username = "apify"
user_repos = get_user_repos(username)
if user_repos:
print(f"Repositories of {username}:")
for repo in user_repos:
print(repo["name"])
else:
print(f"Failed to retrieve repositories.")
And heres the code output:
Repositories of apify:
crawlee
got-scraping
fingerprint-suite
browser-pool
proxy-chain
The 5 repositories are displayed and sorted by their last updated date.
To perform other Python API operations using the POST, PATCH, or DELETE request, you need the access token. Let's create a new repository using the POST request.
In the code below, you pass your access token, repo name, and repo description, and youll see that a new repo will be created.
import requests
base_url = "https://api.github.com"
def create_repo(access_token, repo_name, repo_descr=None):
url = f"{base_url}/user/repos"
headers = {
"Authorization": f"token {access_token}",
}
# create json data to send using the post request
data = {
"name": repo_name,
"description": repo_descr,
}
response = requests.post(url, headers=headers, json=data)
if response.status_code == 201:
repo_data = response.json()
return repo_data
else:
return None
access_token = "YOUR ACCESS TOKEN"
repo_name = "apify_testing"
repo_descr = "New repo created using the Python GitHub API."
new_repo = create_repo(access_token, repo_name, repo_descr)
if new_repo:
print(f"New public repo created successfully!")
else:
print("Failed to create a new repo.")
Check your GitHub repositories and youll notice that a new repository has been created.
Now, let's update the description of the repository. This can be easily done by making a PATCH request to the GitHub API in Python. You can do this by calling the requests.patch() function with the URL, data to be updated, and header.
import requests
base_url = "https://api.github.com"
def update_repo_descr(access_token, username, repo_name, new_description):
url = f"{base_url}/repos/{username}/{repo_name}"
headers = {
"Authorization": f"token {access_token}",
}
data = {
"description": new_description
}
response = requests.patch(url, headers=headers, json=data)
if response.status_code == 200:
updated_repo_data = response.json()
return updated_repo_data
else:
return None
access_token = "YOUR ACCESS TOKEN"
username = "GITHUB USERNAME"
repo_name = "apify_testing"
new_description = "This is an updated description using PATCH request."
updated_repo = update_repo_descr(access_token, username, repo_name, new_description)
if updated_repo:
print(f"Repository description has been updated.")
else:
print("Failed to update the repository description.")
The repo description will be updated.
Now, let's delete this repository using the DELETE request in Python. You can do this by calling the requests.delete() function and pass the header with the access token.
import requests
base_url = "https://api.github.com"
def delete_repo(access_token, username, repo_name):
url = f"{base_url}/repos/{username}/{repo_name}"
headers = {
"Authorization": f"token {access_token}",
}
response = requests.delete(url, headers=headers)
if response.status_code == 204:
print(f"Repository has been deleted successfully. Status code: {response.status_code}")
elif response.status_code == 404:
print(f"Repository not found or already deleted. Status code: {response.status_code}")
else:
print(f"Failed to delete repository. Status code: {response.status_code}")
access_token = "YOUR ACCESS TOKEN"
username = "YOUR GITHUB USERNAME"
repo_name = "apify_testing"
delete_repo(access_token, username, repo_name)
The code output is this:
Repository has been deleted successfully. Status code: 204
The 204 status code will be returned, which indicates that theres no content to return because the repository has been deleted. If you check your repositories, you wont find it.
Now, if you run the code again, youll get a 404 status code, which means that the repository doesnt exist.
Repository not found or already deleted. Status code: 404
You've successfully learned how to interact with the GitHub API using Python. This knowledge enables you to automate tasks, manage repositories, and integrate GitHub features into your applications. Continue to explore the API's capabilities and consider applying these techniques to other projects.
If you're a Pythonista, you might like to explore web scraping with Python.
Top comments (0)