Skip to content

Instantly share code, notes, and snippets.

@hasantayyar
Forked from lukewduncan/.env
Created October 11, 2017 08:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hasantayyar/261612430b5eddebd0c71ebcade37107 to your computer and use it in GitHub Desktop.
Save hasantayyar/261612430b5eddebd0c71ebcade37107 to your computer and use it in GitHub Desktop.
AWS Lambda function for Mailchimp list subscription
API_KEY='xxxxxxxxxxxxxxxxxxx-us1'
LIST_ID=12033101ax
require('dotenv').load();
var Mailchimp = require('mailchimp-api-v3');
// mailchimp environment variables
var API_KEY = process.env.API_KEY,
LIST_ID = process.env.LIST_ID;
// function for subscribing a user
function addToList(emailAddress) {
var mailchimp = new Mailchimp(API_KEY);
mailchimp.request({
method : 'post',
path : '/lists/' + LIST_ID + '/members',
body : {
email_address : emailAddress,
status : 'subscribed'
}
}).then(function(results) {
console.log(results);
})
.catch(function (err) {
console.log(err);
});
}
exports.handler = function(event, context, callback) {
var emailAddress = event.email;
console.log(event.email);
addToList(emailAddress);
}
{
"name": "mailchimp-lambda-subscription",
"version": "1.0.0",
"description": "A Lambda function to create a subscription to a Mailchimp List",
"main": "index.js",
"scripts": {
"zip": "zip -r mailchimp-lambda.zip .env index.js node_modules/"
},
"author": "Luke Duncan",
"license": "MIT",
"dependencies": {
"mailchimp-api-v3": "*"
},
"repository": {
"type": "git"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment