Registry / http-networking / ghapi

ghapi

JSON →
library1.0.13pypypi✓ verified 52d ago

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.

http-networkingdevops
pip install ghapi
Install & Compatibility
Where this runs
tested against v1.0.13 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.930 runs
installs and imports cleanly · install 0.0s · import 0.820s · 19.4MB
glibc
py 3.103.930 runs
installs and imports cleanly · install 1.8s · import 0.733s · 20MB
18MB installed
● package 18MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

GhApi
from ghapi.all import GhApi
from ghapi import GhApi
The primary `GhApi` class and all its sub-APIs are conveniently exposed via `ghapi.all` for a complete interface. Direct imports from submodules (e.g., `ghapi.repos`) are also possible for specific functionality.

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.")
Debug
Known issues
gotchaFor authenticated operations, a GitHub Personal Access Token (PAT) is required. GhApi will automatically use the `GITHUB_TOKEN` environment variable if available, otherwise, you must pass it explicitly to the `GhApi` constructor. An `authenticate=False` flag can be used to override this behavior and create an unauthenticated client even if the token is present.
fix
Set `GITHUB_TOKEN` environment variable or pass `token='YOUR_TOKEN'` to `GhApi()` during initialization.
affects: All versions
breakingWhen updating files via the API, GitHub now explicitly requires `branch`, `author`, and `committer` parameters. Older code that omits these for file updates will fail.
fix
Ensure `branch`, `author`, and `committer` are provided in file update calls (e.g., `api.repos.update_file()`).
affects: Prior to 1.0.6
gotchaWhen working with media types that are not JSON (e.g., `application/vnd.github.v3.sha`), `GhApi` might return a raw string instead of a parsed Python object.
fix
Be aware of the expected return type for non-JSON media types and handle the string output accordingly.
affects: All versions
deprecatedThe `GitPython` library was removed as a dependency in `v1.0.8`. If your project implicitly relied on `GhApi` installing `GitPython` as a transitive dependency, you will now need to add `GitPython` as a direct dependency to your project.
fix
Explicitly install `GitPython` (`pip install GitPython`) if your project requires it.
affects: Prior to 1.0.8
gotchaPrevious versions (before 1.0.13) might have incorrectly decoded binary response types, potentially leading to errors or corrupted data when fetching certain content.
fix
Upgrade to GhApi 1.0.13 or newer to ensure correct decoding of non-binary types and proper handling of binary content.
affects: Prior to 1.0.13
Errors
Common errors & fixes
{ "message": "Bad credentials", "documentation_url": "https://docs.github.com/rest" }
This error, or a `401 Client Error: Unauthorized`, indicates that the GitHub API token provided is invalid, has insufficient scopes (permissions), or has expired.
fix
Ensure you are using a valid GitHub Personal Access Token (PAT) with the necessary scopes enabled for the API operation you are trying to perform. You can create a PAT in your GitHub settings under Developer settings > Personal access tokens. Initialize `GhApi` with `api = GhApi(token='YOUR_GITHUB_TOKEN')` or ensure the `GITHUB_TOKEN` environment variable is correctly set.
ModuleNotFoundError: No module named 'ghapi'
The `ghapi` library is not installed in your current Python environment, or your Python interpreter cannot find the installed package.
fix
Install the library using pip: `pip install ghapi` or if using conda: `conda install -c fastai ghapi`. Ensure your Python environment is correctly configured.
AttributeError: 'GhApi' object has no attribute 'some_nonexistent_method'
You are trying to call an API endpoint or access a property that does not exist or is misspelled within the `ghapi` object structure. `ghapi` dynamically generates its API methods from the OpenAPI spec, so the method names must exactly match the GitHub API operations.
fix
Consult the `ghapi` documentation or the GitHub API reference to find the correct operation name and its group (e.g., `api.repos.create_commit` instead of `api.repo.make_commit`). You can also inspect the `api` object (e.g., `api.git.` and press Tab in an interactive console) to discover available methods.
404 Not Found (for an existing resource)
When accessing private GitHub resources (like a private repository or private issue), GitHub's API returns a `404 Not Found` error instead of `403 Forbidden` if authentication is missing or insufficient. This is a security measure to prevent information leakage about private assets.
fix
This often points to an underlying authentication or authorization issue. Ensure your GitHub API token has the correct `repo` scopes and permissions to access the specific private resource. Double-check that the token is correctly passed and is not expired.
Upgrade
Version history
1.0.13latest on PyPI
Audit
Dependencies
fastcoreoptionalCommonly used in the fast.ai ecosystem for utility functions and object extensions, often implicitly relied upon by users for a more 'Pythonic' experience, especially when using `ghapi.all`.
Agent activity
15 hits · last 30 days
node
4
seranking-bot
4
ahrefsbot
3
Amazon
1
bingbot
1
bytedance
1
Resources