Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119
BestASPNETHostingReview.com | Best and Cheap Node.js hosting. In this post I will explains about How to Integrate MailChimp with your Node.js App.
MailChimp is a well-known email marketing SaaS product. With its generous free tier option, it’s seen as the perfect solution for many startups and small businesses who want to set up and build u their mailing lists.
But what many fail to notice is how rich it’s REST API is.
That’s why in this tutorial, I’d like to walk you through a few use cases for how you may want to integrate MailChimp into your own Node.JS application, focusing on:
- Adding new users to your own mailing list in MailChimp.
- Allowing users to import an existing list of contacts from their MailChimp account into your application and sync them back.
We’ll be using the following Node.js libraries: – Express for our backend – Superagent to make REST requests to MailChimp – A few other small libraries for performing OAuth
1 – Adding new users to MailChimp
First let’s start by setting up a very simple project on the command line:
1 2 3 | $ npm init // feel free to enter whatever for these $ npm install --save express body-parser superagent $ touch index.js |
This will install our core dependencies and create our launch file. Open index.js
in your text editor of choice and paste the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 | var express = require('express'); var bodyParser = require('body-parser'); var app = express(); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({extended: true})); app.get('/', function (req, res) { res.send('Hello World!'); }); app.listen(3000, function () { console.log('Example app listening on port 3000!'); }); |
Then on the command line type:
1 2 | $ node index.js Example app listening on port 3000!$ |
If you go into your browser to http://localhost:3000/
, you should see a simple page which says “Hello World!”. With that set up, we can now start integrating a bit deeper.
Adding a signup form
We’ll create a very simple HTML page in views/signup.html
which takes the first name, last name, and email address:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <!doctype html> <html> <head> <title>Sign Up</title> <link href="https://cdnjs.cloudflare.com/ajax/libs/spectre.css/0.1.25/spectre.min.css" rel="stylesheet" crossorigin="anonymous"> </head> <body> <form action="/signup" method="POST" class="centered mt-10 ml-10 mr-10"> <div class="form-group"> <label class="form-label" for="firstname">First Name</label> <input class="form-input" type="text" name="firstname" id="firstname"/> </div> <div class="form-group"> <label class="form-label" for="lastname">Last Name</label> <input class="form-input" type="text" name="lastname" id="lastname"/> </div> <div class="form-group"> <label class="form-label" for="email">Email</label> <input class="form-input" type="text" name="email" id="email"/> </div> <input type="submit" class="btn btn-primary" value="Submit"/> </form> </body> </html> |
In our index.js
we need to serve up static files, so please add the following line:
1 | app.use(express.static('views')); |
We also want to handle the form submission; so for now add the following to index.js
1 2 3 4 | app.post('/signup', function (req, res) { // save user details to your database. res.send('Signed Up!'); }); |
Now when you re-run the app and signup,
Saving our new user to MailChimp
I’m going to assume you have a database somewhere to save this user to, so let’s skip straight to saving this user to a new list in MailChimp.
We’ll need the following information from MailChimp to make the calls:
- Your API token – Log in to your MailChimp account and go to
Profile
in the top right. Then on theProfile
page go toExtras
->API Keys
. Scroll down and if you don’t have any available then clickCreate A Key
- Your Server Instance – This is also embedded in your API token. It is taken from the last characters after the
-
. For example my API token is637274b5ab272affbf7df7d3723ea2a1-us6
, therefore my server instance isus6
. - The Unique List Id – this is for the list you want to add people to. For this, click on
Lists
, find your list, then on the right hand side click the dropdown arrow, then chooseSettings
and scroll down on this page to the bottom where you should seeUnique id for list <your list name>
:
With all of these, we’re ready to make some REST calls! I personally prefer using a library called SuperAgent to make rest calls (we installed it with our initial npm
modules).
At the top of our index.js
load superagent:
1 2 | <span class="token keyword keyword-var">var</span> request <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'superagent'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> |
Then we’ll update our signup
method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <span class="token keyword keyword-var">var</span> mailchimpInstance <span class="token operator">=</span> <span class="token string">'us6'</span><span class="token punctuation">,</span> listUniqueId <span class="token operator">=</span> <span class="token string">'b6a82d89f0'</span><span class="token punctuation">,</span> mailchimpApiKey <span class="token operator">=</span> <span class="token string">'637274b5ab272affbf7df7d3723ea2a1-us6'</span><span class="token punctuation">;</span> app<span class="token punctuation">.</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string">'/signup'</span><span class="token punctuation">,</span> <span class="token keyword keyword-function">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span> <span class="token punctuation">{</span> request <span class="token punctuation">.</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string">'https://'</span> <span class="token operator">+</span> mailchimpInstance <span class="token operator">+</span> <span class="token string">'.api.mailchimp.com/3.0/lists/'</span> <span class="token operator">+</span> listUniqueId <span class="token operator">+</span> <span class="token string">'/members/'</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token keyword keyword-set">set</span><span class="token punctuation">(</span><span class="token string">'Content-Type'</span><span class="token punctuation">,</span> <span class="token string">'application/json;charset=utf-8'</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token keyword keyword-set">set</span><span class="token punctuation">(</span><span class="token string">'Authorization'</span><span class="token punctuation">,</span> <span class="token string">'Basic '</span> <span class="token operator">+</span> <span class="token keyword keyword-new">new</span> <span class="token class-name">Buffer</span><span class="token punctuation">(</span><span class="token string">'any:'</span> <span class="token operator">+</span> mailchimpApiKey <span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token string">'base64'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token string">'email_address'</span><span class="token punctuation">:</span> req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>email<span class="token punctuation">,</span> <span class="token string">'status'</span><span class="token punctuation">:</span> <span class="token string">'subscribed'</span><span class="token punctuation">,</span> <span class="token string">'merge_fields'</span><span class="token punctuation">:</span> <span class="token punctuation">{</span> <span class="token string">'FNAME'</span><span class="token punctuation">:</span> req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>firstName<span class="token punctuation">,</span> <span class="token string">'LNAME'</span><span class="token punctuation">:</span> req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>lastName <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token keyword keyword-function">function</span><span class="token punctuation">(</span>err<span class="token punctuation">,</span> response<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>response<span class="token punctuation">.</span>status <span class="token operator"><</span> <span class="token number">300</span> <span class="token operator">||</span> <span class="token punctuation">(</span>response<span class="token punctuation">.</span>status <span class="token operator">===</span> <span class="token number">400</span> <span class="token operator">&&</span> response<span class="token punctuation">.</span>body<span class="token punctuation">.</span>title <span class="token operator">===</span> <span class="token string">"Member Exists"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> res<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token string">'Signed Up!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword keyword-else">else</span> <span class="token punctuation">{</span> res<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token string">'Sign Up Failed :('</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> |
Here we’ve added variables for our three important pieces of information: MailChimp instance, unique list ID, and our API key. Then in our post
handler, we’re making a POST
request to the MailChimp list management REST API. We set the following HTTP Headers:
Content-Type
– to be JSON and utf-8 charactersAuthorization
– we base64 encode our MailChimp API key and pass it as the Basic auth token.
Next we send the data for the new contact we want to add, including:
- Their email address;
- What we want their status to be on the mailing list. This can be one-off:
subscribed
,unsubscribed
,pending
, andcleaned
; and - The values we want to set for the
merge_fields
on this list. Merge fields allow you to add extra data to a contact in a mailing list. For example you could store phone numbers, dates of births, company names, etc. In this example we’ll just add the first name and last name
Finally we listen for the response by registering an end
handler. In here we check for two states initially. If the status is less than 300 it means the user was successfully added, or if we get back a HTTP status 400 but the title in the response says Member Exists
, then the email address is already in this list. In both cases we can report back to the user that they have successfully signed up. If we get any other status code then something went wrong. In this situation, you’d likely want to let the user try again or raise an alert to yourself and manually fix it.
And if we go check out our MailChimp list, we should see one new member:
2 – Letting your users integrate with their MailChimp account
Now we’re adding our own users, wouldn’t it be awesome to let our users integrate our app with their MailChimp account? Some common use cases are:
- Perhaps you want to offer mailing list functionality without writing it yourself—for example if you’re writing a CRM, shopping platform, or a blogging service;
- Import contacts to pre-fill your users contact list; or to
- Create and Send campaign emails on their behalf from within your app.
In our example, we take a quick look at how we could import all the contacts from an existing Mailing List in MailChimp into our own data store. We’ll do this in 3 stages:
- We’ll use OAuth and let the user grant us access to their MailChimp account
- With that authentication granted we’ll grab the available Lists and pick which one they want to sync the members from
- We’ll grab all the members and store them!
So let’s get cracking!
Using OAuth to get access to the users account
In order to access another user’s account on MailChimp, we need to register ourselves as an App on the platform:
- Log in to your MailChimp account and go to
Profile
on the top right. Then on theProfile
page, go toExtras
->Registered apps
. ClickRegister An App
. You’ll then be presented with a form to fill in the details of your app:
For the most part you can set these values to whatever makes sense. The really important one though is Redirect URI
. This is the url in our application where MailChimp will redirect users once they’ve been authenticated. Since we’re going to run this on our local machine for now, we need to set this to:
http://127.0.0.1:3000/mailchimp/auth/callback
Once you’ve clicked Create App
you’ll be told your app is created. On this page, scroll down and you’ll find two very important pieces of information:
Client ID
Client Secret
Now let’s create a simple page in our views
directory which will trigger our flow called integrated-mailchimp.html
:
1 2 3 4 5 6 7 8 9 10 11 | <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>!doctype</span> <span class="token attr-name">html</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>html</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>head</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>title</span><span class="token punctuation">></span></span>Integrate MailChimp<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>title</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>link</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>https://cdnjs.cloudflare.com/ajax/libs/spectre.css/0.1.25/spectre.min.css<span class="token punctuation">"</span></span> <span class="token attr-name">rel</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>stylesheet<span class="token punctuation">"</span></span> <span class="token attr-name">crossorigin</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>anonymous<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>head</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>body</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>a</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>btn btn-primary<span class="token punctuation">"</span></span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>/mailchimp/auth/authorize<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>Connect with MailChimp<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>a</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>body</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>html</span><span class="token punctuation">></span></span> |
Next we need to hook up an express
handler to process the call to /mailchimp/auth/authorize
:
1 2 3 4 5 6 7 8 9 10 11 12 | <span class="token keyword keyword-var">var</span> querystring <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'querystring'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword keyword-var">var</span> mailchimpClientId <span class="token operator">=</span> <span class="token string">'xxxxxxxxxxxxxxxx'</span><span class="token punctuation">;</span> app<span class="token punctuation">.</span><span class="token keyword keyword-get">get</span><span class="token punctuation">(</span><span class="token string">'/mailchimp/auth/authorize'</span><span class="token punctuation">,</span> <span class="token keyword keyword-function">function</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span> <span class="token punctuation">{</span> res<span class="token punctuation">.</span><span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string">'https://login.mailchimp.com/oauth2/authorize?'</span> <span class="token operator">+</span> querystring<span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token string">'response_type'</span><span class="token punctuation">:</span> <span class="token string">'code'</span><span class="token punctuation">,</span> <span class="token string">'client_id'</span><span class="token punctuation">:</span> mailchimpClientId<span class="token punctuation">,</span> <span class="token string">'redirect_uri'</span><span class="token punctuation">:</span> <span class="token string">'http://127.0.0.1:3000/mailchimp/auth/callback'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> |
Insert your MailChimp Client ID from earlier and then restart your server and hit our new page: http://localhost:3000/integrate-mailchimp.html
:
If you try it out, you should see that when you click the button you’re asked to log into MailChimp. Then afterwards you’re returned to a page on your local machine —this is perfect. Now we need to handle the OAuth callback from MailChimp.
The OAuth callback needs to do the following:
- Request an
access_token
for the user. This is needed to make all future requests for this user - Request for the OAuth
metadata
for the use. This provides information about which API Endpoint we need to talk to for this user - Save the above information for the user for future interactions with MailChimp
Let’s see the code for this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <span class="token keyword keyword-var">var</span> mailchimpSecretKey <span class="token operator">=</span> <span class="token string">'xxxxxxxxxxxxxxxxxxxx'</span><span class="token punctuation">;</span> <span class="token keyword keyword-var">var</span> dataStore <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'./dataStore.js'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> app<span class="token punctuation">.</span><span class="token keyword keyword-get">get</span><span class="token punctuation">(</span><span class="token string">'/mailchimp/auth/callback'</span><span class="token punctuation">,</span> <span class="token keyword keyword-function">function</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span> <span class="token punctuation">{</span> request<span class="token punctuation">.</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string">'https://login.mailchimp.com/oauth2/token'</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span>querystring<span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token string">'grant_type'</span><span class="token punctuation">:</span> <span class="token string">'authorization_code'</span><span class="token punctuation">,</span> <span class="token string">'client_id'</span><span class="token punctuation">:</span> mailchimpClientId<span class="token punctuation">,</span> <span class="token string">'client_secret'</span><span class="token punctuation">:</span> mailchimpSecretKey<span class="token punctuation">,</span> <span class="token string">'redirect_uri'</span><span class="token punctuation">:</span> <span class="token string">'http://127.0.0.1:3000/mailchimp/auth/callback'</span><span class="token punctuation">,</span> <span class="token string">'code'</span><span class="token punctuation">:</span> req<span class="token punctuation">.</span>query<span class="token punctuation">.</span>code <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">(</span>err<span class="token punctuation">,</span> result<span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">{</span> <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>err<span class="token punctuation">)</span> <span class="token punctuation">{</span> res<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token string">'An unexpected error occured while trying to perform MailChimp oAuth'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword keyword-else">else</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// we need to get the metadata for the user</span> request<span class="token punctuation">.</span><span class="token keyword keyword-get">get</span><span class="token punctuation">(</span><span class="token string">'https://login.mailchimp.com/oauth2/metadata'</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token keyword keyword-set">set</span><span class="token punctuation">(</span><span class="token string">'Accept'</span><span class="token punctuation">,</span> <span class="token string">'application/json'</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token keyword keyword-set">set</span><span class="token punctuation">(</span><span class="token string">'Authorization'</span><span class="token punctuation">,</span> <span class="token string">'OAuth '</span> <span class="token operator">+</span> result<span class="token punctuation">.</span>body<span class="token punctuation">.</span>access_token<span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">(</span>err<span class="token punctuation">,</span> metaResult<span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">{</span> <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>err<span class="token punctuation">)</span> <span class="token punctuation">{</span> res<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token string">'An unexpected error occured while trying to get MailChimp meta oAuth'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword keyword-else">else</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// save the result.body.access_token</span> <span class="token comment" spellcheck="true">// save the metadata in metaResult.body</span> <span class="token comment" spellcheck="true">// against the current user</span> <span class="token keyword keyword-var">var</span> mailchimpConf <span class="token operator">=</span> metaResult<span class="token punctuation">;</span> mailchimpConf<span class="token punctuation">.</span>access_token <span class="token operator">=</span> result<span class="token punctuation">.</span>body<span class="token punctuation">.</span>access_token<span class="token punctuation">;</span> dataStore<span class="token punctuation">.</span><span class="token function">saveMailChimpForUser</span><span class="token punctuation">(</span>mailchimpConf<span class="token punctuation">.</span>login<span class="token punctuation">.</span>email<span class="token punctuation">,</span> metaResult<span class="token punctuation">)</span><span class="token punctuation">;</span> res<span class="token punctuation">.</span><span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string">'/pick-a-list.html?email='</span> <span class="token operator">+</span> mailchimpConf<span class="token punctuation">.</span>login<span class="token punctuation">.</span>email<span class="token punctuation">)</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> |
To remind ourselves, the above handler will get called after the user has logged into MailChimp for us, and we’re given a temporary code
with which we can request the long-lived access_token
. Once we get the access_token
, we immediately make a second REST call to get our user’s metadata
. The metadata
returned looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <span class="token punctuation">{</span> <span class="token property">"dc"</span><span class="token operator">:</span> <span class="token string">"us6"</span><span class="token punctuation">,</span> <span class="token property">"role"</span><span class="token operator">:</span> <span class="token string">"owner"</span><span class="token punctuation">,</span> <span class="token property">"accountname"</span><span class="token operator">:</span> <span class="token string">"mattgoldspink"</span><span class="token punctuation">,</span> <span class="token property">"user_id"</span><span class="token operator">:</span> <span class="token number">15048915</span><span class="token punctuation">,</span> <span class="token property">"login"</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token property">"email"</span><span class="token operator">:</span> <span class="token string">"mattgoldspink1@gmail.com"</span><span class="token punctuation">,</span> <span class="token property">"avatar"</span><span class="token operator">:</span> <span class="token null">null</span><span class="token punctuation">,</span> <span class="token property">"login_id"</span><span class="token operator">:</span> <span class="token number">15048915</span><span class="token punctuation">,</span> <span class="token property">"login_name"</span><span class="token operator">:</span> <span class="token string">"mattgoldspink"</span><span class="token punctuation">,</span> <span class="token property">"login_email"</span><span class="token operator">:</span> <span class="token string">"mattgoldspink1@gmail.com"</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token property">"login_url"</span><span class="token operator">:</span> <span class="token string">"https://login.mailchimp.com"</span><span class="token punctuation">,</span> <span class="token property">"api_endpoint"</span><span class="token operator">:</span> <span class="token string">"https://us6.api.mailchimp.com"</span> <span class="token punctuation">}</span> |
In the above JSON response, the most important piece of information is the api_endpoint
. All future REST requests for this user to MailChimp must be made to this server.
For simplicity, I’ve created a very simple in memory dataStore.js
file which looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <span class="token keyword keyword-var">var</span> simpleInMemoryDataStore <span class="token operator">=</span> <span class="token punctuation">{</span> mailchimpConf<span class="token punctuation">:</span> <span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">;</span> module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span> saveMailChimpForUser<span class="token punctuation">:</span> <span class="token keyword keyword-function">function</span><span class="token punctuation">(</span>email<span class="token punctuation">,</span> mailchimpConf<span class="token punctuation">)</span> <span class="token punctuation">{</span> simpleInMemoryDataStore<span class="token punctuation">.</span>mailchimpConf<span class="token punctuation">[</span>email<span class="token punctuation">]</span> <span class="token operator">=</span> mailchimpConf<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> getMailChimpForUser<span class="token punctuation">:</span> <span class="token keyword keyword-function">function</span><span class="token punctuation">(</span>email<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword keyword-return">return</span> simpleInMemoryDataStore<span class="token punctuation">.</span>mailchimpConf<span class="token punctuation">[</span>email<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">;</span> |
I shall leave the real exercise of saving this information and the access_token
as an exercise for the reader since everyone’s data storage solution is different.
Getting the user’s lists
Now we can start building a simple UI which lets the user select their List and then imports the Members. After connecting to MailChimp, we redirect the user to the pick-a-list.html
page.
Let’s write this page:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>!doctype</span> <span class="token attr-name">html</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>html</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>head</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>title</span><span class="token punctuation">></span></span>Pick A List From MailChimp<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>title</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>link</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>https://cdnjs.cloudflare.com/ajax/libs/spectre.css/0.1.25/spectre.min.css<span class="token punctuation">"</span></span> <span class="token attr-name">rel</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>stylesheet<span class="token punctuation">"</span></span> <span class="token attr-name">crossorigin</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>anonymous<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>head</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>body</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>h1</span><span class="token punctuation">></span></span>Pick a List<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>h1</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>p</span><span class="token punctuation">></span></span>Choose which list to sync your Members from: <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>select</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>lists<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>option</span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>null<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>option</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>select</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>p</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>script</span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>https://code.jquery.com/jquery-2.2.4.min.js<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>script</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>script</span><span class="token punctuation">></span></span><span class="token script language-javascript"> <span class="token function">$</span><span class="token punctuation">(</span><span class="token keyword keyword-function">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword keyword-var">var</span> queryParams <span class="token operator">=</span> <span class="token keyword keyword-new">new</span> <span class="token class-name">URLSearchParams</span><span class="token punctuation">(</span>location<span class="token punctuation">.</span>search<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword keyword-var">var</span> mailingListSelect <span class="token operator">=</span> <span class="token function">$</span><span class="token punctuation">(</span><span class="token string">'#lists'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 1. make AJAX call for the list of Lists</span> $<span class="token punctuation">.</span><span class="token keyword keyword-get">get</span><span class="token punctuation">(</span><span class="token string">'/mailchimp/lists?email='</span> <span class="token operator">+</span> queryParams<span class="token punctuation">.</span><span class="token keyword keyword-get">get</span><span class="token punctuation">(</span><span class="token string">'email'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token keyword keyword-function">function</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 2. insert lists into DOM</span> data<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token keyword keyword-function">function</span><span class="token punctuation">(</span>mailchimpList<span class="token punctuation">)</span> <span class="token punctuation">{</span> mailingListSelect<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string">'<option value="'</span> <span class="token operator">+</span> mailchimpList<span class="token punctuation">.</span>id <span class="token operator">+</span> <span class="token string">'">'</span><span class="token operator">+</span> mailchimpList<span class="token punctuation">.</span>name <span class="token operator">+</span> <span class="token string">'('</span> <span class="token operator">+</span> mailchimpList<span class="token punctuation">.</span>stats<span class="token punctuation">.</span>member_count <span class="token operator">+</span> <span class="token string">' Members)'</span> <span class="token operator">+</span> <span class="token string">'</option>'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> </span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>script</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>body</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>html</span><span class="token punctuation">></span></span> |
I’ve kept the logic very simple here. When the page loads we get the email
address from the url using URLSearchParams
. Then we simply perform an ajax request to our backend to get all the mailing lists for the current user. Once that returns, we add them as <option>
‘s into our select
.
The backend service that powers this looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | app<span class="token punctuation">.</span><span class="token keyword keyword-get">get</span><span class="token punctuation">(</span><span class="token string">'/mailchimp/lists'</span><span class="token punctuation">,</span> <span class="token keyword keyword-function">function</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword keyword-var">var</span> mailchimpConf <span class="token operator">=</span> dataStore<span class="token punctuation">.</span><span class="token function">getMailChimpForUser</span><span class="token punctuation">(</span>req<span class="token punctuation">.</span>query<span class="token punctuation">.</span>email<span class="token punctuation">)</span><span class="token punctuation">;</span> request<span class="token punctuation">.</span><span class="token keyword keyword-get">get</span><span class="token punctuation">(</span>mailchimpConf<span class="token punctuation">.</span>api_endpoint <span class="token operator">+</span> <span class="token string">'/3.0/lists'</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token keyword keyword-set">set</span><span class="token punctuation">(</span><span class="token string">'Accept'</span><span class="token punctuation">,</span> <span class="token string">'application/json'</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token keyword keyword-set">set</span><span class="token punctuation">(</span><span class="token string">'Authorization'</span><span class="token punctuation">,</span> <span class="token string">'OAuth '</span> <span class="token operator">+</span> mailchimpConf<span class="token punctuation">.</span>access_token<span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">(</span>err<span class="token punctuation">,</span> result<span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">{</span> <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>err<span class="token punctuation">)</span> <span class="token punctuation">{</span> res<span class="token punctuation">.</span><span class="token function">status</span><span class="token punctuation">(</span><span class="token number">500</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">json</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword keyword-else">else</span> <span class="token punctuation">{</span> res<span class="token punctuation">.</span><span class="token function">json</span><span class="token punctuation">(</span>result<span class="token punctuation">.</span>body<span class="token punctuation">.</span>lists<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> |
We simply retrieve the MailChimp configuration for the user from our dataStore
and make a REST request to MailChimp’s list API. Notice that we construct the MailChimp REST API using the api_endpoint
we stored for the user. We also have to pass the OAuth access_token
for the user in order to authenticate to their account.
If you restart your server and re-authenticate with MailChimp, you should now see the select
list in the page populated with your mailing lists!
Getting a list’s members
Now that we have all the user’s Mailing Lists when they choose one, we should go retrieve all the contacts in that list so that we can:
- display them in the UI
- sync them to our database for the user.
First, on our client side, we’ll update our pick-a-list.html
page by adding:
- a simple HTML table to show the members
- and a jQuery handler to listen to the selection change:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>h2</span><span class="token punctuation">></span></span>Members<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>h2</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>table</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>members<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>table<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>thead</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>th</span><span class="token punctuation">></span></span>Email<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>th</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>th</span><span class="token punctuation">></span></span>First Name<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>th</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>th</span><span class="token punctuation">></span></span>Last Name<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>th</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>thead</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>tbody</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>tbody</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>table</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>script</span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>https://code.jquery.com/jquery-2.2.4.min.js<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>script</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>script</span><span class="token punctuation">></span></span><span class="token script language-javascript"> <span class="token function">$</span><span class="token punctuation">(</span><span class="token keyword keyword-function">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword keyword-var">var</span> queryParams <span class="token operator">=</span> <span class="token keyword keyword-new">new</span> <span class="token class-name">URLSearchParams</span><span class="token punctuation">(</span>location<span class="token punctuation">.</span>search<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword keyword-var">var</span> mailingListSelect <span class="token operator">=</span> <span class="token function">$</span><span class="token punctuation">(</span><span class="token string">'#lists'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 1. make AJAX call for the list of Lists</span> $<span class="token punctuation">.</span><span class="token keyword keyword-get">get</span><span class="token punctuation">(</span><span class="token string">'/mailchimp/lists?email='</span> <span class="token operator">+</span> queryParams<span class="token punctuation">.</span><span class="token keyword keyword-get">get</span><span class="token punctuation">(</span><span class="token string">'email'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment" spellcheck="true">// code from previous step</span> <span class="token punctuation">)</span><span class="token punctuation">;</span> mailingListSelect<span class="token punctuation">.</span><span class="token function">change</span><span class="token punctuation">(</span><span class="token keyword keyword-function">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 3. when an option is select then download the list of members</span> $<span class="token punctuation">.</span><span class="token keyword keyword-get">get</span><span class="token punctuation">(</span><span class="token string">'/mailchimp/list/members/'</span> <span class="token operator">+</span> mailingListSelect<span class="token punctuation">.</span><span class="token function">val</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'?email='</span> <span class="token operator">+</span> queryParams<span class="token punctuation">.</span><span class="token keyword keyword-get">get</span><span class="token punctuation">(</span><span class="token string">'email'</span><span class="token punctuation">)</span> <span class="token punctuation">,</span> <span class="token keyword keyword-function">function</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword keyword-var">var</span> tbodyEl <span class="token operator">=</span> <span class="token function">$</span><span class="token punctuation">(</span><span class="token string">'#members tbody'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> tbodyEl<span class="token punctuation">.</span><span class="token function">html</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span> data<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token keyword keyword-function">function</span><span class="token punctuation">(</span>member<span class="token punctuation">)</span> <span class="token punctuation">{</span> tbodyEl<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string">'<tr><td>'</span> <span class="token operator">+</span> member<span class="token punctuation">.</span>email_address <span class="token operator">+</span> <span class="token string">'</td><td>'</span> <span class="token operator">+</span> member<span class="token punctuation">.</span>merge_fields<span class="token punctuation">.</span>FNAME <span class="token operator">+</span> <span class="token string">'</td><td>'</span> <span class="token operator">+</span> member<span class="token punctuation">.</span>merge_fields<span class="token punctuation">.</span>LNAME <span class="token operator">+</span> <span class="token string">'</td></tr>'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> </span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>script</span><span class="token punctuation">></span></span> |
So here we added a new part to the page which includes a simple html table to render our members. Then we added our change
handler which makes an Ajax request to our backend to get the lists members. On responding, we simply clear the body of the table and add our new members.
Let’s take a look at our backend service for this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | app<span class="token punctuation">.</span><span class="token keyword keyword-get">get</span><span class="token punctuation">(</span><span class="token string">'/mailchimp/list/members/:id'</span><span class="token punctuation">,</span> <span class="token keyword keyword-function">function</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword keyword-var">var</span> mailchimpConf <span class="token operator">=</span> dataStore<span class="token punctuation">.</span><span class="token function">getMailChimpForUser</span><span class="token punctuation">(</span>req<span class="token punctuation">.</span>query<span class="token punctuation">.</span>email<span class="token punctuation">)</span><span class="token punctuation">;</span> request<span class="token punctuation">.</span><span class="token keyword keyword-get">get</span><span class="token punctuation">(</span>mailchimpConf<span class="token punctuation">.</span>api_endpoint <span class="token operator">+</span> <span class="token string">'/3.0/lists/'</span> <span class="token operator">+</span> req<span class="token punctuation">.</span>params<span class="token punctuation">.</span>id <span class="token operator">+</span> <span class="token string">'/members'</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token keyword keyword-set">set</span><span class="token punctuation">(</span><span class="token string">'Accept'</span><span class="token punctuation">,</span> <span class="token string">'application/json'</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token keyword keyword-set">set</span><span class="token punctuation">(</span><span class="token string">'Authorization'</span><span class="token punctuation">,</span> <span class="token string">'OAuth '</span> <span class="token operator">+</span> mailchimpConf<span class="token punctuation">.</span>access_token<span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">(</span>err<span class="token punctuation">,</span> result<span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">{</span> <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>err<span class="token punctuation">)</span> <span class="token punctuation">{</span> res<span class="token punctuation">.</span><span class="token function">status</span><span class="token punctuation">(</span><span class="token number">500</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">json</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword keyword-else">else</span> <span class="token punctuation">{</span> res<span class="token punctuation">.</span><span class="token function">json</span><span class="token punctuation">(</span>result<span class="token punctuation">.</span>body<span class="token punctuation">.</span>members<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> |
If you compare this to our earlier call to get the lists, you’ll see it’s almost identical. In fact all future API calls to MailChimp now become very simple!
If you restart your server and try this, then you’ll see the table will now populate once you select a mailing list:
3. Where to go from here?
Now that you have the list of members, you can easily save them to your data store. Each member has an ID which is used to look it up in MailChimp. Therefore if you wanted to sync back changes to that user in MailChimp, you’d simply make an HTTP PATCH
call to the members’ endpoint. And in the request body, add these changes:
1 2 3 4 5 6 | request<span class="token punctuation">.</span><span class="token function">patch</span><span class="token punctuation">(</span>mailchimpConf<span class="token punctuation">.</span>api_endpoint <span class="token operator">+</span> <span class="token string">'/3.0/lists/'</span> <span class="token operator">+</span> req<span class="token punctuation">.</span>params<span class="token punctuation">.</span>id <span class="token operator">+</span> <span class="token string">'/members/'</span> <span class="token operator">+</span> req<span class="token punctuation">.</span>params<span class="token punctuation">.</span>memberId<span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token keyword keyword-set">set</span><span class="token punctuation">(</span><span class="token string">'Accept'</span><span class="token punctuation">,</span> <span class="token string">'application/json'</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token keyword keyword-set">set</span><span class="token punctuation">(</span><span class="token string">'Authorization'</span><span class="token punctuation">,</span> <span class="token string">'OAuth '</span> <span class="token operator">+</span> mailchimpConf<span class="token punctuation">.</span>access_token<span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string">"merge_fields"</span><span class="token punctuation">:</span><span class="token punctuation">{</span><span class="token string">"FNAME"</span><span class="token punctuation">:</span><span class="token string">"new"</span><span class="token punctuation">,</span><span class="token string">"LNAME"</span><span class="token punctuation">:</span><span class="token string">"name"</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span> |
The MailChimp REST API is extremely rich and supports viewing and editing all sorts of data, including:
- Adding notes to mailing list members.
- Create new campaigns.
- Create and view mail templates.
- Display reports.
- Even manage e-commerce stores.