Mailchimp Node.js Library
A Mailchimp Node library provides convenient access to the Mailchimo API from applications written in server-side JavaScript.
This library uses Mailchimp API v3.0
Forked from the excellent Stripe Node library
Installation
Install the package with:
npm install mailchimp-node --save
# Usage
The package needs to be configured with your account's secret key which is available in your Mailchimp Dashboard. Require it with the key's value:
var mailchimp = 'sk_test_...';mailchimplist;
Or using ES modules, this looks more like:
;const mailchimp = ;
Using Promises
Every method returns a chainable promise which can be used instead of a regular callback:
// Create a new subscriber and then a new charge for that subscriber:mailchimplist;
Configuring Timeout
Request timeout is configurable (the default is Node's default of 120 seconds):
mailchimp; // in ms (this is 20 seconds)
Configuring a Proxy
An https-proxy-agent can be configured with
setHttpAgent
.
To use mailchimp behind a proxy you can pass to sdk:
if processenvhttp_proxy const ProxyAgent = ; mailchimp;
Examining Responses
Some information about the response which generated a resource is available
with the lastResponse
property:
chargelastResponserequestId // see: https://mailchimp.com/docs/api/node#request_idschargelastResponsestatusCode
request
and response
events
The mailchimp object emits request
and response
events. You can use them like this:
var mailchimp = 'sk_test_...'; { // Do something.} // Add the event handler function:mailchimp; // Remove the event handler function:mailchimp;
request
object
api_version: 'latest' account: 'acct_TEST' // Only present if provided idempotency_key: 'abc123' // Only present if provided method: 'POST' path: '/v1/charges'
response
object
api_version: 'latest' account: 'acct_TEST' // Only present if provided idempotency_key: 'abc123' // Only present if provided method: 'POST' path: '/v1/charges' status: 402 request_id: 'req_Ghc9r26ts73DRf' elapsed: 445 // Elapsed time in milliseconds
Webhook signing
mailchimp can optionally sign the webhook events it sends to your endpoint, allowing you to validate that they were not sent by a third-party. You can read more about it here.
Please note that you must pass the raw request body, exactly as received from mailchimp, to the constructEvent()
function; this will not work with a parsed (i.e., JSON) request body.
You can find an example of how to use this with Express in the examples/webhook-signing
folder, but here's what it looks like:
event = mailchimpwebhooks;
Writing a Plugin
If you're writing a plugin that uses the library, we'd appreciate it if you identified using mailchimp.setAppInfo()
:
mailchimp;
This information is passed along when the library makes calls to the mailchimp API.
More Information
Development
Run all tests:
$ npm install$ npm test
Run a single test suite:
$ npm run mocha -- test/Error.spec.js
Run a single test (case sensitive):
$ npm run mocha -- test/Error.spec.js --grep 'Populates with type'
If you wish, you may run tests using your mailchimp Test API key by setting the
environment variable MAILCHIMP_TEST_API_KEY
before running the tests:
$ export mailchimp_TEST_API_KEY='sk_test....'$ npm test