Created
May 6, 2022 20:07
-
-
Save panphora/168cc4beb03f6b5b2d4f4aa2ba3b32ce to your computer and use it in GitHub Desktop.
This file contains 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
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