1 | |
2 | type Buttondown = { |
3 | token: string; |
4 | }; |
5 | |
6 | * Create Webhook |
7 | * |
8 | */ |
9 | export async function main( |
10 | auth: Buttondown, |
11 | body: { |
12 | status?: "enabled" | "disabled"; |
13 | event_types: |
14 | | "subscriber.created" |
15 | | "subscriber.unsubscribed" |
16 | | "subscriber.changed_email" |
17 | | "subscriber.confirmed" |
18 | | "subscriber.trial_started" |
19 | | "subscriber.trial_ended" |
20 | | "subscriber.type.changed" |
21 | | "subscriber.tags.changed" |
22 | | "subscriber.clicked" |
23 | | "subscriber.opened" |
24 | | "subscriber.paid" |
25 | | "subscriber.churned" |
26 | | "subscriber.updated" |
27 | | "subscriber.deleted" |
28 | | "subscriber.viewed_checkout_page" |
29 | | "subscriber.replied" |
30 | | "subscriber.paused" |
31 | | "subscriber.resumed" |
32 | | "subscriber.responded_to_survey" |
33 | | "subscriber.referred" |
34 | | "subscriber.referred.paid" |
35 | | "subscriber.commented" |
36 | | "email.created" |
37 | | "email.sent" |
38 | | "email.updated" |
39 | | "email.deleted" |
40 | | "email.status.changed" |
41 | | "scheduled_email.converted" |
42 | | "mention.created" |
43 | | "advertising_slot.purchased" |
44 | | "social_mention.created" |
45 | | "export.created" |
46 | | "export.completed" |
47 | | "export.failed" |
48 | | "automation.invoked" |
49 | | "stripe.checkout.session.completed" |
50 | | "stripe.subscription.activated" |
51 | | "stripe.subscription.churning" |
52 | | "stripe.subscription.deactivated" |
53 | | "stripe.customer.updated" |
54 | | "memberful.subscription.created" |
55 | | "memberful.subscription.deleted" |
56 | | "memberful.member.updated"[]; |
57 | url: string; |
58 | description?: string; |
59 | }, |
60 | ) { |
61 | const url = new URL(`https://api.buttondown.com/v1/webhooks`); |
62 |
|
63 | const response = await fetch(url, { |
64 | method: "POST", |
65 | headers: { |
66 | "Content-Type": "application/json", |
67 | Authorization: "Token " + auth.token, |
68 | }, |
69 | body: JSON.stringify(body), |
70 | }); |
71 | if (!response.ok) { |
72 | const text = await response.text(); |
73 | throw new Error(`${response.status} ${text}`); |
74 | } |
75 | return await response.json(); |
76 | } |
77 |
|