Sub topic 7
What a GitHub App is
A GitHub App is an integration registered with GitHub that has its own identity, its own permissions, and its own credentials. A fine-grained personal access token (PAT) borrows a person's identity. A GitHub App does not.
This chapter assumes you have read the earlier chapters on bearer tokens and fine-grained PATs, and that you can call a REST API from a terminal. It covers what an app is, what you get when you register one, and how it differs from a token and from an OAuth app. Installation details, permission settings, JWT signing, and installation tokens each get their own chapter, so this one only previews them.
The problem apps solve
A fine-grained PAT works well for a script that you run as yourself. It has weaknesses when the caller is a service:
- The token acts as a person. Commits, comments, and API calls show up under that person's name.
- The token stops working or loses access if that person leaves the organization or changes roles.
- Each token is created by hand, and someone has to remember to renew it.
- One token belongs to one resource owner, so serving many organizations means many tokens.
A GitHub App separates the integration from any one person. The app is a first-class actor. It can be installed by many accounts, each with its own repository selection, and it gets short-lived tokens on demand.
What you create when you register an app
You register an app under a personal account or an organization (Settings, then Developer settings, then GitHub Apps, then New GitHub App). The owner controls the app's settings. It does not decide which repositories the app can reach. That is decided later, per installation.
Registration gives you these pieces:
| Item | What it is | Secret? |
|---|---|---|
| App ID | Numeric identifier of the app. Used as the `iss` claim of the JWT. | No |
| Client ID | Identifier used in the OAuth-style flow for user access tokens. | No |
| Slug and name | Public name and URL-friendly form. The bot account appears as `slug[bot]`. | No |
| Private key | An RSA key pair half you download once. GitHub keeps only the public half. | Yes |
| Client secret | Used to exchange a user authorization code for a user token. | Yes |
| Webhook secret | Lets your server verify that a webhook delivery came from GitHub. | Yes |
| Permissions | The maximum access the app can ask for, per resource type. | No |
| Subscribed events | The webhook events GitHub sends to your app. | No |
GitHub shows the private key only when you generate it. If you lose it, you generate a new one. You can have more than one key at a time, which makes rotation possible without downtime. The hygiene chapter covers this.
The app also has a visibility setting that decides who can install it. "Only on this account" keeps it private to the owner. "Any account" makes it installable by anyone, which is what you want for a product you ship to others.
The three kinds of credential an app uses
An app never uses a long-lived password-like token. It moves between three credentials, each with a different purpose:
- App JWT. A JSON Web Token (JWT) is a signed, self-describing token. You sign it with the app's private key. It proves "I am this app" and is valid for at most 10 minutes. It can call only the endpoints that are about the app itself, such as listing its installations.
- Installation access token. You exchange the JWT for this token to act inside one installation. It expires after one hour and is limited to the repositories and permissions of that installation. This is what most automation uses.
- User access token. If the app needs to act on behalf of a signed-in user, the user authorizes the app and you receive a token limited to what both the app and the user can do.
The following diagram shows how the pieces relate.
Notice that the app is registered once, but each installation gets its own token. A token for installation 1 cannot touch the repositories of installation 2.
Permissions and events at a glance
An app declares two things at registration:
- Permissions are what the app can do through the API, for example read contents or write issues. They use the same named permission model as fine-grained PATs, with access levels of none, read, and write. The app's declared permissions are a ceiling. An installation can grant that set, and a token request can ask for less.
- Events are what GitHub tells the app about by sending a webhook (an HTTP POST to your server) when something happens, such as a pull request opening.
A webhook can only be delivered for an event if the app holds the permission that the event requires. The app permissions and events chapter explains this link in detail.
Public information about an app
Anyone can read the public metadata of a public app without authentication. This is a convenient way to confirm the slug, owner, and declared permissions before you install something.
curl -L \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/apps/APP_SLUGReplace APP_SLUG with the slug from the app's URL. The response includes fields such as id, slug, name, owner, permissions, and events.
To call an endpoint that is about your own app, such as GET /app, you need the JWT from the next chapters. Without it you get a 401.
How an app differs from a PAT and an OAuth app
| Property | Fine-grained PAT | OAuth app | GitHub App |
|---|---|---|---|
| Acts as | The user who created it | The user who authorized it | The app itself, or a user if the user authorizes it |
| Access granularity | Per repository and per permission | Broad OAuth scopes, such as `repo` | Per repository and per permission |
| Token lifetime | Chosen at creation, can be set to expire | Does not expire unless revoked | 1 hour for installation tokens, up to 10 minutes for the JWT |
| Tied to a person's membership | Yes | Yes | No for installation tokens |
| Webhooks | No | Limited to repository webhooks you set up | Built in, per app |
| Installable by many accounts | No | Authorized by many users | Yes |
| Appears in activity as | The user | The user | `slug[bot]` |
An OAuth app is the older model. It requests coarse scopes, and a scope like repo grants access to every repository the user can reach. For new integrations GitHub recommends GitHub Apps, because the narrower permissions limit the damage from a leaked credential.
Behaviors that surprise people
- The bot identity is separate. Actions taken with an installation token are attributed to
slug[bot]. Commits are not attributed to a person unless you set the author yourself. - Rate limits are per installation. Installation tokens get their own limit, so many installations do not drain a single shared budget. The limit grows with the number of repositories and users in the installation, and the exact figures differ by plan, so check the current rate limit documentation.
- Some endpoints do not accept apps. A few APIs still require a user token, and a few account-level actions are not available to apps. Check the endpoint's documentation, which lists whether it supports GitHub App tokens.
- A changed permission needs approval. If you add a permission to the app later, each installation owner must accept it before the installation gains it. Until then, calls that need it fail with
403. - Webhooks are optional. An app that only runs on a schedule, such as a nightly job, can leave webhooks off and only use the API.
Common errors
401 Bad credentialsonGET /app. The JWT is malformed, expired, or signed with the wrong key. Check thatissis the App ID and that the clock on your machine is correct.404when using an installation token. The repository is not in the installation's selection, or the app lacks the permission. GitHub returns404instead of403for some private resources so it does not confirm that they exist.403 Resource not accessible by integration. The app or the installation does not have the permission the endpoint needs.- Using the JWT for repository calls. The JWT is only for app-level endpoints. Exchange it for an installation token first.
What comes next
The next chapters follow the order you meet these pieces in practice. You will see how an account installs the app and picks repositories, how permissions and events are configured, how to sign the JWT, and how to exchange it for an installation token.
Exercise
A teammate's automation uses a fine-grained PAT that they created for the whole team. They are leaving the company next week. Give two reasons a GitHub App would be a better fit, and name the credential the app would use to call the repository API.
Show answer
The PAT acts as the teammate, so the automation can lose access or break when their membership ends, and its activity is attributed to them. The token also has to be renewed by hand. A GitHub App has its own identity (slug[bot]), is not tied to one person's membership, and gets short-lived tokens on demand. For repository API calls the app uses an installation access token, which it gets by signing a JWT with its private key and exchanging that JWT.
Exercise
You call GET https://api.github.com/app with an installation access token and get a 401. What is the most likely cause?
Show answer
GET /app is an app-level endpoint and requires the app JWT, not an installation token. Sign a JWT with the private key, with iss set to the App ID, and send it as the bearer token.