0

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

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