0

Update Automation

by
Published Apr 8, 2025
Script buttondown Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Buttondown = {
3
  token: string;
4
};
5
/**
6
 * Update Automation
7
 *
8
 */
9
export async function main(
10
  auth: Buttondown,
11
  id: string,
12
  body: {
13
    name?: string;
14
    status?: "active" | "inactive";
15
    trigger?:
16
      | "subscriber.created"
17
      | "subscriber.unsubscribed"
18
      | "subscriber.changed_email"
19
      | "subscriber.confirmed"
20
      | "subscriber.trial_started"
21
      | "subscriber.trial_ended"
22
      | "subscriber.type.changed"
23
      | "subscriber.tags.changed"
24
      | "subscriber.clicked"
25
      | "subscriber.opened"
26
      | "subscriber.paid"
27
      | "subscriber.churned"
28
      | "subscriber.updated"
29
      | "subscriber.deleted"
30
      | "subscriber.viewed_checkout_page"
31
      | "subscriber.replied"
32
      | "subscriber.paused"
33
      | "subscriber.resumed"
34
      | "subscriber.responded_to_survey"
35
      | "subscriber.referred"
36
      | "subscriber.referred.paid"
37
      | "subscriber.commented"
38
      | "email.created"
39
      | "email.sent"
40
      | "email.updated"
41
      | "email.deleted"
42
      | "email.status.changed"
43
      | "scheduled_email.converted"
44
      | "mention.created"
45
      | "advertising_slot.purchased"
46
      | "social_mention.created"
47
      | "export.created"
48
      | "export.completed"
49
      | "export.failed"
50
      | "automation.invoked"
51
      | "stripe.checkout.session.completed"
52
      | "stripe.subscription.activated"
53
      | "stripe.subscription.churning"
54
      | "stripe.subscription.deactivated"
55
      | "stripe.customer.updated"
56
      | "memberful.subscription.created"
57
      | "memberful.subscription.deleted"
58
      | "memberful.member.updated";
59
    timing?: {
60
      time: "immediate" | "delay";
61
      delay?: {
62
        value: string;
63
        unit: "minutes" | "hours" | "days" | "weeks";
64
        time_of_day?: "" | "morning" | "evening";
65
      };
66
    };
67
    actions?: {}[];
68
    filters?: {
69
      filters: {
70
        field: string;
71
        operator:
72
          | "equals"
73
          | "not_equals"
74
          | "contains"
75
          | "not_contains"
76
          | "is_empty"
77
          | "is_not_empty"
78
          | "greater_than"
79
          | "less_than";
80
        value: string;
81
      }[];
82
      groups: {}[];
83
      predicate: "and" | "or";
84
    };
85
    metadata?: {};
86
  },
87
) {
88
  const url = new URL(`https://api.buttondown.com/v1/automations/${id}`);
89

90
  const response = await fetch(url, {
91
    method: "PATCH",
92
    headers: {
93
      "Content-Type": "application/json",
94
      Authorization: "Token " + auth.token,
95
    },
96
    body: JSON.stringify(body),
97
  });
98
  if (!response.ok) {
99
    const text = await response.text();
100
    throw new Error(`${response.status} ${text}`);
101
  }
102
  return await response.json();
103
}
104