0

List all API Logs

by
Published Apr 8, 2025

Returns a list of your organization's API Logs.

Script persona Verified

The script

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

52
function encodeParams(o: any) {
53
  function iter(o: any, path: string) {
54
    if (Array.isArray(o)) {
55
      o.forEach(function (a) {
56
        iter(a, path + "[]");
57
      });
58
      return;
59
    }
60
    if (o !== null && typeof o === "object") {
61
      Object.keys(o).forEach(function (k) {
62
        iter(o[k], path + "[" + k + "]");
63
      });
64
      return;
65
    }
66
    data.push(path + "=" + o);
67
  }
68
  const data: string[] = [];
69
  Object.keys(o).forEach(function (k) {
70
    if (o[k] !== undefined) {
71
      iter(o[k], k);
72
    }
73
  });
74
  return new URLSearchParams(data.join("&"));
75
}
76