0

Create an organization invitation

by
Published Oct 25, 2023

Invite people to an organization by using their GitHub user ID or their email address.

Script github Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 398 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * Create an organization invitation
6
 * Invite people to an organization by using their GitHub user ID or their email address.
7
 */
8
export async function main(
9
  auth: Github,
10
  org: string,
11
  body: {
12
    email?: string;
13
    invitee_id?: number;
14
    role?: "admin" | "direct_member" | "billing_manager";
15
    team_ids?: number[];
16
    [k: string]: unknown;
17
  }
18
) {
19
  const url = new URL(`https://api.github.com/orgs/${org}/invitations`);
20

21
  const response = await fetch(url, {
22
    method: "POST",
23
    headers: {
24
      "Content-Type": "application/json",
25
      Authorization: "Bearer " + auth.token,
26
    },
27
    body: JSON.stringify(body),
28
  });
29
  if (!response.ok) {
30
    const text = await response.text();
31
    throw new Error(`${response.status} ${text}`);
32
  }
33
  return await response.json();
34
}
35