Use Mailchimp API

I would like to use the Mailchimp Node.js API in my Parse Cloud Hosting to subscribe to the mailing list. Parse does not support NPM, but given that the Mailchimp API has no dependencies, I thought I could copy the code into my project. However, the Mailchimp API uses the https module, which Parse does not support.

Does anyone know about this?

+2
source share
3 answers

I was not able to directly use the Mailchimp API, but the REST API is pretty easy to use.

main.js . API URL- REST, - Mailchimp (http://apidocs.mailchimp.com/api/2.0/)

var mailchimpApiKey = "<<REPLACE_WITH_YOUR_KEY>>";

Parse.Cloud.define("SubscribeUserToMailingList", function(request, response) {

  if (!request.params ||
        !request.params.email){
    response.error("Must supply email address, firstname and lastname to Mailchimp signup");
    return;
  }

  var mailchimpData = {
    apikey  : mailchimpApiKey,
    id      : request.params.listid,
    email   : {
      email : request.params.email
    },
    merge_vars : request.params.mergevars
  }

  var url = "https://<<REPLACE_WITH_DATA_CENTRE>>.api.mailchimp.com/2.0/lists/subscribe.json";

  Parse.Cloud.httpRequest({
    method: 'POST',
    url: url,
    body: JSON.stringify(mailchimpData),
    success: function(httpResponse) {
      console.log(httpResponse.text);

      response.success("Successfully subscribed");
    },
    error: function(httpResponse) {
      console.error('Request failed with response code ' + httpResponse.status);
      console.error(httpResponse.text);

      response.error('Mailchimp subscribe failed with response code ' + httpResponse.status);
    }
  });

});

, ... ( )

Parse.Cloud.run("SubscribeUserToMailingList", {
    listid      : "<<REPLACE_WITH_LIST_ID>>",
    email       : email,
    mergevars   : {
        FNAME   : firstName,
        LNAME   : lastName
    }
})
.then(function(success){
    console.log("Successfully subscribed");
    // ...
},
function(error){
    console.log("Unable to subscribe");
    // ...
});
+6
  • mailchimp

    npm install mailchimp-api
    
  • -
      $http

    $http({
        method : 'POST',
        url : '/mailchimp-users/subscribe',
        data : {user:this.name}}).
            success(function(response) {
        console.log("hai this is basic test" + response);
        $scope.send = response.message;
    }).error(function(response) {
        $scope.error = response.message;
    });
    
  •  

    var MailchimpUser = mongoose.model('MailchimpUser'),
    _ = require('lodash'),
    mcapi = require('mailchimp-api');
    var apiKey = '4bf6fb8820c333da4179216c3c2ef8fb-us10';
    // Change this to your Key
    var listID = 'ebbf193760';
    var mc = new mcapi.Mailchimp(apiKey, {version: '2.0'});
    

    exports.subscribe = function(req, res) {
        var entry = req.body.user;
        var mcReq = {
            apikey: '4bf6fb8820c333da4179216c3c2ef8fb-us10',
            id: 'ebbf193760',
            email: {email: entry + '@gmail.com'},
            merge_vars: {
                FNAME: 'subscriber-first-name',
                LNAME: 'subscriber-last-name'
            },
            'double_optin': false,
            'send_welcome': true
        }
        // submit subscription request to mail chimp
        mc.lists.subscribe(mcReq, function(data) {
            console.log(data);
        }, function(error) {
            console.log(error);
        });
    };
    
  • app.route('/mailchimp-users/subscribe')
       .post(mailchimpUsers.subscribe);
    
+4

, MailChimp API v3.0, / , / / !

: - MD5 .

    var jsmd5 = require('cloud/md5js.js');

    // here replace that with your own data center (by looking at your API key).
    var datacenter = "us13";
    var MAILCHIMP_URL = "https://<any_string>:<apikey>@" + datacenter + ".api.mailchimp.com/3.0/";
    var MAILCHIMP_LIST_NEWSLETTER_ID = <yourlistId>;

    Parse.Cloud.define("SubscribeUserToMailingList", function(request, response) {

      if (!request.params ||
            !request.params.email){
        response.error("Must supply email address, firstname and lastname to Mailchimp signup");
        return;
      }

      var email = request.params.email;
      var firstName = request.params.firstname;
      var lastName = request.params.lastname;

      // this converts the email string into an MD5 hash.
      // this is Required if you want to use a "PUT" which allows add/update of an entry, compared to the POST that allows only adding a new subscriber.
      var emailHash = jsmd5.MD5(email);

      var mailchimpData = {
        'email_address': email,
        'status': "subscribed",
        'merge_fields': {
          'FNAME': firstName,
          'LNAME': lastName
        },
        'interests': {
          "<groupID>": true  // optional, if you want to add the user to a "Group".
        }
      };

      var url = MAILCHIMP_URL + "lists/" + MAILCHIMP_LIST_NEWSLETTER_ID + "/members/" + emailHash;

      // using a "PUT" allows you to add/update an entry.
      Parse.Cloud.httpRequest({
        method: 'PUT',
        url: url,
        body: JSON.stringify(mailchimpData),
        success: function(httpResponse) {
          console.log(httpResponse.text);

          response.success("Successfully subscribed");
        },
        error: function(httpResponse) {
          console.error('Request failed with response code ' + httpResponse.status);
          console.error(httpResponse.text);

          response.error('Mailchimp subscribe failed with response code ' + httpResponse.status);
        }
      });
    });
Hide result
+1
source

All Articles