shellphone.app/app/api/newsletter/_mailchimp.ts

22 lines
638 B
TypeScript
Raw Normal View History

import getConfig from "next/config";
import got from "got";
2021-07-18 15:32:45 +00:00
const { serverRuntimeConfig } = getConfig();
2021-07-18 15:32:45 +00:00
export async function addSubscriber(email: string) {
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",
};
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-18 15:32:45 +00:00
return got.post(url, { json: data, headers });
2021-07-18 15:32:45 +00:00
}