0
Create Mailing List Member
One script reply has been approved by the moderators Verified

Add to an existing mailing list. See the docs here

Created by paulattaiffloth 929 days ago Viewed 3337 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 229 days ago
1
type Mailgun = {
2
  api_key: string;
3
};
4

5
export async function main(
6
  resource: Mailgun,
7
  data: {
8
    listAddress: string;
9
    member: {
10
      address: string;
11
      name?: string;
12
      vars?: Record<string, string>;
13
      subscribed?: boolean;
14
      upsert?: boolean;
15
    };
16
  }
17
) {
18
  const form = new FormData();
19
  form.append("address", data.member.address);
20
  form.append("name", data.member.name || "");
21
  form.append(
22
    "vars",
23
    data.member.vars ? JSON.stringify(data.member.vars) : "{}"
24
  );
25
  form.append("subscribed", data.member.subscribed);
26
  form.append("upsert", data.member.upsert);
27

28
  return (
29
    await fetch(
30
      `https://api.mailgun.net/v3/lists/${data.listAddress}/members`,
31
      {
32
        method: "POST",
33
        headers: {
34
          Authorization:
35
            "Basic " +
36
            Buffer.from(`api:${resource.api_key}`).toString("base64"),
37
        },
38
        body: form,
39
      }
40
    )
41
  ).json();
42
}
43