0
Create List
One script reply has been approved by the moderators Verified
Created by armancedinari 935 days ago Viewed 9790 times
0
Submitted by adam186 Deno
Verified 760 days ago
1
import { removeObjectEmptyFields } from "https://deno.land/x/windmill_helpers@v1.0.1/mod.ts";
2

3
/**
4
 * Find more information about the parameters at
5
 * https://mailchimp.com/developer/marketing/api/lists/add-list/
6
 */
7
type Mailchimp = {
8
  api_key: string;
9
  server: string;
10
};
11
export async function main(
12
  auth: Mailchimp,
13
  name: string,
14
  permission_reminder: string,
15
  campaign_from_name: string,
16
  campaign_from_email: string,
17
  campaign_subject: string,
18
  campaign_language: string,
19
  email_type_option: boolean,
20
  contact_company: string,
21
  contact_country: string,
22
  contact_city: string,
23
  contact_address1: string,
24
  contact_address2?: string,
25
  contact_state?: string,
26
  contact_zip?: string,
27
  contact_phone?: string,
28
  notify_on_subscribe?: string,
29
  notify_on_unsubscribe?: string,
30
  use_archive_bar?: boolean,
31
  double_optin?: boolean,
32
  marketing_permissions?: boolean,
33
) {
34
  const url = new URL(`https://${auth.server}.api.mailchimp.com/3.0/lists`);
35
  const body = {
36
    name,
37
    contact: {
38
      company: contact_company,
39
      country: contact_country,
40
      city: contact_city,
41
      address1: contact_address1,
42
      address2: contact_address2,
43
      state: contact_state,
44
      zip: contact_zip,
45
      phone: contact_phone,
46
    },
47
    permission_reminder,
48
    campaign_defaults: {
49
      from_name: campaign_from_name,
50
      from_email: campaign_from_email,
51
      subject: campaign_subject,
52
      language: campaign_language,
53
    },
54
    email_type_option,
55
    use_archive_bar,
56
    notify_on_subscribe,
57
    notify_on_unsubscribe,
58
    double_optin,
59
    marketing_permissions,
60
  };
61

62
  const response = await fetch(url, {
63
    method: "POST",
64
    headers: {
65
      Authorization: `Bearer ${auth.api_key}`,
66
    },
67
    body: JSON.stringify(removeObjectEmptyFields(body)),
68
  });
69

70
  if (!response.ok) {
71
    throw Error(await response.text());
72
  }
73
  return await response.json();
74
}
75