Sub topic 8
Installing an app on accounts and repositories
This chapter assumes you know what a GitHub App is, how repositories and organizations relate, and how to call the REST API with a bearer token. It covers what an installation is, how repository access is chosen, and how your code finds the installation it needs.
Registration versus installation
Registering a GitHub App creates its identity: an app ID, a private key, a set of requested permissions, and optional webhook settings. The app cannot touch any repository yet. Access begins only when an account installs it.
An installation connects the app to one user account or one organization. Each installation has its own ID, its own repository selection, and its own tokens. One app with 500 customers has 500 installations.
Choosing repository access
When an owner installs the app, GitHub asks for one of two repository scopes:
| Choice | Effect | Typical use |
|---|---|---|
| All repositories | Includes every current and future repository the owner has | Org-wide automation such as labeling |
| Only select repositories | Includes just the repositories picked | Least-privilege setups, trials |
The app's permissions, such as contents: read, are fixed by the app registration. The installer chooses where those permissions apply, not what they are. An installation token can never reach a repository outside this selection, even if the app's permissions would otherwise allow it.
An owner can change the selection later in the installation settings. Adding or removing a repository takes effect immediately for new tokens.
Listing installations from code
Your code often needs an installation ID. Authenticate as the app with a JWT (covered in a later chapter), then call GET /app/installations:
curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $APP_JWT" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/app/installationsEach item in the response includes id, the account that installed it, repository_selection (all or selected), and the granted permissions. For a known organization, GET /orgs/ORG/installation returns the same data for just that account. Repositories have an equivalent, GET /repos/OWNER/REPO/installation.
You can also receive installation IDs without polling. Every webhook payload for an installed app includes an installation object with its id. Store this ID when you receive an installation event with the action created.
Installation lifecycle events
The installation changes over time, and your app should react to these changes.
installationwithcreated: a new account installed the app.installationwithdeleted: the account uninstalled it. Discard stored data and tokens for it.installationwithsuspendorunsuspend: the owner paused the app. API calls fail until it resumes.installation_repositorieswithaddedorremoved: the repository selection changed.
Common errors
- A
404on a repository usually means the repository is outside the installation's selection, not that it doesn't exist. - A
403with a message about a missing permission means the app was never granted it. Changing the app's permissions in its settings sends owners an approval request, and the installation keeps the old permissions until they accept. - Using the installation ID from a test account in production fails with
404. Each account has its own ID.
Exercise
An app is installed on an organization with "Only select repositories" set to api and web. Your code requests GET /repos/acme/billing with a valid installation token. What do you expect, and what two things could you change to fix it?
Show answer
Expect a 404, because billing is outside the installation's repository selection. An organization owner can add billing to the installation, or switch the installation to "All repositories". Changing the app's registered permissions would not help, since the problem is repository scope.
Exercise
Which webhook event tells your app that an owner added a repository to an existing installation?
Show answer
installation_repositories with the action added.