Change Challenge TTL setting

Specify how long a visitor is allowed access to your site after successfully completing a challenge (such as a CAPTCHA). After the TTL has expired the visitor will have to complete a new challenge. We recommend a 15 - 45 minute setting and will attempt to honor any setting above 45 minutes. (https://support.cloudflare.com/hc/en-us/articles/200170136).

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
 * Change Challenge TTL setting
8
 * Specify how long a visitor is allowed access to your site after successfully completing a challenge (such as a CAPTCHA). After the TTL has expired the visitor will have to complete a new challenge. We recommend a 15 - 45 minute setting and will attempt to honor any setting above 45 minutes. (https://support.cloudflare.com/hc/en-us/articles/200170136).
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  zone_identifier: string,
13
  body: {
14
    value:
15
      | 300
16
      | 900
17
      | 1800
18
      | 2700
19
      | 3600
20
      | 7200
21
      | 10800
22
      | 14400
23
      | 28800
24
      | 57600
25
      | 86400
26
      | 604800
27
      | 2592000
28
      | 31536000;
29
    [k: string]: unknown;
30
  }
31
) {
32
  const url = new URL(
33
    `https://api.cloudflare.com/client/v4/zones/${zone_identifier}/settings/challenge_ttl`
34
  );
35

36
  const response = await fetch(url, {
37
    method: "PATCH",
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