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

22 lines
628 B
TypeScript
Raw Normal View History

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