Create a device settings profile

Creates a device settings profile to be applied to certain devices matching the criteria.

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 a device settings profile
8
 * Creates a device settings profile to be applied to certain devices matching the criteria.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  identifier: string,
13
  body: {
14
    allow_mode_switch?: boolean;
15
    allow_updates?: boolean;
16
    allowed_to_leave?: boolean;
17
    auto_connect?: number;
18
    captive_portal?: number;
19
    description?: string;
20
    disable_auto_fallback?: boolean;
21
    enabled?: boolean;
22
    exclude_office_ips?: boolean;
23
    match: string;
24
    name: string;
25
    precedence: number;
26
    service_mode_v2?: { mode?: string; port?: number; [k: string]: unknown };
27
    support_url?: string;
28
    switch_locked?: boolean;
29
    [k: string]: unknown;
30
  }
31
) {
32
  const url = new URL(
33
    `https://api.cloudflare.com/client/v4/accounts/${identifier}/devices/policy`
34
  );
35

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