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

Updates API key properties.

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
 * Update key
9
 * Updates API key properties.
10
 */
11
export async function main(
12
  auth: Clickhouse,
13
  organizationId: string,
14
  keyId: string,
15
  body: {
16
    name?: string;
17
    roles?: "admin" | "developer" | "org_member" | "billing"[];
18
    expireAt?: string;
19
    state?: "enabled" | "disabled";
20
  }
21
) {
22
  const url = new URL(
23
    `https://api.clickhouse.cloud/v1/organizations/${organizationId}/keys/${keyId}`
24
  );
25

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