0

List Organizations

by
Published today

List the organizations the authenticated user belongs to. Private memberships need the read:org scope (classic tokens).

Script github Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 1 hour ago
1
//native
2

3
/**
4
 * List Organizations
5
 * List the organizations the authenticated user belongs to. Private memberships need the read:org scope (classic tokens).
6
 */
7
export async function main(
8
  auth: RT.Github,
9
  per_page: number = 30,
10
  page: number = 1
11
) {
12
  const url = new URL("https://api.github.com/user/orgs")
13
  url.searchParams.append("per_page", String(per_page))
14
  url.searchParams.append("page", String(page))
15

16
  const response = await fetch(url, {
17
    headers: {
18
      Authorization: `Bearer ${auth.token}`,
19
      Accept: "application/vnd.github+json",
20
      "X-GitHub-Api-Version": "2022-11-28",
21
    },
22
  })
23

24
  if (!response.ok) {
25
    throw new Error(`${response.status} ${await response.text()}`)
26
  }
27

28
  return await response.json()
29
}
30