MailChimp API 3.0 – Get All List Members
In MailChimp API docs it said:
Use offset and count in the URL query string to paginate because it provides greater control over how you view your data.
Offset defaults to 0. Count defaults to 10.
So, it means if you change count
you can increase the number of results. Like this
https://usX.api.mailchimp.com/3.0/lists/{LIST_ID}/members?count=20
Seems simple but the maximum allowed count
parameter is 50. So, you can not get more than 50 subscribers just in a one API call.
OK, what to do if you have much more subscribers in a list?

Just use both count
and offset
parameters! For example if you want to get 100 subscribers, you will need 2 API calls:
https://usX.api.mailchimp.com/3.0/lists/{LIST_ID}/members?offset=0&count=50
and
https://usX.api.mailchimp.com/3.0/lists/{LIST_ID}/members?offset=50&count=50
Use the example below to get thouthands of your list members at once
If you would like to use WordPress example, please scroll down a little, if you connect to MailChimp API via cURL, then copy this function and insert it somewhere in your website files. You haven’t to change anything in it, just copy it as is.
So, now our MailChimp API connection becomes easier.
$api_key = 'YOU API KEY';
$list_id = 'LIST ID';
$dc = substr($api_key,strpos($api_key,'-')+1); // us5, us8 etc
// URL to connect
$url = 'https://'.$dc.'.api.mailchimp.com/3.0/lists/'.$list_id;
// connect and get results
$body = json_decode( rudr_mailchimp_curl_connect( $url, 'GET', $api_key ) );
// number of members in this list
$member_count = $body->stats->member_count;
$emails = array();
for( $offset = 0; $offset < $member_count; $offset += 50 ) :
$data = array(
'offset' => $offset,
'count' => 50
);
// URL to connect
$url = 'https://'.$dc.'.api.mailchimp.com/3.0/lists/'.$list_id.'/members';
// connect and get results
$body = json_decode( rudr_mailchimp_curl_connect( $url, 'GET', $api_key, $data ) );
foreach ( $body->members as $member ) {
$emails[] = $member->email_address;
}
endfor;
print_r( $emails );
WordPress example
Actually for WordPress you can use any of these examples. Both of them should work well.
$api_key = 'YOU API KEY';
$list_id = 'LIST ID';
$dc = substr($api_key,strpos($api_key,'-')+1); // us5, us8 etc
$args = array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'user:'. $api_key )
)
);
// connect
$response = wp_remote_get( 'https://'.$dc.'.api.mailchimp.com/3.0/lists/'.$list_id, $args );
// decode the response
$body = json_decode( $response['body'] );
if ( $response['response']['code'] == 200 ) :
// subscribers count
$member_count = $body->stats->member_count;
$emails = array();
for( $offset = 0; $offset < $member_count; $offset += 50 ) :
$response = wp_remote_get( 'https://'.$dc.'.api.mailchimp.com/3.0/lists/'.$list_id.'/members?offset=' . $offset . '&count=50', $args );
// decode the result
$body = json_decode( $response['body'] );
if ( $response['response']['code'] == 200 ) {
foreach ( $body->members as $member ) {
$emails[] = $member->email_address;
}
}
endfor;
endif;
// print all emails
print_r( $emails );

Misha Rudrastyh
Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me
Thanks for posting this. I am not using WordPress, but your first code snippet gave me the idea on how to tackle this in my SF2 application.
How would I extract details of Merge Fields ? Should I change email_address for merge1 ? And o extract multiple merge fields, do I separate them with commas ?
Hi Cormac,
Sorry, I mean I want to extract details from CERTAIN Merge Fields (not all) but I do NOT want to extract the email address. So I was thinking I might need to change something like this
$emails[] = $member->merge1, merge3, merge4;
???
If I understand you properly:
$emails[] = $member->merge_fields->merge1;
for multiple fields:
$emails[] = $member->merge_fields;
I mean in my previous comment that you can find out everything by yourself using
print_r()
function.Almost there
I have 5 merge fields. I want to ONLY extract merge1, merge3 and merge4 – so just a selection of all merge fields
So what would the delimiter be between fields – like this ?
$emails[] = $member->merge_fields->merge1,merge3,merge4;
I understand you,
here it is:
thx. this post is vary useful
This seems to stop functioning after it hits a certain limit
What is your limit, Jon?
Thank you for the very fast reply.
My apologies. That was a premature comment, I should have investigated further first. I am getting 2,300+ subscribers returned. What is strange, however, is that some users who are in the list on MailChimp’s backend do not get returned in this list. That’s where I thought it was a hitting a limit, but the count was just a coincidence. I’ve checked the list ID (there’s only one), I’ve checked to make sure the user is subscribed to the list, made sure their status is ‘subscribed’…all checks out. For some reason, however, their email does not get returned in the array. It only happens for some, not all.
Strange. I remember that I printed 8K subscribers. But maybe not all as well – I didn’t checked. I will try to test this issue when I have a time.
Thank you.
Hi Misha,
I’m trying to make use mailchimp API to work on my custom CMS, i would like show the stats, subscribed users, campaign stats, campaign templates etc I nearly want to retrieve and show everything on the admin dashboard. So that admin doesn’t need to login to the Mailchimp.
Can you please guide me or can you provide me resources? or give some useful link so that i can get started and learn few basic and advance things to use Mailchimp API.
Thank You
Hi,
Sure, this helped me a lot https://developer.mailchimp.com/documentation/mailchimp/reference/overview/
Hey Misha,
thank so so much for this tutorial – I’ve been looking for something like this for ages and
I always felt quite overwhelmed by the mailchimp API.
In my case I was looking for a lighweight method to display a table of subscribers in wordpress for logged in editors without the need of switching to mailchimp admin area.
I’m heavily using custom merge fields and I was quite delighted you can easily output them by their custom merge name e.g. ‘$member->merge_fields->ADDRESS’.
Maybe this snippet is useful for someone:
Hey Patrick,
Thanks for sharing.
Hi Im having Fatal error: Call to undefined function rudr_mailchimp_curl_connect()
Hi,
Read the post please.
Hello, thank you for the excellent code.
I’m having trouble to load the members, it takes minute to load all of it.
I have 3000+ members. Do you have any idea how can I lessen the load time?
Hello,
No 🙃
Maybe a better server performance could help. But it wouldn’t be much speed improvement.
Thanks!
Thanks luv
When attempting to use this outside of WordPress I see this:
Fatal error: Uncaught Error: Call to undefined function rudr_mailchimp_curl_connect() in /home/robotbea/public_html/mc.php:10 Stack trace: #0 {main} thrown in /home/robotbea/public_html/mc.php on line 10
If using it with the WordPress code snippet I’m getting rendering issues.
Any ideas? Thanks
It is because you didn’t read the post, just inserted the code 🙃
It is described in the post above where to get
rudr_mailchimp_curl_connect()
function.So are you indicating that I need to include the function shown here: https://rudrastyh.com/mailchimp-api/get-lists.html#mailchimp_api_connect in addition to the WordPress code snippet shown above?
Where do you suggest putting these functions?
You can put it to your current WP theme
functions.php
file.Thanks !this helped me alot….but i want to know that how could i unsubscribe the list .plz help
Hey,
This tutorial should help.
Fatal error: Call to undefined function rudr_mailchimp_curl_connect()
plz guide me
Now i get only the emails, i need to get some fields… if you can help me ill be thankfull all life!
You can get fields from the
merge_fields
object, example:$member->merge_fields->YOUR MERGE FIELD NAME
.If you are not sure, you could try to do it
print_r( $member->merge_fields );
just to make a look what is inside the object.Tks, now, im receiving only the data of a single registered user. What i need to do to rectrieve all users?
How i will get all the subsciber of rating above 3 from all the list?