Skip to content

Instantly share code, notes, and snippets.

@panphora
Created May 6, 2022 20:07
Show Gist options
  • Save panphora/168cc4beb03f6b5b2d4f4aa2ba3b32ce to your computer and use it in GitHub Desktop.
Save panphora/168cc4beb03f6b5b2d4f4aa2ba3b32ce to your computer and use it in GitHub Desktop.
function addSubscriberToConvertKit ({firstName, email, formAction, onSuccess, onError}) {
fetch(formAction, {
method: "POST",
body: JSON.stringify({
"first_name": firstName,
"email_address": email
}),
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
}
})
.then(res => res.json())
.then(res => {
if (res.status === "success") {
onSuccess(res);
} else {
onError(res); // TIP: trigger an "invalid email" error if res.errors.fields.includes("email_address")
}
})
.catch(err => {
onError(res);
});
}
addSubscriberToConvertKit({
firstName: "xxxxxxx",
email: "[email protected]",
// create an "inline form" in ConvertKit, then paste the form's action here
formAction: "https://app.convertkit.com/forms/xxxxxxxx/subscriptions",
onSuccess: (res) => console.log("success", res),
onError: (res) => console.log("error", res)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment