0
List Tokens
One script reply has been approved by the moderators Verified

List all access tokens you created.

Created by hugo697 254 days ago Viewed 8904 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 254 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * List Tokens
8
 * List all access tokens you created.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  page: string | undefined,
13
  per_page: string | undefined,
14
  direction: "asc" | "desc" | undefined
15
) {
16
  const url = new URL(`https://api.cloudflare.com/client/v4/user/tokens`);
17
  for (const [k, v] of [
18
    ["page", page],
19
    ["per_page", per_page],
20
    ["direction", direction],
21
  ]) {
22
    if (v !== undefined && v !== "") {
23
      url.searchParams.append(k, v);
24
    }
25
  }
26
  const response = await fetch(url, {
27
    method: "GET",
28
    headers: {
29
      "X-AUTH-EMAIL": auth.email,
30
      "X-AUTH-KEY": auth.key,
31
      Authorization: "Bearer " + auth.token,
32
    },
33
    body: undefined,
34
  });
35
  if (!response.ok) {
36
    const text = await response.text();
37
    throw new Error(`${response.status} ${text}`);
38
  }
39
  return await response.json();
40
}
41