Hey there! Thanks for having a look at these code files. If you have any questions/ concerns, feel free to reach me out of twitter
This gist contains the code files alongside the blog Integration Mailchimp with your Node.js App
const express = require("express"); | |
const bodyParser = require("body-parser"); | |
const request = require("request"); | |
const path = require("path"); | |
const app = express(); | |
app.use(express.json()); | |
app.use(bodyParser.urlencoded({extended: false})); | |
app.use(express.static(path.join(__dirname, 'public'))) | |
app.post('/subscribe', (req, res) => { | |
const {email} = req.body; | |
const addData = { | |
members: [ | |
{ | |
email_address: email, | |
status: "pending" | |
} | |
] | |
} | |
addDataJson = JSON.stringify(addData); | |
const options = { | |
url: "https://us6.api.mailchimp.com/3.0/lists/ac7ad45fa0", | |
method: "POST", | |
headers: { | |
Authorization: "auth <YOUR API KEY>" | |
}, | |
body: addDataJson | |
} | |
request (options, (error, response, body) => { | |
body = JSON.parse(body) | |
if(body.errors) { | |
res.sendStatus(400) // error :( | |
} else { | |
res.sendStatus(200); //successful :) | |
} | |
}) | |
}) | |
const PORT = process.env.PORT || 8888; | |
app.listen(PORT, console.log('The server has started!')); |
<!DOCTYPE html> | |
<html> | |
<style> | |
body {font-family: Arial, Helvetica, sans-serif;} | |
form { | |
border: 3px solid #f1f1f1; | |
font-family: Arial; | |
} | |
.container { | |
padding: 20px; | |
background-color: #f1f1f1; | |
} | |
input[type=text], input[type=submit] { | |
width: 100%; | |
padding: 12px; | |
margin: 8px 0; | |
display: inline-block; | |
border: 1px solid #ccc; | |
box-sizing: border-box; | |
} | |
input[type=checkbox] { | |
margin-top: 16px; | |
} | |
input[type=submit] { | |
background-color: #04AA6D; | |
color: white; | |
border: none; | |
} | |
input[type=submit]:hover { | |
opacity: 0.8; | |
} | |
</style> | |
<body> | |
<h2>Mailchimp NodeJS Integration</h2> | |
<form action="/action_page.php"> | |
<div class="container"> | |
<h2>Subscribe to our Newsletter</h2> | |
<p>By clicking on on subscribe, you'll be generating a POST request to the /subscribe endpoint, with the the value in the "Email address" field in the body.</p> | |
</div> | |
<div class="container" style="background-color:white"> | |
<input type="text" placeholder="Email address" name="mail" required id="email"> | |
</div> | |
<div class="container"> | |
<input type="submit" value="Subscribe" id="submit"> | |
</div> | |
</form> | |
</body> | |
<script> | |
let submit = document.getElementById('submit'); | |
let email = document.getElementById('email').value; | |
submit.addEventListener('click', (event) => { | |
event.preventDefault(); | |
if(this.email.value == null || this.email.value == "") { | |
alert("error: email not added"); | |
} else { | |
let fetchData = { | |
method: "POST", | |
body: JSON.stringify({email: this.email.value}), | |
headers: {"Content-Type": "application/json"} | |
} | |
fetch('/subscribe', fetchData) | |
.then(res => { | |
if (res.ok) { | |
alert("Success!") | |
} else { | |
alert("Error!") | |
} | |
}) | |
} | |
}) | |
</script> | |
</html> | |
Hey there! Thanks for having a look at these code files. If you have any questions/ concerns, feel free to reach me out of twitter
This gist contains the code files alongside the blog Integration Mailchimp with your Node.js App
Thank you, very useful but if you want to include the person fname and lname with merge fields how do you go about that