0

List organizations

by
Published Oct 17, 2025

When using a service token, returns the list of organizations the service token has access to. When using an OAuth token, returns the list of organizations the user has access to. ### Authorization A OAuth token must have at least one of the following scopes in order to use this API endpoint: **OAuth Scopes** | Resource | Scopes | | :------- | :---------- | | User | `read_organizations` |

Script planetscale Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Planetscale = {
3
  serviceTokenId: string;
4
  serviceToken: string;
5
};
6
/**
7
 * List organizations
8
 * When using a service token, returns the list of organizations the service token has access to. When using an OAuth token, returns the list of organizations the user has access to.
9
### Authorization
10
A   OAuth token must have at least one of the following   scopes in order to use this API endpoint:
11

12
**OAuth Scopes**
13

14
 | Resource | Scopes |
15
| :------- | :---------- |
16
| User | `read_organizations` |
17
 */
18
export async function main(
19
  auth: Planetscale,
20
  page: string | undefined,
21
  per_page: string | undefined,
22
) {
23
  const url = new URL(`https://api.planetscale.com/v1/organizations`);
24
  for (const [k, v] of [
25
    ["page", page],
26
    ["per_page", per_page],
27
  ]) {
28
    if (v !== undefined && v !== "" && k !== undefined) {
29
      url.searchParams.append(k, v);
30
    }
31
  }
32
  const response = await fetch(url, {
33
    method: "GET",
34
    headers: {
35
      Authorization: `${auth.serviceTokenId}:${auth.serviceToken}`,
36
    },
37
    body: undefined,
38
  });
39
  if (!response.ok) {
40
    const text = await response.text();
41
    throw new Error(`${response.status} ${text}`);
42
  }
43
  return await response.json();
44
}
45