Update Zone Subscription

Updates zone subscriptions, either plan or add-ons.

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 Zone Subscription
8
 * Updates zone subscriptions, either plan or add-ons.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  identifier: string,
13
  body: {
14
    app?: { install_id?: string; [k: string]: unknown };
15
    component_values?: {
16
      default?: number;
17
      name?: string;
18
      price?: number;
19
      value?: number;
20
      [k: string]: unknown;
21
    }[];
22
    currency?: string;
23
    current_period_end?: string;
24
    current_period_start?: string;
25
    frequency?: "weekly" | "monthly" | "quarterly" | "yearly";
26
    id?: string;
27
    price?: number;
28
    rate_plan?: {
29
      currency?: string;
30
      externally_managed?: boolean;
31
      id?: { [k: string]: unknown };
32
      is_contract?: boolean;
33
      public_name?: string;
34
      scope?: string;
35
      sets?: string[];
36
      [k: string]: unknown;
37
    };
38
    state?:
39
      | "Trial"
40
      | "Provisioned"
41
      | "Paid"
42
      | "AwaitingPayment"
43
      | "Cancelled"
44
      | "Failed"
45
      | "Expired";
46
    zone?: { id?: string; name?: string; [k: string]: unknown };
47
    [k: string]: unknown;
48
  }
49
) {
50
  const url = new URL(
51
    `https://api.cloudflare.com/client/v4/zones/${identifier}/subscription`
52
  );
53

54
  const response = await fetch(url, {
55
    method: "PUT",
56
    headers: {
57
      "X-AUTH-EMAIL": auth.email,
58
      "X-AUTH-KEY": auth.key,
59
      "Content-Type": "application/json",
60
      Authorization: "Bearer " + auth.token,
61
    },
62
    body: JSON.stringify(body),
63
  });
64
  if (!response.ok) {
65
    const text = await response.text();
66
    throw new Error(`${response.status} ${text}`);
67
  }
68
  return await response.json();
69
}
70