List Client Certificates

List all of your Zone's API Shield mTLS Client Certificates by Status and/or using Pagination

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
 * List Client Certificates
8
 * List all of your Zone's API Shield mTLS Client Certificates by Status and/or using Pagination
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  zone_identifier: string,
13
  status:
14
    | "all"
15
    | "active"
16
    | "pending_reactivation"
17
    | "pending_revocation"
18
    | "revoked"
19
    | undefined,
20
  page: string | undefined,
21
  per_page: string | undefined,
22
  limit: string | undefined,
23
  offset: string | undefined
24
) {
25
  const url = new URL(
26
    `https://api.cloudflare.com/client/v4/zones/${zone_identifier}/client_certificates`
27
  );
28
  for (const [k, v] of [
29
    ["status", status],
30
    ["page", page],
31
    ["per_page", per_page],
32
    ["limit", limit],
33
    ["offset", offset],
34
  ]) {
35
    if (v !== undefined && v !== "") {
36
      url.searchParams.append(k, v);
37
    }
38
  }
39
  const response = await fetch(url, {
40
    method: "GET",
41
    headers: {
42
      "X-AUTH-EMAIL": auth.email,
43
      "X-AUTH-KEY": auth.key,
44
      Authorization: "Bearer " + auth.token,
45
    },
46
    body: undefined,
47
  });
48
  if (!response.ok) {
49
    const text = await response.text();
50
    throw new Error(`${response.status} ${text}`);
51
  }
52
  return await response.json();
53
}
54