Search code examples
node.jsnpmmailchimp-api-v3.0mailchimp-api-v3

No More than 10 Segment Members in A Batch Call with mailchimp-api-v3


I have been using mailchimp-api-v3 in node js for some time now and I think it is a great package and so easy to use. However, recently I needed to GET Segment Members for several segments using its .batch() method.

Unfortunately, only 10 members are returned for each segment even if there are more than 10 members and a count query parameter value of more than 10 is specified in the call. With

const mc  = require('./mc-config');
const Mailchimp = require('mailchimp-api-v3');
const mailchimp = new Mailchimp(mc.apiKey);
const list_id   = mc.list_id;

Each of the following returns more than 10 members, as expected:

mailchimp.request({method:'GET',path:`/list/${list_id}/segments/${segment_id}/members`, query: {count: 1000}})....;
//or
mailchimp.get(`/lists/${list_id}/segments/${segment_id}/members?count=1000`)....;

However, the following only returns at most 10 segment members, per segment:

mailchimp.batch([
    {method:'GET',path:`/lists/${list_id}/segments/${segment_id_1}/members`, query: {count:1000}},
    {method:'GET',path:`/lists/${list_id}/segments/${segment_id_2}/members`, query: {count:1000}},
    {method:'GET',path:`/lists/${list_id}/segments/${segment_id_3}/members`, query: {count:1000}},
    {method:'GET',path:`/lists/${list_id}/segments/${segment_id_4}/members`, query: {count:1000}},
    {method:'GET',path:`/lists/${list_id}/segments/${segment_id_5}/members`, query: {count:1000}}
])....;

I thought that perhaps query parameters were being ingnored altogether but when I added the following parameters I got more members returned for segments which had less than 10 members:

.... include_cleaned:true, include_unsubscribed:true ....

Has anybody else experienced this issue? Is there something I'm missing?


Solution

  • Using Promise.all() seems to work just fine, a pity though that I can use .batch():

    (async () => {
        try {
            const segments = [segment_id1, segment_id2, segment_id3, segment_id4];
            const members = await Promise.all(
                segments.map(tag => mailchimp.get(`/lists/${list_id}/segments/${tag}/members?count=1000`))
            );
            //see how many members are returned
            console.log( members.map(m => m.members.length) );
        } catch( err ) {
            console.log( err );
        } finally {
            console.log( 'All done' );
        }
    })();
    

    For each segment/tag that had more than 10 members, all the members were returned (upto 1000).