0

Create a service account

by
Published Oct 17, 2025

Can be used by the following roles assigned at the organization scope: - ORG_ADMIN - CLUSTER_ADMIN

Script cockroachdb Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Cockroachdb = {
3
  token: string;
4
};
5
/**
6
 * Create a service account
7
 * Can be used by the following roles assigned at the organization scope:
8
- ORG_ADMIN
9
- CLUSTER_ADMIN
10

11
 */
12
export async function main(
13
  auth: Cockroachdb,
14
  body: {
15
    description: string;
16
    name: string;
17
    roles: {
18
      name:
19
        | "BILLING_COORDINATOR"
20
        | "ORG_ADMIN"
21
        | "ORG_MEMBER"
22
        | "CLUSTER_ADMIN"
23
        | "CLUSTER_OPERATOR_WRITER"
24
        | "CLUSTER_DEVELOPER"
25
        | "CLUSTER_CREATOR"
26
        | "FOLDER_ADMIN"
27
        | "FOLDER_MOVER";
28
      resource: { id?: string; type: "ORGANIZATION" | "CLUSTER" | "FOLDER" };
29
    }[];
30
  },
31
) {
32
  const url = new URL(`https://cockroachlabs.cloud/api/v1/service-accounts`);
33

34
  const response = await fetch(url, {
35
    method: "POST",
36
    headers: {
37
      "Content-Type": "application/json",
38
      Authorization: "Bearer " + auth.token,
39
    },
40
    body: JSON.stringify(body),
41
  });
42
  if (!response.ok) {
43
    const text = await response.text();
44
    throw new Error(`${response.status} ${text}`);
45
  }
46
  return await response.json();
47
}
48