-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscribe.ts
44 lines (38 loc) · 1.05 KB
/
subscribe.ts
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
import type { NextApiRequest, NextApiResponse } from "next";
const client = require("@mailchimp/mailchimp_marketing");
client.setConfig({
apiKey: process.env.MAILCHIMP_API_KEY,
server: process.env.MAILCHIMP_API_SERVER,
});
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const { email, name } = req.body;
if (!email || !name) {
return res.status(400).json({ error: "Email and name are required" });
}
try {
const response = await client.lists.addListMember(
process.env.MAILCHIMP_AUDIENCE_ID,
{
email_address: email,
merge_fields: {
FNAME: name,
},
status: "subscribed",
tags: ["portfolio"],
}
);
if (response.status >= 400) {
return res.status(400).json({
error: `There was an error subscribing to the newsletter.`,
});
}
return res.status(201).json({ error: "" });
} catch (error) {
return res
.status(500)
.json({ error: (error as Error).message || (error as Error).toString() });
}
}