llama-index-integrations/readers/llama-index-readers-github/GITHUB_APP_QUICKSTART.md
This guide will help you quickly set up and use GitHub App authentication with the llama-index-readers-github package.
Install the package with GitHub App support:
pip install llama-index-readers-github[github-app]
Create a GitHub App (one-time setup):
Generate a private key:
.pem file securelyInstall the GitHub App:
https://github.com/settings/installations/{installation_id}from llama_index.readers.github import GithubRepositoryReader, GitHubAppAuth
# 1. Set up authentication
github_app_auth = GitHubAppAuth(
app_id="123456", # Your GitHub App ID
private_key=open("your-app.pem").read(), # Your private key
installation_id="789012", # Your installation ID
)
# 2. Create reader
reader = GithubRepositoryReader(
owner="owner-name",
repo="repo-name",
github_app_auth=github_app_auth,
)
# 3. Load documents
documents = reader.load_data(branch="main")
print(f"Loaded {len(documents)} documents")
For better security, use environment variables:
import os
from llama_index.readers.github import GithubRepositoryReader, GitHubAppAuth
# Load from environment variables
github_app_auth = GitHubAppAuth(
app_id=os.environ["GITHUB_APP_ID"],
private_key=os.environ["GITHUB_PRIVATE_KEY"],
installation_id=os.environ["GITHUB_INSTALLATION_ID"],
)
reader = GithubRepositoryReader(
owner="owner-name",
repo="repo-name",
github_app_auth=github_app_auth,
)
documents = reader.load_data(branch="main")
Set environment variables in your shell:
export GITHUB_APP_ID="123456"
export GITHUB_INSTALLATION_ID="789012"
export GITHUB_PRIVATE_KEY="$(cat your-app.private-key.pem)"
Tokens are automatically:
from llama_index.readers.github import (
GitHubAppAuth,
GitHubAppAuthenticationError,
)
try:
github_app_auth = GitHubAppAuth(
app_id="123456",
private_key="invalid-key",
installation_id="789012",
)
token = github_app_auth.get_installation_token()
except GitHubAppAuthenticationError as e:
print(f"Authentication failed: {e}")
# Manually refresh the token if needed
github_app_auth.get_installation_token(force_refresh=True)
# Invalidate cached token (useful if token is revoked)
github_app_auth.invalidate_token()
github_app_auth = GitHubAppAuth(
app_id="123456",
private_key="...",
installation_id="789012",
github_base_url="https://github.your-company.com/api/v3", # Your GHE URL
)
ImportError: cannot import name 'GitHubAppAuth'Solution: Install with GitHub App support:
pip install llama-index-readers-github[github-app]
GitHubAppAuthenticationError: Failed to generate JWTSolution: Check that your private key is valid:
.pem file downloaded from GitHub401 Unauthorized when fetching tokenPossible causes:
Solution: Verify your credentials:
403 Forbidden when accessing repositoryPossible causes:
Solution:
| Feature | Personal Access Token (PAT) | GitHub App |
|---|---|---|
| Rate Limit | 5,000 requests/hour | 5,000 requests/hour per installation |
| Scope | User-level access | Repository/organization-level |
| Security | Broader access | Granular permissions |
| Expiration | Manual rotation | Automatic (1-hour tokens) |
| Audit Logs | Limited | Detailed |
| Best For | Personal projects | Team/enterprise use |
examples/github_app_example.py for more detailed examplesIf you encounter issues:
Pro Tip: Store your private key securely (e.g., using a secrets manager or environment variables). Never commit .pem files to version control!