0

Update subscription

by
Published Apr 8, 2025

Update an existing subscription. Canceled subscriptions cannot be updated. For an in-depth explanation of each parameter, refer to the [Create subscription](create-subscription) endpoint. > 🔑 Access with > > API key > > Access token with **subscriptions.write**

Script mollie Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Mollie = {
3
  token: string;
4
};
5
/**
6
 * Update subscription
7
 * Update an existing subscription.
8

9
Canceled subscriptions cannot be updated.
10

11
For an in-depth explanation of each parameter, refer to the [Create subscription](create-subscription) endpoint.
12

13
> 🔑 Access with
14
>
15
> API key
16
>
17
> Access token with **subscriptions.write**
18
 */
19
export async function main(
20
  auth: Mollie,
21
  customerId: string,
22
  id: string,
23
  testmode: string | undefined,
24
  body: {
25
    amount?: { currency: string; value: string };
26
    description?: string;
27
    interval?: string;
28
    startDate?: string;
29
    times?: number;
30
    metadata?: string;
31
    webhookUrl?: string;
32
    mandateId?: string;
33
  },
34
) {
35
  const url = new URL(
36
    `https://api.mollie.com/v2/customers/${customerId}/subscriptions/${id}`,
37
  );
38
  for (const [k, v] of [["testmode", testmode]]) {
39
    if (v !== undefined && v !== "" && k !== undefined) {
40
      url.searchParams.append(k, v);
41
    }
42
  }
43
  const response = await fetch(url, {
44
    method: "PATCH",
45
    headers: {
46
      "Content-Type": "application/json",
47
      Authorization: "Bearer " + auth.token,
48
    },
49
    body: JSON.stringify(body),
50
  });
51
  if (!response.ok) {
52
    const text = await response.text();
53
    throw new Error(`${response.status} ${text}`);
54
  }
55
  return await response.text();
56
}
57