Skip to content

Instantly share code, notes, and snippets.

@smccracken
Last active April 14, 2024 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smccracken/76b871ee786870ade9e73fcfbc36a5e7 to your computer and use it in GitHub Desktop.
Save smccracken/76b871ee786870ade9e73fcfbc36a5e7 to your computer and use it in GitHub Desktop.
Fetch subscriber count from buttondown email API
// fetch buttondown newsletter subscriber count from API at build time
// common file location src/_data/newsletter.js
module.exports = async function() {
let subscribers;
try {
const response = await fetch(
`https://api.buttondown.email/v1/subscribers`,
{
headers: {
Authorization: `Token ${process.env.BUTTONDOWN_API_KEY}`,
},
},
);
const result = await response.json();
subscribers = `${result.count.toString()}+`;
} catch (error) {
subscribers = 'a whole bunch of';
}
return subscribers;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment