Get user audit logs

Gets a list of audit logs for a user account. Can be filtered by who made the change, on which zone, and the timeframe of the change.

Script cloudflare Verified

by hugo697 ยท 11/16/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 383 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Get user audit logs
8
 * Gets a list of audit logs for a user account. Can be filtered by who made the change, on which zone, and the timeframe of the change.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  id: string | undefined,
13
  _export: string | undefined,
14
  action_type: string | undefined,
15
  actor_ip: string | undefined,
16
  actor_email: string | undefined,
17
  since: string | undefined,
18
  before: string | undefined,
19
  zone_name: string | undefined,
20
  direction: "desc" | "asc" | undefined,
21
  per_page: string | undefined,
22
  page: string | undefined,
23
  hide_user_logs: string | undefined
24
) {
25
  const url = new URL(`https://api.cloudflare.com/client/v4/user/audit_logs`);
26
  for (const [k, v] of [
27
    ["id", id],
28
    ["export", _export],
29
    ["action.type", action_type],
30
    ["actor.ip", actor_ip],
31
    ["actor.email", actor_email],
32
    ["since", since],
33
    ["before", before],
34
    ["zone.name", zone_name],
35
    ["direction", direction],
36
    ["per_page", per_page],
37
    ["page", page],
38
    ["hide_user_logs", hide_user_logs],
39
  ]) {
40
    if (v !== undefined && v !== "") {
41
      url.searchParams.append(k, v);
42
    }
43
  }
44
  const response = await fetch(url, {
45
    method: "GET",
46
    headers: {
47
      "X-AUTH-EMAIL": auth.email,
48
      "X-AUTH-KEY": auth.key,
49
      Authorization: "Bearer " + auth.token,
50
    },
51
    body: undefined,
52
  });
53
  if (!response.ok) {
54
    const text = await response.text();
55
    throw new Error(`${response.status} ${text}`);
56
  }
57
  return await response.json();
58
}
59