This is part one of a blog series designed to help you automate MailChimp using JavaScript.

Before we start, here are some prerequisites,

  • Basic JavaScript knowledge. Enough to comprehend the code samples and follow along.
  • A MailChimp account – preferably a dummy account with some subscriber data.

Getting the API Key

Before running any scripts, first we need to obtain an access token, also referred to in the MailChimp documentation as the API Key. Use button below to authenticate with MailChimp.

Authorize with MailChimp

Hello, MailChimp!

The easiest way to get started with MailChimp API is to use the mailchimp-api-v3 NPM package. It abstracts the gnarly details of constructing API requests.

Once you have successfully obtained the API key, run the code below to make first contact with MailChimp API.

// Import mailchimp-api-v3 NPM package
const MailChimp = require('mailchimp-api-v3')

// Create new MailChimp API object with API KEY
const api = new MailChimp(process.env.MAILCHIMP_API_KEY)

// Get account info "/"
const result = await api.get('/')

In this example, we are making a request to MailChimp API root ("/"), which would return the authenticated user account information. See API reference here. Click on green Run button, it should return your account info as described in the reference doc.

We can build on the code above and do something more interesting, such as pulling out the first and last name.

const { first_name, last_name } = result
console.log(`Hello MailChimp, my name is ${first_name} ${last_name}`)

Working with Lists

At the heart of MailChimp is the subscriber list. This blog series is dedicated to help you automate the repetive work around maintaining a list, such as de-duplicating, auto tagging, or pruning subscribers. But first we need to have a good grasp on how to work with lists using the API.

const MailChimp = require('mailchimp-api-v3')
const api = new MailChimp(process.env.MAILCHIMP_API_KEY)

// Get "/lists"
const result = await api.get('/lists')
for (let list of result.lists) {
	console.log(`List: ${} - ID: ${}`)

Getting the lists is straight forward, simply make a GET request to "/lists". The list result include meta data, such as name, list ID (see reference). The list result, however, does not include the subscribers. In order get the subscribers, we will need to make a subsequent request using the list ID.

In this code example, we will take the first list, result.lists[0], and make a query for the first 10 of its subscribers.

const MailChimp = require('mailchimp-api-v3')
const api = new MailChimp(process.env.MAILCHIMP_API_KEY)

// Get "/lists"
let result = await api.get('/lists')
if (result.lists.length === 0) return

// Get the first list result
const list = result.lists[0]
console.log(`Using List: ${} - ID: ${}`)

// Get list subscribers
result = await api.get(`/lists/${}/members`, { count: 10 })

What's Next

This post covered the basics of reading account info and subscriber lists from the MailChimp API. Up next: