Created
May 25, 2020 19:40
-
-
Save andrewstiefel/c2450854b626bf520430df3f19051d02 to your computer and use it in GitHub Desktop.
Netlify function to add subscribers to ConvertKit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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