0

Get account audit logs

by
Published Nov 16, 2023

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

Script cloudflare Verified

The script

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