Update Many Routes

Update multiple Magic static routes. Use `?validate_only=true` as an optional query parameter to run validation only without persisting changes. Only fields for a route that need to be changed need be provided.

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
 * Update Many Routes
8
 * Update multiple Magic static routes. Use `?validate_only=true` as an optional query parameter to run validation only without persisting changes. Only fields for a route that need to be changed need be provided.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  account_identifier: string,
13
  body: {
14
    routes: ({ id: string; [k: string]: unknown } & {
15
      description?: string;
16
      nexthop: string;
17
      prefix: string;
18
      priority: number;
19
      scope?: {
20
        colo_names?: string[];
21
        colo_regions?: string[];
22
        [k: string]: unknown;
23
      };
24
      weight?: number;
25
      [k: string]: unknown;
26
    })[];
27
    [k: string]: unknown;
28
  }
29
) {
30
  const url = new URL(
31
    `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/magic/routes`
32
  );
33

34
  const response = await fetch(url, {
35
    method: "PUT",
36
    headers: {
37
      "X-AUTH-EMAIL": auth.email,
38
      "X-AUTH-KEY": auth.key,
39
      "Content-Type": "application/json",
40
      Authorization: "Bearer " + auth.token,
41
    },
42
    body: JSON.stringify(body),
43
  });
44
  if (!response.ok) {
45
    const text = await response.text();
46
    throw new Error(`${response.status} ${text}`);
47
  }
48
  return await response.json();
49
}
50