Buttondown
export default async (req, res) => {
const { email } = req.body;
if (!email) {
return res.status(400).json({ error: 'Email is required' });
}
try {
const API_KEY = process.env.BUTTONDOWN_API_KEY;
const response = await fetch(
`https://api.buttondown.email/v1/subscribers`,
{
body: JSON.stringify({ email }),
headers: {
Authorization: `Token ${API_KEY}`,
'Content-Type': 'application/json'
},
method: 'POST'
}
);
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.message || error.toString() });
}
};
Usage
First, create a Buttondown account.
From the settings page, retrieve your API key at the bottom.
To securely access the API, we need to include the secret with each request.
Remember: never commit secrets to git. Thus, we should use an environment variable.