Sub topic 8
Installing an app on accounts and repositories
This chapter assumes you know what a GitHub App is, the difference between an app and its installations at a high level, and how to call the REST API with a bearer token. It also assumes you can work in a terminal with environment variables. Here you will learn what an installation is, how repository access is chosen and changed, and how to list and inspect installations from code.
Registration and installation are different things
A GitHub App has two layers.
- The registration is the app itself. It has an app ID, a name, a private key, a set of requested permissions, and a list of webhook events it wants. It exists once, and it is owned by one user or organization.
- An installation is the app placed on one account. That account can be a user or an organization. Each installation has its own installation ID.
The registration says what the app is allowed to ask for. The installation says where it can act. An app with no installations can authenticate as itself, but it cannot read or write any repository.
One app can have many installations. A popular CI app might be installed on thousands of organizations, and each one is a separate installation with a separate ID, a separate set of repositories, and separate tokens.
Who can install an app
Who can install depends on the account type and on the app's visibility.
- Personal accounts: the account owner can install the app on their own account.
- Organizations: an organization owner can install any app. A member who is not an owner can usually only request an installation, and an owner then approves it. Organization members with the app manager role can manage the app registration but do not automatically get to install it everywhere.
- Public and private apps: a public app can be installed by any account. A private app can only be installed by the account that owns it.
Because the installer sees the permissions on the consent screen, an owner approves the whole permission set at once. The app cannot use more than the registered permissions, and the installer cannot grant more than the app asks for.
Choosing repository access
When someone installs the app, GitHub asks which repositories the installation can reach. There are two choices.
| Choice | What the app can reach | When it fits |
|---|---|---|
| All repositories | Every repository the account owns now, plus any it creates later | Org-wide tooling such as a policy checker |
| Only select repositories | A list the installer picks | Tools that serve a few projects, or a trial run |
The all-repositories choice is convenient and carries a risk. Because it includes future repositories, a new private repository is covered automatically, with no one deciding that it should be. For most apps, prefer the selected list and widen it deliberately.
The repository choice combines with the app's permissions. If the app has contents: read and the installation covers two repositories, the app can read the contents of those two repositories and no others. It cannot read a third repository even if it is public to the world with other credentials, because the installation token is limited to the installation's repositories.
Two more details matter in practice:
- Some permissions apply to the account rather than to repositories, such as organization-level permissions like reading members. These work for the installation as a whole and do not depend on the repository list.
- Repositories owned by other accounts are never included. An installation on
acmecannot reachglobex/api. If your tool must work in both, it needs two installations.
Changing an installation later
Installers can change access at any time. Go to the account's Settings, then Applications (for a personal account) or Installed GitHub Apps (for an organization), and choose Configure next to the app. From there you can:
- switch between all repositories and selected repositories,
- add or remove repositories from the selected list,
- suspend the installation, which stops tokens from working without deleting it,
- uninstall the app, which deletes the installation.
An app also learns about these changes from webhooks. The installation event fires when someone installs, uninstalls, suspends, or unsuspends. The installation_repositories event fires when repositories are added or removed. A well-behaved app listens for both so that its own records match GitHub's.
Permission changes and re-approval
An app owner can edit the registered permissions after installation. If the new set asks for more access, existing installations do not receive it automatically. GitHub sends the installer a request to accept the new permissions. Until an owner accepts, the installation keeps working with the old set, and requests that need the new permission fail with a 403 response.
This is a safety property. An app cannot widen its own reach after the fact without a human saying yes. When you update an app's permissions, expect some installations to lag behind and handle 403 errors with a clear message.
Finding installations from code
To list the installations of your app, authenticate as the app with a JWT (covered in a later chapter) and call GET /app/installations. The installation tokens you create later are for working inside one installation. The app JWT is for managing installations.
import fs from "node:fs";
import { App } from "octokit";
const app = new App({
appId: process.env.GITHUB_APP_ID,
privateKey: fs.readFileSync(process.env.GITHUB_APP_PRIVATE_KEY_PATH, "utf8"),
});
const { data: installations } = await app.octokit.request("GET /app/installations");
for (const inst of installations) {
console.log(
inst.id,
inst.account.login,
inst.account.type,
inst.repository_selection
);
}The output has one line per installation. The fields to notice are:
id, the installation ID that you pass when creating a token,account.loginandaccount.type, which tell you whether the installation is on a user or an organization,repository_selection, which isallorselected,suspended_at, which is not null when the installation is suspended.
The list is paginated, so an app with many installations must follow the page links. Octokit provides app.eachInstallation.iterator() for that.
You can also look up one installation by account. GET /orgs/{org}/installation and GET /users/{username}/installation return the installation of your app on that account, or 404 if it is not installed. This is the usual way to get an installation ID when a webhook is not involved.
After you have an installation token, GET /installation/repositories lists the repositories that installation can reach. This is the check to run when a request fails and you want to know whether the repository is simply not in the installation.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `404 Not Found` on a repository you expected to reach | The repository is not in the installation's selected list | Add the repository in the installation settings |
| `404` on `/orgs/{org}/installation` | The app is not installed on that organization | Ask an owner to install it |
| `403 Resource not accessible by integration` | The app lacks the needed permission, or a new permission has not been accepted | Check the registered permissions and the pending approval |
| Token worked yesterday, fails today | The installation was suspended or deleted | Handle the `installation` webhook and check `suspended_at` |
| New repository is not covered | Selection is `selected`, and nobody added it | Add the repository or listen for `repository` events and prompt the owner |
Note that GitHub returns 404 instead of 403 for repositories you cannot see. This prevents leaking which private repositories exist, but it also means a missing repository and a missing grant look alike. Compare against GET /installation/repositories before you debug anything else.
Design guidance
Keep these habits when you build an app that others will install:
- Ask for the smallest permission set that works, since every installer must approve all of it.
- Store installation IDs together with the account login and the repository selection, and update them from webhooks.
- Do not assume that a repository stays available. Handle
404and403as normal outcomes. - Do not cache an installation token past its expiry. Create a new one when needed from the installation ID.
- Make "who can fix this" clear in error messages. Often only an organization owner can change the repository list.
Exercise
Your app is installed on the organization acme with "Only select repositories" set to acme/api. The app has contents: read. A request to read a file in acme/web returns 404. Give two possible reasons, and name the call that lets you check which repositories the installation can reach.
Show answer
The repository acme/web is not in the installation's selected list, so the installation token cannot see it, and GitHub returns 404 instead of 403 to avoid revealing private repositories. A second reason is that the installation was suspended or uninstalled, in which case tokens stop working. To check, create an installation access token and call GET /installation/repositories. To check the installation itself, call GET /orgs/acme/installation with the app JWT and look at repository_selection and suspended_at.
Exercise
The owner of an app adds the issues: write permission to its registration. Existing installations still get 403 Resource not accessible by integration when creating issues. Why, and what has to happen?
Show answer
Increases in permissions are not applied to existing installations automatically. GitHub asks the installer, such as an organization owner, to accept the new permissions. Until that owner approves, the installation keeps the old permission set, so the issue creation fails. Once an owner accepts the request, the installation gains issues: write and the calls succeed.