upstream #1

Merged
jblu merged 1007 commits from upstream into main 2024-11-04 22:35:57 -06:00
Showing only changes of commit 795d596012 - Show all commits

View File

@ -1,32 +1,33 @@
export default async (req, res) => { export default async (req, res) => {
const { email } = req.body const { email } = req.body
if (!email) { if (!email) {
return res.status(400).json({error: 'Email is required'}) return res.status(400).json({ error: 'Email is required' })
} }
try { try {
const API_KEY = process.env.KLAVIYO_API_KEY const API_KEY = process.env.KLAVIYO_API_KEY
const LIST_ID = process.env.KLAVIYO_LIST_ID const LIST_ID = process.env.KLAVIYO_LIST_ID
const response = await fetch(`https://a.klaviyo.com/api/v2/list/${LIST_ID}/subscribe?api_key=${API_KEY}`, { const response = await fetch(
method: 'POST', `https://a.klaviyo.com/api/v2/list/${LIST_ID}/subscribe?api_key=${API_KEY}`,
headers: { {
Accept: 'application/json', method: 'POST',
'Content-Type': 'application/json', headers: {
}, Accept: 'application/json',
// You can add additional params here i.e. SMS, etc. 'Content-Type': 'application/json',
// https://developers.klaviyo.com/en/reference/subscribe },
body: JSON.stringify({ // You can add additional params here i.e. SMS, etc.
profiles: [ // https://developers.klaviyo.com/en/reference/subscribe
{email: email}, body: JSON.stringify({
] profiles: [{ email: email }],
}), }),
}) }
if (response.status >= 400) { )
return res.status(400).json({ if (response.status >= 400) {
error: `There was an error subscribing to the list.`, return res.status(400).json({
}) error: `There was an error subscribing to the list.`,
} })
return res.status(201).json({ error: '' }) }
return res.status(201).json({ error: '' })
} catch (error) { } catch (error) {
return res.status(500).json({ error: error.message || error.toString() }) return res.status(500).json({ error: error.message || error.toString() })
} }