0

Retrieve an API key

by
Published Apr 8, 2025

Retrieves the information for an existing API key, including its value.

Script persona Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Persona = {
3
  apiKey: string;
4
};
5
/**
6
 * Retrieve an API key
7
 * Retrieves the information for an existing API key, including its value.
8
 */
9
export async function main(
10
  auth: Persona,
11
  api_key_id: string,
12
  include?: string,
13
  fields?: string,
14
  Key_Inflection?: string,
15
  Idempotency_Key?: string,
16
  Persona_Version?: string,
17
) {
18
  const url = new URL(
19
    `https://api.withpersona.com/api/v1/api-keys/${api_key_id}`,
20
  );
21
  for (const [k, v] of [
22
    ["include", include],
23
    ["fields", fields],
24
  ]) {
25
    if (v !== undefined && v !== "" && k !== undefined) {
26
      url.searchParams.append(k, v);
27
    }
28
  }
29
  const headers: Record<string, string> = {
30
    Authorization: `Bearer ${auth.apiKey}`,
31
  };
32
  if (Key_Inflection) {
33
    headers["Key-Inflection"] = Key_Inflection;
34
  }
35
  if (Idempotency_Key) {
36
    headers["Idempotency-Key"] = Idempotency_Key;
37
  }
38
  if (Persona_Version) {
39
    headers["Persona-Version"] = Persona_Version;
40
  }
41
  const response = await fetch(url, {
42
    method: "GET",
43
    headers,
44
    body: undefined,
45
  });
46
  if (!response.ok) {
47
    const text = await response.text();
48
    throw new Error(`${response.status} ${text}`);
49
  }
50
  return await response.json();
51
}
52