GhApi

1.0.13 · active · verified Sun Apr 05

GhApi is a Python client for the GitHub API, providing 100% always-updated coverage by automatically converting the OpenAPI spec. It allows automation of various GitHub tasks, including managing issues, pull requests, releases, GitHub Actions, and more. The current version is 1.0.13, and it has a regular release cadence with several minor updates in recent months.

Warnings

Install

Imports

Quickstart

Initializes the GhApi client, demonstrating how to fetch public user information and, if authenticated with a `GITHUB_TOKEN` environment variable, list repositories for an organization. `GhApi` dynamically generates methods based on the GitHub API, providing a Pythonic interface.

import os
from ghapi.all import GhApi

# Initialize GhApi. It will automatically use GITHUB_TOKEN from environment if available.
# For unauthenticated calls, you can initialize without a token if no authenticated endpoints are used.
# Alternatively, pass a token explicitly: api = GhApi(token="YOUR_GITHUB_TOKEN")
# Set authenticate=False to explicitly create an unauthenticated client even if GITHUB_TOKEN is set.
api = GhApi(token=os.environ.get('GITHUB_TOKEN', ''))

# Example: Get user information for a public user (can be unauthenticated)
try:
    user_info = api.users.get_by_username(username='fastai')
    print(f"User: {user_info.login}, Public Repos: {user_info.public_repos}")
except Exception as e:
    print(f"Could not retrieve user info: {e}. Ensure username is correct and token (if needed) is valid.")

# Example: Get a list of repositories for an organization (requires authentication for private repos)
# Note: 'owner' and 'repo' can also be passed to GhApi constructor for auto-insertion.
if os.environ.get('GITHUB_TOKEN'):
    try:
        # For this example, let's assume we want repos for 'fastai'
        org_repos = api.repos.list_for_org(org='fastai')
        print(f"\nFirst 3 repos in 'fastai' organization:")
        for i, repo in enumerate(org_repos[:3]):
            print(f"- {repo.name}")
    except Exception as e:
        print(f"\nCould not list organization repos: {e}. Check token scopes and organization name.")
else:
    print("\nSkipping organization repo listing as GITHUB_TOKEN is not set.")

view raw JSON →