├── .env.sample ├── .gitignore ├── .nvmrc ├── LICENSE ├── README.md ├── index.js └── package.json /.env.sample: -------------------------------------------------------------------------------- 1 | API_KEY=YOUR_API_KEY 2 | LIST_ID=YOUR_LIST_ID 3 | USERNAME=YOUR_USERNAME 4 | DATACENTER=YOUR_DATACENTER 5 | STATUS="pending" 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .env 2 | node_modules/ 3 | -------------------------------------------------------------------------------- /.nvmrc: -------------------------------------------------------------------------------- 1 | v4.1.1 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Permission is hereby granted, free of charge, to any person obtaining a copy 2 | of this software and associated documentation files (the "Software"), to deal 3 | in the Software without restriction, including without limitation the rights 4 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 5 | copies of the Software, and to permit persons to whom the Software is 6 | furnished to do so, subject to the following conditions: 7 | 8 | The above copyright notice and this permission notice shall be included in 9 | all copies or substantial portions of the Software. 10 | 11 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 12 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 13 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 14 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 15 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 16 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 17 | THE SOFTWARE. 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MailChimp Lambda 2 | 3 | A Lambda function for creating MailChimp subscriptions. 4 | 5 | ## Authentication 6 | 7 | Set your MailChimp data center, API key, list ID, and username in the `.env` file. Copy the 8 | sample to get started: 9 | 10 | ``` 11 | $ cp .env.sample .env 12 | ``` 13 | 14 | Additional details about authenticating with the MailChimp API is available [here](http://developer.mailchimp.com/documentation/mailchimp/guides/get-started-with-mailchimp-api-3/). 15 | 16 | ## Deployment 17 | 18 | There's a handy script included to create your zip archive: 19 | 20 | ``` 21 | $ npm run zip 22 | ``` 23 | 24 | Integrate with the 25 | [AWS API Gateway](http://docs.aws.amazon.com/lambda/latest/dg/gs-amazon-gateway-integration.html) 26 | to access the function via HTTP POST: 27 | 28 | ``` 29 | $ curl -X POST -H "Content-Type: application/json" \ 30 | -d '{ "email": "name@email.com" }' \ 31 | YOUR_API_GATEWAY_URL 32 | ``` 33 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | require('dotenv').load(); 2 | 3 | var Promise = require('es6-promise').Promise, 4 | request = require('superagent'), 5 | md5 = require('md5'); 6 | 7 | var API_URL = '.api.mailchimp.com/3.0/lists/', 8 | DATACENTER = process.env.DATACENTER, 9 | API_KEY = process.env.API_KEY, 10 | LIST_ID = process.env.LIST_ID, 11 | USERNAME = process.env.USERNAME, 12 | STATUS = process.env.STATUS; 13 | 14 | function urlForList() { 15 | return 'https://' + DATACENTER + API_URL + LIST_ID + '/members/'; 16 | } 17 | 18 | function urlForUser(emailAddress) { 19 | return urlForList() + md5(emailAddress); 20 | } 21 | 22 | function updateSubscription(emailAddress) { 23 | return new Promise(function(resolve, reject) { 24 | request.patch(urlForUser(emailAddress)) 25 | .auth(USERNAME, API_KEY) 26 | .send({ status: STATUS }) 27 | .end(function(err, res) { 28 | if (err) { 29 | console.log('ERROR', err); 30 | reject({ status: err.status, message: err.response.text }); 31 | } else { 32 | resolve(res.body); 33 | } 34 | }); 35 | }); 36 | } 37 | 38 | function createSubscription(emailAddress) { 39 | return new Promise(function(resolve, reject) { 40 | request.post(urlForList()) 41 | .auth(USERNAME, API_KEY) 42 | .send({ email_address: emailAddress, status: STATUS }) 43 | .end(function(err, res) { 44 | if (err) { 45 | console.log('ERROR', err); 46 | reject({ status: err.status, message: err.response.text }); 47 | } else { 48 | resolve(res.body); 49 | } 50 | }); 51 | }); 52 | } 53 | 54 | exports.handler = function(event, context) { 55 | var emailAddress = event.email; 56 | function create() { 57 | createSubscription(emailAddress) 58 | .then(function(responseBody) { 59 | context.succeed(responseBody); 60 | }) 61 | .catch(function(err) { 62 | context.fail(new Error(err)); 63 | }); 64 | } 65 | 66 | updateSubscription(emailAddress) 67 | .then(function(responseBody) { 68 | context.succeed(responseBody); 69 | }) 70 | .catch(function(err) { 71 | if (err.status === 404) { 72 | create(); 73 | } 74 | }); 75 | }; 76 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mailchimp-lambda", 3 | "version": "1.0.0", 4 | "description": "A Lambda function for creating MailChimp subscriptions", 5 | "main": "index.js", 6 | "scripts": { 7 | "zip": "zip -r mailchimp-lambda.zip .env index.js node_modules/" 8 | }, 9 | "author": "Taylor Briggs", 10 | "license": "MIT", 11 | "dependencies": { 12 | "dotenv": "^1.2.0", 13 | "es6-promise": "^3.0.2", 14 | "md5": "^2.0.0", 15 | "superagent": "^3.7.0" 16 | }, 17 | "repository": { 18 | "type": "git", 19 | "url": "https://github.com/TaylorBriggs/mailchimp-lambda.git" 20 | } 21 | } 22 | --------------------------------------------------------------------------------