0

Update an API key

by
Published Apr 8, 2025

Updates an existing API key.

Script persona Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Persona = {
3
  apiKey: string;
4
};
5
/**
6
 * Update an API key
7
 * Updates an existing API key.
8
 */
9
export async function main(
10
  auth: Persona,
11
  api_key_id: string,
12
  body: {
13
    data?: {
14
      attributes?: {
15
        name?: string;
16
        note?: string;
17
        "api-version"?: string;
18
        "api-key-inflection"?: string;
19
        "api-attributes-blocklist"?: string[];
20
        "ip-address-allowlist"?: string[];
21
        permissions?: string[];
22
        "file-access-token-expires-in"?: number;
23
      };
24
    };
25
  },
26
  include?: string,
27
  fields?: string,
28
  Key_Inflection?: string,
29
  Idempotency_Key?: string,
30
  Persona_Version?: string,
31
) {
32
  const url = new URL(
33
    `https://api.withpersona.com/api/v1/api-keys/${api_key_id}`,
34
  );
35
  for (const [k, v] of [
36
    ["include", include],
37
    ["fields", fields],
38
  ]) {
39
    if (v !== undefined && v !== "" && k !== undefined) {
40
      url.searchParams.append(k, v);
41
    }
42
  }
43
  const headers: Record<string, string> = {
44
    Authorization: `Bearer ${auth.apiKey}`,
45
    "Content-Type": "application/json",
46
  };
47
  if (Key_Inflection) {
48
    headers["Key-Inflection"] = Key_Inflection;
49
  }
50
  if (Idempotency_Key) {
51
    headers["Idempotency-Key"] = Idempotency_Key;
52
  }
53
  if (Persona_Version) {
54
    headers["Persona-Version"] = Persona_Version;
55
  }
56
  const response = await fetch(url, {
57
    method: "PATCH",
58
    headers,
59
    body: JSON.stringify(body),
60
  });
61
  if (!response.ok) {
62
    const text = await response.text();
63
    throw new Error(`${response.status} ${text}`);
64
  }
65
  return await response.json();
66
}
67