npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mailchimp-node

v1.1.7

Published

Mailchimp API wrapper

Downloads

31

Readme

Mailchimp Node.js Library

Version Build Status Downloads Try on RunKit

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 = require('mailchimp-node')('sk_test_...');

mailchimp.list.createMember(
  'exampleListHash',
  { email: '[email protected]', status: 'subscribed' },
  function(err, subscriber) {
    err; // null if no error occurred
    subscriber; // the created subscriber object
  }
);

Or using ES modules, this looks more like:

import mailchimpPackage from 'mailchimp';
const mailchimp = mailchimpPackage('sk_test_...');

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:
mailchimp.list.create({
  name: 'Foobar Johnson',
  contact: {
    company: 'Foobar Inc.',
    address1: '123 Foobar Street',
    city: 'Foocity',
    state: 'Foostate',
    zip: 01234,
    country: 'US',
  },
  permission_reminder: '',
  campaign_defaults: {
    from_name: 'Foobar Johnson',
    from_email: '[email protected]',
    subject: 'Default Foobar Subject',
    language: 'eng',
  },
  email_type_option: false,
}).then(function(list){
  return mailchimp.list.createMember(list.id, {
    email: '[email protected]',
    status: 'subscribed',
  });
}).then(function(subscriber) {
  // New subscriber
}).catch(function(err) {
  // Deal with an error
});

Configuring Timeout

Request timeout is configurable (the default is Node's default of 120 seconds):

mailchimp.setTimeout(20000); // 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 (process.env.http_proxy) {
  const ProxyAgent = require('https-proxy-agent');
  mailchimp.setHttpAgent(new ProxyAgent(process.env.http_proxy));
}

Examining Responses

Some information about the response which generated a resource is available with the lastResponse property:

charge.lastResponse.requestId // see: https://mailchimp.com/docs/api/node#request_ids
charge.lastResponse.statusCode

request and response events

The mailchimp object emits request and response events. You can use them like this:

var mailchimp = require('mailchimp')('sk_test_...');

function onRequest(request) {
  // Do something.
}

// Add the event handler function:
mailchimp.on('request', onRequest);

// Remove the event handler function:
mailchimp.off('request', onRequest);

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 = mailchimp.webhooks.constructEvent(
  webhookRawBody,
  webhookmailchimpSignatureHeader,
  webhookSecret
);

Writing a Plugin

If you're writing a plugin that uses the library, we'd appreciate it if you identified using mailchimp.setAppInfo():

mailchimp.setAppInfo({
  name: 'MyAwesomePlugin',
  version: '1.2.34', // Optional
  url: 'https://myawesomeplugin.info', // Optional
});

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