0

Cancel a subscription

by
Published Oct 17, 2025

Your subscription can be either cancelled immediately or at the end of the current term based on the value of `cancel_at_end`.

Script zoho Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Zoho = {
3
  token: string;
4
};
5
/**
6
 * Cancel a subscription
7
 * Your subscription can be either cancelled immediately or at the end of the current term based on the value of `cancel_at_end`.
8
 */
9
export async function main(
10
  auth: Zoho,
11
  subscription_id: string,
12
  cancel_at_end: string | undefined,
13
  X_com_zoho_subscriptions_organizationid: string,
14
) {
15
  const url = new URL(
16
    `https://www.zohoapis.com/billing/v1/subscriptions/${subscription_id}/cancel`,
17
  );
18
  for (const [k, v] of [["cancel_at_end", cancel_at_end]]) {
19
    if (v !== undefined && v !== "" && k !== undefined) {
20
      url.searchParams.append(k, v);
21
    }
22
  }
23
  const response = await fetch(url, {
24
    method: "POST",
25
    headers: {
26
      "X-com-zoho-subscriptions-organizationid":
27
        X_com_zoho_subscriptions_organizationid,
28
      Authorization: "Zoho-oauthtoken " + auth.token,
29
    },
30
    body: undefined,
31
  });
32
  if (!response.ok) {
33
    const text = await response.text();
34
    throw new Error(`${response.status} ${text}`);
35
  }
36
  return await response.json();
37
}
38