Sub topic 11
Creating and using installation access tokens
This chapter assumes you can sign an app JWT (covered in the previous chapter), know what an installation is, and are comfortable with curl and environment variables. It shows how to trade the JWT for an installation access token and how to use that token.
Why a second token is needed
The app JWT proves you are the app. It can only call a small set of endpoints under /app, such as listing installations. It cannot read a repository's issues or contents.
To act inside an account where the app is installed, you exchange the JWT for an installation access token. This token has these properties:
- It is limited to the permissions the installation was granted.
- It covers only the repositories the installation can access.
- It expires after one hour.
Creating the token with the REST API
You need the installation ID. You can find it in the URL of the installation's settings page, in webhook payloads, or by calling GET /app/installations with the JWT. Then send a POST request:
curl -L -X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $APP_JWT" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/app/installations/$INSTALLATION_ID/access_tokensThe response contains a token, an expires_at timestamp, the granted permissions, and a repository_selection value. Use the token as a bearer token, in the same way as a personal access token:
curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $INSTALLATION_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/installation/repositoriesNarrowing the token further
You can ask for less than the installation holds. The request body accepts a repositories list and a permissions object. GitHub returns an error if you ask for more than the installation was granted.
curl -L -X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $APP_JWT" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/app/installations/$INSTALLATION_ID/access_tokens \
-d '{"repositories":["hello-world"],"permissions":{"issues":"write"}}'Use this for jobs that need only one repository or one permission. If the token leaks, less is exposed.
Letting Octokit do the work
The Octokit App class signs the JWT, requests the installation token, and refreshes it when it expires:
import { App } from "octokit";
import fs from "node:fs";
const app = new App({
appId: process.env.GITHUB_APP_ID,
privateKey: fs.readFileSync(process.env.GITHUB_APP_PRIVATE_KEY_PATH, "utf8"),
});
const octokit = await app.getInstallationOctokit(Number(process.env.INSTALLATION_ID));
const { data } = await octokit.request("GET /installation/repositories");
console.log(data.repositories.map((r) => r.full_name));Run this with GITHUB_APP_ID, GITHUB_APP_PRIVATE_KEY_PATH and INSTALLATION_ID set. It prints the full name of each repository the installation can reach.
Common errors
| Status | Likely cause |
|---|---|
| 401 on the token request | The JWT is expired, signed with the wrong key, or has the wrong `iss`. Check your clock. |
| 404 on the token request | The installation ID is wrong, or the app is no longer installed. |
| 403 or 404 on a repository call | The installation lacks that permission, or the repository is outside its selection. |
| 401 after about an hour | The installation token expired. Request a new one. |
Exercise
A job must only comment on issues in one repository, but the app is installed on twenty repositories with several permissions. How do you create a suitable token, and what happens if you request issues: write when the installation only has issues: read?
Show answer
Send the access_tokens request with a body such as {"repositories":["that-repo"],"permissions":{"issues":"write"}}. This gives a token limited to that repository and that permission. If the installation only has issues: read, GitHub rejects the request with an error. A token can never exceed what the installation was granted.