Read MailChimp Contacts Using the RESTful Client API

Updated: Jan 31, 2023

This example shows you how to read MailChimp contacts using the RESTful client API in DataPipeline.

You can see similar MailChimp examples here.

Java Code Listing

package com.northconcepts.datapipeline.mailchimp;

import com.northconcepts.datapipeline.mailchimp.list.ListMember;
import com.northconcepts.datapipeline.mailchimp.list.ListMemberStatus;
import com.northconcepts.datapipeline.mailchimp.list.ListMembers;

public class ReadMailChimpContactsUsingClientAPI {
    private static final String apiKey = "api-key";
    private static final String listId = "list-id";

    private static final MailChimpClient client = MailChimpClient.Proxy.get(apiKey);
    
    public static void main(String[] args) {
        ListMembers listMembers = client.getListMembersObject(listId, ListMemberStatus.subscribed, 0, 5);
        
        System.out.println("Total contacts :"+listMembers.getMembers().size());
        for (ListMember listMember: listMembers.getMembers()) {
            System.out.println("---------------------------");
            System.out.println("Id :"+listMember.getId());
            System.out.println("Email :"+listMember.getEmailAddress());
            System.out.println("Status :"+listMember.getStatus());
        }
    }
    
}

Code Walkthrough

  1. apiKey is your MailChimp token which you can easily learn how to generate in https://mailchimp.com/help/about-api-keys/.
  2. listId is your audience id.
  3. An instance of MailChimpClient is created and will be used to fetch the list of members/contacts.
  4. getListMembersObject returns a list of members.
  5. ListMemberStatus.subscribed is the status of the contact.
  6. The status can either be subscribed, unsubscribed, cleaned, pending, or archived.
  7. 0 is the offset and 5 is the number of records you want to fetch.
  8. The output is finally printed after looping through the members.
Mobile Analytics