0

Create subscription

by
Published Apr 8, 2025

With subscriptions, you can schedule recurring payments to take place at regular intervals.

Script mollie Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Mollie = {
3
  token: string;
4
};
5
/**
6
 * Create subscription
7
 * With subscriptions, you can schedule recurring payments to take place at regular intervals.
8
 */
9
export async function main(
10
  auth: Mollie,
11
  customerId: string,
12
  body: {
13
    resource?: string;
14
    id?: string;
15
    mode?: string;
16
    status?: string;
17
    amount: { currency: string; value: string };
18
    times?: number;
19
    timesRemaining?: number;
20
    interval: string;
21
    startDate?: string;
22
    nextPaymentDate?: string;
23
    description: string;
24
    method?: string;
25
    applicationFee?: {
26
      amount?: { currency: string; value: string };
27
      description?: string;
28
    };
29
    metadata?: string | {} | string[];
30
    webhookUrl?: string;
31
    customerId?: string;
32
    mandateId?: string;
33
    profileId?: string;
34
    createdAt?: string;
35
    canceledAt?: string;
36
    testmode?: false | true;
37
    _links?: {
38
      self?: { href?: string; type?: string };
39
      customer?: { href?: string; type?: string };
40
      payments?: { href?: string; type?: string };
41
      documentation?: { href?: string; type?: string };
42
    };
43
  },
44
) {
45
  const url = new URL(
46
    `https://api.mollie.com/v2/customers/${customerId}/subscriptions`,
47
  );
48

49
  const response = await fetch(url, {
50
    method: "POST",
51
    headers: {
52
      "Content-Type": "application/json",
53
      Authorization: "Bearer " + auth.token,
54
    },
55
    body: JSON.stringify(body),
56
  });
57
  if (!response.ok) {
58
    const text = await response.text();
59
    throw new Error(`${response.status} ${text}`);
60
  }
61
  return await response.text();
62
}
63