# GitHub API tokens: find, store, rotate, and use them safely with AI agents

Source: https://botpassword.com/keys/github
Last checked: 2026-10-25

GitHub tokens are created under Settings → Developer settings → Personal access tokens, as either fine-grained tokens (recommended by GitHub) or classic tokens. GitHub Apps mint short-lived installation tokens (`ghs_`) that expire after one hour. Treat every one like a password and prefer the narrowest permissions and shortest expiry.

## Key types

| Type | Prefix | Secret | Scope | Expiry |
|---|---|---|---|---|
| Fine-grained personal access token | `github_pat_` | Yes | One resource owner, chosen repositories, and per-permission read/write/admin levels | Can be set to no expiry unless an organization or enterprise lifetime policy forbids it; tokens unused for a year are removed |
| Personal access token (classic) | `ghp_` | Yes | Broad OAuth-style scopes such as `repo`; reaches every repository the user can access | Chosen at creation (default options or a custom date); tokens unused for a year are removed |
| GitHub App installation access token | `ghs_` | Yes | Permissions and repositories granted to the app installation, optionally narrowed per request | Expires after 1 hour |
| GitHub App user access token | `ghu_` | Yes | What the app is permitted to do on behalf of the user | — |

## How to create a GitHub key

1. Sign in to GitHub, click your profile picture, then **Settings**.
2. Open **Developer settings** → **Personal access tokens**.
3. Choose **Fine-grained tokens** → **Generate new token** (or **Tokens (classic)** → **Generate new token (classic)**).
4. Set a name, an **Expiration**, the **Resource owner** and **Repository access** (fine-grained) or scopes (classic).
5. Click **Generate token** and copy it; GitHub does not show it again.

## Limiting what the key can do

Fine-grained tokens are the narrow option: pick one resource owner, only the repositories needed, and the minimum permission levels. Classic tokens use broad scopes and grant access to every repository you can reach. A token never exceeds its owner's access. For automation inside GitHub Actions, GitHub recommends the built-in `GITHUB_TOKEN` with an explicit `permissions` key.

## How to rotate a GitHub key

1. Generate a new token with the same owner, repositories and permissions.
2. Update every system that uses the old token.
3. Delete the old token under **Settings** → **Developer settings** → **Personal access tokens**.

## How to revoke a GitHub key

1. Go to **Settings** → **Developer settings** → **Personal access tokens**.
2. Open **Fine-grained tokens** or **Tokens (classic)** and click **Delete** next to the token.
3. Deleting a token that created a deploy key also deletes that deploy key.

## If the key leaked

1. Delete the token immediately in Developer settings; GitHub advises rotating the credential first.
2. Create a replacement with narrower permissions and update the systems that used it.
3. Review the account's security log and the repositories the token could reach for unexpected activity.
4. Check the secret scanning alert under the repository's security tab if the token was pushed to a repository.
5. Rewrite Git history only if needed; it is slow and often unnecessary once the token is revoked.

## Environment variables

- `GH_TOKEN`
- `GITHUB_TOKEN`
- `GITHUB_PERSONAL_ACCESS_TOKEN`

## Gotchas

- Classic tokens reach every repository you can access, so a leaked `ghp_` token is far more damaging than a repository-scoped fine-grained one.
- Fine-grained tokens can behave confusingly with `gh auth login --with-token`; the `gh` manual suggests setting `GH_TOKEN` instead.
- Several failed authentication attempts in a short period can make the API temporarily reject all attempts, including valid ones, with 403.
- Organizations can block classic tokens or require approval for fine-grained ones, so a token that works on personal repositories may fail on organization repositories.

## Using this key with AI agents (BotPassword)

- `list_credentials`: Lists the credentials this agent is granted and what it may do with each. Never values.
- `request_access`: Asks you for a grant, which you approve with your master password or decline in BotPassword on your desktop, never in the chat. The agent gets a receipt, never a value. Approval on your phone is planned.
- `run_with`: Runs a command with the key injected as an environment variable or a 0600 temporary file, never on the command line, and redacts the key from the output.
- `http_request`: Makes an authenticated API call after checking the destination against the hosts this key may be sent to. The response comes back with credential-shaped values redacted, and one that holds the key itself is refused.
- `cli_login`: Lets git, Docker, the AWS CLI and kubectl log in through bpw's credential helper, so the token is fetched when the tool needs it and not written into its config. The helper hands the value to the tool, so the grant is always disclosing. Other tools use run_with; OAuth device logins are planned.
- `rotate`: Partly built: creates a new key, stores it as pending, verifies it, promotes it, then revokes the old key after a grace period. Today it works for Cloudflare tokens and AWS IAM keys you have set up, and an agent can start it only under a grant with autopilot; other providers cannot be rotated through their API yet.
- `capture_secret`: Planned, not built yet: will read a newly created key straight from the page or command output into the vault.
- `store_secret`: Saves a value the agent already holds as a new vault item.

## FAQ

### Where do I find my GitHub personal access token?

You cannot view an existing token. Create one at **Settings** → **Developer settings** → **Personal access tokens** and copy it when it is generated; if you lost it, delete it and create a new one.

### What is the difference between a classic and a fine-grained GitHub token?

Classic tokens (`ghp_`) use broad scopes across everything you can access. Fine-grained tokens (`github_pat_`) are limited to one owner, chosen repositories and specific permissions, and GitHub recommends them.

### Do GitHub personal access tokens expire?

Both kinds let you pick an expiration at creation. Fine-grained tokens can be set to never expire unless an organization policy forbids it, and GitHub removes tokens unused for a year.

### How do I log in to GitHub from the command line with a token?

Run `gh auth login`, or set `GH_TOKEN` for headless use. For plain Git over HTTPS, use the token as the password.

### Can an AI agent use my GitHub key without seeing it?

Yes, if the key never enters the agent's context. Instead of pasting the key into a chat or leaving it in a `.env` file the agent can read, keep it in a credential broker that injects it into the command or request on the agent's behalf. BotPassword does this with `list_credentials`, `run_with`, `http_request`, so the agent gets the result and never the value.

## Sources

- https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens
- https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/about-authentication-to-github
- https://cli.github.com/manual/gh_auth_login
- https://docs.github.com/en/rest/authentication/authenticating-to-the-rest-api
- https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/generating-an-installation-access-token-for-a-github-app
- https://docs.github.com/en/code-security/secret-scanning/introduction/about-secret-scanning
- https://github.com/github/github-mcp-server
