0

Update Subscriber

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 Subscriber
7
 *
8
 */
9
export async function main(
10
  auth: Buttondown,
11
  id_or_email: string,
12
  body: {
13
    email_address?: string;
14
    notes?: string;
15
    metadata?: {};
16
    tags?: string[];
17
    referrer_url?: string;
18
    type?:
19
      | "regular"
20
      | "premium"
21
      | "churning"
22
      | "past_due"
23
      | "gifted"
24
      | "unpaid"
25
      | "churned"
26
      | "unactivated"
27
      | "unsubscribed"
28
      | "malformed"
29
      | "complained"
30
      | "undeliverable"
31
      | "spammy"
32
      | "removed"
33
      | "trialed"
34
      | "disabled"
35
      | "paused"
36
      | "disposable";
37
    unsubscription_reason?: string;
38
  },
39
) {
40
  const url = new URL(
41
    `https://api.buttondown.com/v1/subscribers/${id_or_email}`,
42
  );
43

44
  const response = await fetch(url, {
45
    method: "PATCH",
46
    headers: {
47
      "Content-Type": "application/json",
48
      Authorization: "Token " + auth.token,
49
    },
50
    body: JSON.stringify(body),
51
  });
52
  if (!response.ok) {
53
    const text = await response.text();
54
    throw new Error(`${response.status} ${text}`);
55
  }
56
  return await response.json();
57
}
58