0

SwapPlan

by
Published Oct 17, 2025

Schedules a `SWAP_PLAN` action to swap a subscription plan variation in an existing subscription. For more information, see [Swap Subscription Plan Variations](https://developer.squareup.com/docs/subscriptions-api/swap-plan-variations).

Script square Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Square = {
3
  token: string;
4
};
5
/**
6
 * SwapPlan
7
 * Schedules a `SWAP_PLAN` action to swap a subscription plan variation in an existing subscription. 
8
For more information, see [Swap Subscription Plan Variations](https://developer.squareup.com/docs/subscriptions-api/swap-plan-variations).
9
 */
10
export async function main(
11
  auth: Square,
12
  subscription_id: string,
13
  body: {
14
    new_plan_variation_id?: string;
15
    phases?: { ordinal: number; order_template_id?: string }[];
16
  },
17
) {
18
  const url = new URL(
19
    `https://connect.squareup.com/v2/subscriptions/${subscription_id}/swap-plan`,
20
  );
21

22
  const response = await fetch(url, {
23
    method: "POST",
24
    headers: {
25
      "Content-Type": "application/json",
26
      Authorization: "Bearer " + auth.token,
27
    },
28
    body: JSON.stringify(body),
29
  });
30
  if (!response.ok) {
31
    const text = await response.text();
32
    throw new Error(`${response.status} ${text}`);
33
  }
34
  return await response.json();
35
}
36