2021-07-31 15:57:43 +00:00
|
|
|
import getConfig from "next/config";
|
|
|
|
import got from "got";
|
2021-07-18 15:32:45 +00:00
|
|
|
|
2021-07-31 15:57:43 +00:00
|
|
|
const { serverRuntimeConfig } = getConfig();
|
2021-07-18 15:32:45 +00:00
|
|
|
|
|
|
|
export async function addSubscriber(email: string) {
|
2021-07-31 15:57:43 +00:00
|
|
|
const { apiKey, audienceId } = serverRuntimeConfig.mailChimp;
|
|
|
|
const region = apiKey.split("-")[1];
|
|
|
|
const url = `https://${region}.api.mailchimp.com/3.0/lists/${audienceId}/members`;
|
2021-07-18 15:32:45 +00:00
|
|
|
const data = {
|
|
|
|
email_address: email,
|
|
|
|
status: "subscribed",
|
2021-07-31 15:57:43 +00:00
|
|
|
};
|
|
|
|
const base64ApiKey = Buffer.from(`any:${apiKey}`).toString("base64");
|
2021-07-18 15:32:45 +00:00
|
|
|
const headers = {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Authorization: `Basic ${base64ApiKey}`,
|
2021-07-31 15:57:43 +00:00
|
|
|
};
|
2021-07-18 15:32:45 +00:00
|
|
|
|
2021-07-31 15:57:43 +00:00
|
|
|
return got.post(url, { json: data, headers });
|
2021-07-18 15:32:45 +00:00
|
|
|
}
|