thorning / node-mailchimp

node mailchimp wrapper using v3 of the mailchimp api
MIT License
288 stars 35 forks source link

I can't subscribe new members #37

Closed enzolutions closed 6 years ago

enzolutions commented 6 years ago

Hi folks

I am trying to add a new email into a mail list, but I always get the same error.

Here what I am trying.

var Mailchimp = require('mailchimp-api-v3');
mailchimp_api_key = 'APIKEY-us16';
mc_listid = '129225';

var mailchimp = new Mailchimp(mailchimp_api_key);

const body = {
    email_address: 'example@example.com',
    status:'subscribed'
};

mailchimp.post('/lists/' + mc_listid + '/members',body, function(a,b) { console.log(a,b)});

But always the the following error

{ Error: The requested resource could not be found.
  type: 'http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/',
  title: 'Resource Not Found',
  status: 404,
  detail: 'The requested resource could not be found.',
  instance: 'a706dd6e-429c-495c-af75-1eca5fd61351' 
}

I am using the package inside MeteorJS, just in case that is relevant.

Thanks in advance.

thorning commented 6 years ago

First thing to do is verify the list exists via a get call. http://developer.mailchimp.com/documentation/mailchimp/reference/lists/#

enzolutions commented 6 years ago

@thorning Thanks for your response.

Following your suggestion I create the following code to double check if the list is valid

console.log('/lists/' + mc_listid);
                mailchimp.get({
                    path : '/lists/' + mc_listid
                }).then(function (result) {
                    console.log('Valid list');
                    console.log(result)
                })
                .catch(function (err) {
                    console.log('Invalid list');
                    console.log(err)
                });

At the begging I got the same error Error: The requested resource could not be found., then I started to questioning myself, maybe the list ID is not correct.

How I got the ID, easy from the URL when I was editing the list in my case, I got somethig in my browser like this.

https://us16.admin.mailchimp.com/lists/settings?id=XXXXX

So, I did some research and I found that is not the listid, to get you need to follow the steps explained in the documentation.

https://kb.mailchimp.com/lists/manage-contacts/find-your-list-id

lists_settings_uniqueid

The full example to double check the list and subscribe an email would be like

mailchimp.get({
    path : '/lists/' + mc_listid
}).then(function (result) {
    mailchimp.post('/lists/' + mc_listid + '/members', {
        email_address : 'example@example.com',
        status : 'subscribed'
    })
        .then(function(results) {
            console.log('subscription works');
        })
        .catch(function (err) {
            console.log('subscription fail');
            console.log(err);
        })
})
.catch(function (err) {
    console.log(err)
});

Maybe I could check if already exist or not in the list