Export Audit Logs

#### Allowed For * Admins on accounts that have audit log access

Script zendesk Verified

by hugo697 ยท 11/7/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 377 days ago
1
type Zendesk = {
2
  username: string;
3
  password: string;
4
  subdomain: string;
5
};
6
/**
7
 * Export Audit Logs
8
 * #### Allowed For
9

10
* Admins on accounts that have audit log access
11

12
 */
13
export async function main(
14
  auth: Zendesk,
15
  filter_source_type_: string | undefined,
16
  filter_source_id_: string | undefined,
17
  filter_actor_id_: string | undefined,
18
  filter_ip_address_: string | undefined,
19
  filter_created_at_: string | undefined,
20
  filter_action_: string | undefined
21
) {
22
  const url = new URL(
23
    `https://${auth.subdomain}.zendesk.com/api/v2/audit_logs/export`
24
  );
25
  for (const [k, v] of [
26
    ["filter[source_type]", filter_source_type_],
27
    ["filter[source_id]", filter_source_id_],
28
    ["filter[actor_id]", filter_actor_id_],
29
    ["filter[ip_address]", filter_ip_address_],
30
    ["filter[created_at]", filter_created_at_],
31
    ["filter[action]", filter_action_],
32
  ]) {
33
    if (v !== undefined && v !== "") {
34
      url.searchParams.append(k, v);
35
    }
36
  }
37
  const response = await fetch(url, {
38
    method: "POST",
39
    headers: {
40
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
41
    },
42
    body: undefined,
43
  });
44
  if (!response.ok) {
45
    const text = await response.text();
46
    throw new Error(`${response.status} ${text}`);
47
  }
48
  return await response.text();
49
}
50