Create list items

Appends new items to the list. This operation is asynchronous. To get current the operation status, invoke the [Get bulk operation status](#lists-get-bulk-operation-status) endpoint with the returned `operation_id`.

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
 * Create list items
8
 * Appends new items to the list.
9

10
This operation is asynchronous. To get current the operation status, invoke the [Get bulk operation status](#lists-get-bulk-operation-status) endpoint with the returned `operation_id`.
11
 */
12
export async function main(
13
  auth: Cloudflare,
14
  list_id: string,
15
  account_identifier: string,
16
  body: {
17
    asn?: number;
18
    comment?: string;
19
    hostname?: { url_hostname: string; [k: string]: unknown };
20
    ip?: string;
21
    redirect?: {
22
      include_subdomains?: boolean;
23
      preserve_path_suffix?: boolean;
24
      preserve_query_string?: boolean;
25
      source_url: string;
26
      status_code?: 301 | 302 | 307 | 308;
27
      subpath_matching?: boolean;
28
      target_url: string;
29
      [k: string]: unknown;
30
    };
31
    [k: string]: unknown;
32
  }[]
33
) {
34
  const url = new URL(
35
    `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/rules/lists/${list_id}/items`
36
  );
37

38
  const response = await fetch(url, {
39
    method: "POST",
40
    headers: {
41
      "X-AUTH-EMAIL": auth.email,
42
      "X-AUTH-KEY": auth.key,
43
      "Content-Type": "application/json",
44
      Authorization: "Bearer " + auth.token,
45
    },
46
    body: JSON.stringify(body),
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