0
Create an installation access token for an app
One script reply has been approved by the moderators Verified

Creates an installation access token that enables a GitHub App to make authenticated API requests for the app's installation on an organization or individual account.

Created by hugo697 276 days ago Viewed 8944 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 276 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * Create an installation access token for an app
6
 * Creates an installation access token that enables a GitHub App to make authenticated API requests for the app's installation on an organization or individual account.
7
 */
8
export async function main(
9
  auth: Github,
10
  installation_id: string,
11
  body: {
12
    permissions?: {
13
      actions?: "read" | "write";
14
      administration?: "read" | "write";
15
      checks?: "read" | "write";
16
      contents?: "read" | "write";
17
      deployments?: "read" | "write";
18
      environments?: "read" | "write";
19
      issues?: "read" | "write";
20
      members?: "read" | "write";
21
      metadata?: "read" | "write";
22
      organization_administration?: "read" | "write";
23
      organization_announcement_banners?: "read" | "write";
24
      organization_custom_roles?: "read" | "write";
25
      organization_hooks?: "read" | "write";
26
      organization_packages?: "read" | "write";
27
      organization_plan?: "read";
28
      organization_projects?: "read" | "write" | "admin";
29
      organization_secrets?: "read" | "write";
30
      organization_self_hosted_runners?: "read" | "write";
31
      organization_user_blocking?: "read" | "write";
32
      packages?: "read" | "write";
33
      pages?: "read" | "write";
34
      pull_requests?: "read" | "write";
35
      repository_announcement_banners?: "read" | "write";
36
      repository_hooks?: "read" | "write";
37
      repository_projects?: "read" | "write" | "admin";
38
      secret_scanning_alerts?: "read" | "write";
39
      secrets?: "read" | "write";
40
      security_events?: "read" | "write";
41
      single_file?: "read" | "write";
42
      statuses?: "read" | "write";
43
      team_discussions?: "read" | "write";
44
      vulnerability_alerts?: "read" | "write";
45
      workflows?: "write";
46
      [k: string]: unknown;
47
    };
48
    repositories?: string[];
49
    repository_ids?: number[];
50
    [k: string]: unknown;
51
  }
52
) {
53
  const url = new URL(
54
    `https://api.github.com/app/installations/${installation_id}/access_tokens`
55
  );
56

57
  const response = await fetch(url, {
58
    method: "POST",
59
    headers: {
60
      "Content-Type": "application/json",
61
      Authorization: "Bearer " + auth.token,
62
    },
63
    body: JSON.stringify(body),
64
  });
65
  if (!response.ok) {
66
    const text = await response.text();
67
    throw new Error(`${response.status} ${text}`);
68
  }
69
  return await response.json();
70
}
71