Skip to content

Instantly share code, notes, and snippets.

@andrewstiefel
Created May 25, 2020 19:40
Show Gist options
  • Save andrewstiefel/c2450854b626bf520430df3f19051d02 to your computer and use it in GitHub Desktop.
Save andrewstiefel/c2450854b626bf520430df3f19051d02 to your computer and use it in GitHub Desktop.
Netlify function to add subscribers to ConvertKit
const fetch = require("node-fetch");
const apiKey = process.env.CONVERTKIT_API_KEY;
const apiFormURL = process.env.CONVERTKIT_NEWSLETTER_FORM;
exports.handler = async (event, context) => {
const email = event.queryStringParameters.email || "Oops, no email";
const data = {
api_key: apiKey,
email: email,
};
const subscriber = JSON.stringify(data);
// Subscribe an email
return fetch( apiFormURL, {
method: "post",
body: subscriber,
headers: { "Content-Type": "application/json; charset=utf-8" },
})
.then((res) => res.json())
.then((data) => {
console.log("Success:", data);
})
.then(() => ({
statusCode: 301,
headers: {
Location: "/almost",
},
}))
.catch((error) => {
console.error("Error:", error);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment