{"library":"ghapi","code":"import os\nfrom ghapi.all import GhApi\n\n# Initialize GhApi. It will automatically use GITHUB_TOKEN from environment if available.\n# For unauthenticated calls, you can initialize without a token if no authenticated endpoints are used.\n# Alternatively, pass a token explicitly: api = GhApi(token=\"YOUR_GITHUB_TOKEN\")\n# Set authenticate=False to explicitly create an unauthenticated client even if GITHUB_TOKEN is set.\napi = GhApi(token=os.environ.get('GITHUB_TOKEN', ''))\n\n# Example: Get user information for a public user (can be unauthenticated)\ntry:\n    user_info = api.users.get_by_username(username='fastai')\n    print(f\"User: {user_info.login}, Public Repos: {user_info.public_repos}\")\nexcept Exception as e:\n    print(f\"Could not retrieve user info: {e}. Ensure username is correct and token (if needed) is valid.\")\n\n# Example: Get a list of repositories for an organization (requires authentication for private repos)\n# Note: 'owner' and 'repo' can also be passed to GhApi constructor for auto-insertion.\nif os.environ.get('GITHUB_TOKEN'):\n    try:\n        # For this example, let's assume we want repos for 'fastai'\n        org_repos = api.repos.list_for_org(org='fastai')\n        print(f\"\\nFirst 3 repos in 'fastai' organization:\")\n        for i, repo in enumerate(org_repos[:3]):\n            print(f\"- {repo.name}\")\n    except Exception as e:\n        print(f\"\\nCould not list organization repos: {e}. Check token scopes and organization name.\")\nelse:\n    print(\"\\nSkipping organization repo listing as GITHUB_TOKEN is not set.\")","lang":"python","description":"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.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]}