Search code examples
typescriptgoogle-cloud-functionsmailchimp-api-v3.0

Firebase to MailChimp


Background

I have a completed app (iOS), and it is now in the App Store (Moneypants!). Hooray! I am now finalizing our database code, and I want to send all our new subscribers to MailChimp. I'm using Firebase functions, and I've been able to create a function that successfully accesses the new member node on Firebase and return a value. So far so good.

The Problem

The problem is I can't figure out how to get that data into MailChimp. I've spent four days researching all the API documentation and following every possible lead I could find on Google, all to no avail. I'm sure it's something simple, and so I'm appealing to my fellow coders for help.

What I've Tried

I'm using Visual Studio to code the Firebase function. Here is the code:

// Listens for new messages added to /testAdmin/{pushId}
exports.mailChimpTest3 = functions.database.ref('/testAdmin/{pushId}').onCreate((snapshot, context) => {

// Grab the current value of what was written to the Realtime Database. WORKS!
const original = snapshot.val();
console.log('New user ID:', context.params.pushId,'email:', original.email,'family name:', original.familyName);


// NOW NEED TO SEND THAT INFO TO MAILCHIMP. HOW TO DO IT??
const Mailchimp = require('mailchimp-api-v3')
const mailchimp = new Mailchimp('586xxxxxxxxxxxxxxxxxxxxx-us13');

mailchimp.post('/lists/TEST/members', {
    "email_address": original.email,
    "status": 'subscribed',
    "merge_fields": {
        "FNAME": original.familyName,
        "LNAME": 'Family'
    }

}).then(function(results) {
    console.log('added new firebase user', original.email, 'to mailchimp list (TEST)');
})

.catch(function(err) {
    console.log(err);
})



return null
});

The Error

The above code generates the following error in the console when it runs. I've looked up the error code on MailChimp's site, and it doesn't tell me anything more than the error code itself.

{ Error: The requested resource could not be found. at Request._callback (/user_code/node_modules/mailchimp-api-v3/index.js:506:30) at Request.self.callback (/user_code/node_modules/mailchimp-api-v3/node_modules/request/request.js:185:22) at emitTwo (events.js:106:13) at Request.emit (events.js:191:7) at Request. (/user_code/node_modules/mailchimp-api-v3/node_modules/request/request.js:1157:10) at emitOne (events.js:96:13) at Request.emit (events.js:188:7) at IncomingMessage. (/user_code/node_modules/mailchimp-api-v3/node_modules/request/request.js:1079:12) at IncomingMessage.g (events.js:292:16) at emitNone (events.js:91:20) at IncomingMessage.emit (events.js:185:7) at endReadableNT (_stream_readable.js:974:12) at _combinedTickCallback (internal/process/next_tick.js:80:11) at process._tickDomainCallback (internal/process/next_tick.js:128:9) 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: '37f70bf5-1a91-4a6d-8aea-5fc2a535e86e' }

All I want to do is take the new user's email and username and put them into my 'TEST' list on MailChimp. I'm sure I'm missing something simple, and if anyone could point me in the right direction, I'd really appreciate it. Thanks!!


Solution

  • This answer solved it for me. I was using the wrong name for my subscriber list. Turns out MailChimp uses a secondary name for their lists. For me, it was a string of letters and numbers, like a9fie987, not TEST LIST as I had supposed.