List images V2

List up to 10000 images with one request. Use the optional parameters below to get a specific range of images. Endpoint returns continuation_token if more images are present.

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 images V2
8
 * List up to 10000 images with one request. Use the optional parameters below to get a specific range of images.
9
Endpoint returns continuation_token if more images are present.
10

11
 */
12
export async function main(
13
  auth: Cloudflare,
14
  account_identifier: string,
15
  continuation_token: string | undefined,
16
  per_page: string | undefined,
17
  sort_order: "asc" | "desc" | undefined
18
) {
19
  const url = new URL(
20
    `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/images/v2`
21
  );
22
  for (const [k, v] of [
23
    ["continuation_token", continuation_token],
24
    ["per_page", per_page],
25
    ["sort_order", sort_order],
26
  ]) {
27
    if (v !== undefined && v !== "") {
28
      url.searchParams.append(k, v);
29
    }
30
  }
31
  const response = await fetch(url, {
32
    method: "GET",
33
    headers: {
34
      "X-AUTH-EMAIL": auth.email,
35
      "X-AUTH-KEY": auth.key,
36
      Authorization: "Bearer " + auth.token,
37
    },
38
    body: undefined,
39
  });
40
  if (!response.ok) {
41
    const text = await response.text();
42
    throw new Error(`${response.status} ${text}`);
43
  }
44
  return await response.json();
45
}
46