-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubscribe.js
57 lines (52 loc) · 1.47 KB
/
subscribe.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"use strict";
const fetch = require("node-fetch");
// Mailchimp env variables
const MAILCHIMP_API_KEY = process.env.MAILCHIMP_API_KEY;
const MAILCHIMP_TARGET_LIST = process.env.MAILCHIMP_TARGET_LIST;
const MAILCHIMP_API_USERNAME = process.env.MAILCHIMP_API_USERNAME || "metacall";
// Mailchimp endpoint using v3.0
const dc = MAILCHIMP_API_KEY.split("-")[1];
const apiUrl = `https://${dc}.api.mailchimp.com/3.0`;
// Authorization
const auth = {
username: MAILCHIMP_API_USERNAME,
password: MAILCHIMP_API_KEY,
};
/**
*
* Subscribe to a mailchimp list
* Accepts an email and a mailchimp-contact status
*
* More about mailchimp contact status here:
* https://mailchimp.com/developer/guides/manage-subscribers-with-the-mailchimp-api/#Add_a_contact_status
*
* @param {string} email
* @param {string} [status="pending"] - [subscribed || pending || unsubscribed || cleaned]
*/
async function subscribe(email, status = "pending") {
try {
const req = await fetch(
`${apiUrl}/lists/${MAILCHIMP_TARGET_LIST}/members`,
{
method: "post",
body: JSON.stringify({
email_address: email,
status,
}),
headers: {
"Content-Type": "application/json",
Authorization: `Basic ${Buffer.from(
`${auth.username}:${auth.password}`
).toString("base64")}`,
},
}
);
const resp = await req.json();
return resp;
} catch (e) {
return e;
}
}
module.exports = {
subscribe,
};