Create key
One script reply has been approved by the moderators Verified

Creates new API key.

Created by hugo697 141 days ago
Submitted by hugo697 Bun
Verified 141 days ago
1
//native
2
type Clickhouse = {
3
  username: string;
4
  password: string;
5
  host: string;
6
};
7
/**
8
 * Create key
9
 * Creates new API key.
10
 */
11
export async function main(
12
  auth: Clickhouse,
13
  organizationId: string,
14
  body: {
15
    name?: string;
16
    expireAt?: string;
17
    state?: "enabled" | "disabled";
18
    hashData?: {
19
      keyIdHash?: string;
20
      keyIdSuffix?: string;
21
      keySecretHash?: string;
22
    };
23
    roles?: "admin" | "developer" | "org_member" | "billing"[];
24
  }
25
) {
26
  const url = new URL(
27
    `https://api.clickhouse.cloud/v1/organizations/${organizationId}/keys`
28
  );
29

30
  const response = await fetch(url, {
31
    method: "POST",
32
    headers: {
33
      "Content-Type": "application/json",
34
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
35
    },
36
    body: JSON.stringify(body),
37
  });
38
  if (!response.ok) {
39
    const text = await response.text();
40
    throw new Error(`${response.status} ${text}`);
41
  }
42
  return await response.json();
43
}
44