Ethan Schofer
Ethan Schofer

Reputation: 1848

MailChimp API 3.0 Subscribe

I am having trouble sorting out the new MailChimp API (V3.0). It does not seem like there is a way to call a subscribe method. It seems like I have to use their Sign Up Form. Am I correct?

Upvotes: 5

Views: 2750

Answers (2)

Derek Soike
Derek Soike

Reputation: 11680

Adding/editing a subscriber via MailChimp v3.0 REST API.

// node/javascript specific, but pretty basic PUT request to MailChimp API endpoint
   
// dependencies (npm)
var request = require('request'),
    url = require('url'),
    crypto = require('crypto');

// variables
var datacenter = "yourMailChimpDatacenter", // something like 'us11' (after '-' in api key)
    listId = "yourMailChimpListId",
    email = "subscriberEmailAddress",
    apiKey = "yourMailChimpApiKey";

// mailchimp options
var options = {
    url: url.parse('https://'+datacenter+'.api.mailchimp.com/3.0/lists/'+listId+'/members/'+crypto.createHash('md5').update(email).digest('hex')),
    headers: {
        'Authorization': 'authId '+apiKey // any string works for auth id
    },
    json: true,
    body: {
        email_address: email,
        status_if_new: 'pending', // pending if new subscriber -> sends 'confirm your subscription' email
        status: 'subscribed',            
        merge_fields: {
            FNAME: "subscriberFirstName",
            LNAME: "subscriberLastName"
        },
        interests: {
            MailChimpListGroupId: true // if you're using groups within your list
        }
    }
};

// perform update
request.put(options, function(err, response, body) {
    if (err) {
        // handle error
    } else {
        console.log('subscriber added to mailchimp list');
    }
});

Upvotes: 1

bryangm
bryangm

Reputation: 163

If by "subscribe" you mean that your application will add someone to a mailing list, you may want to take a look at the List Members Collection portion of their documentation.

http://kb.mailchimp.com/api/resources/lists/members/lists-members-collection

Upvotes: 5

Related Questions