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.
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.
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 here’s 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 you’ll 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 you’ll 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 there’s no content to return because the repository has been deleted. If you check your repositories, you won’t find it.
Now, if you run the code again, you’ll get a 404 status code, which means that the repository doesn’t 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.