Get all members from the mailing list using MailChimp API 3.0
Asked Answered
H

6

16

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

Using this resource we can obtain only first 10 members. How to get all?

Housemaster answered 21/8, 2015 at 14:58 Comment(0)
H
23

The answer is quite simple - use offset and count parameters in URL query:

https://us10.api.mailchimp.com/3.0/lists/b5b5fdc2fa/members?offset=150&count=10

Finally I found PHP API client for MailChimp API v3: https://github.com/pacely/mailchimp-api-v3

And official docs about pagination.. I missed it before :( http://kb.mailchimp.com/api/article/api-3-overview

Housemaster answered 26/8, 2015 at 9:46 Comment(4)
@pocockn You can grab them in two steps: 1) get total items 2) get all members. Let me know if you'll find a better solution :)Housemaster
count seems to be maximized at 1000. So you need to iterate through all members in multiple calls.Ceramics
It's worth mentioning there's restrictions on the count you can use in the request. For me it seems to top out around 700.Helgeson
when trying to make a curl call to a "normal" list of about 2600 members I get Internal server error from the mailchimp api, I am trying at the moment to export the list members to an excel file and I am stuck, so the only solution I can see for large list it's to make incremental requests and save the results on a file or db, not a nice solution.Undersecretary
A
7

I stumbled on this one while researching a way to get all list members in MC API 3.0 as well. I noticed that there were some comments on the API timing out when trying to get all list members on one page. I also encountered this at first but was able to overcome it by limiting the fields in the result by using the 'fields' param. My code is for a mass deleter so all I really needed was the ID of each member to put together a batch delete request. Here's how my fetch request looks (psuedo-code):

$total_members = $result['total_items'];//get number of members in list via previous request
https://usXX.api.mailchimp.com/3.0/lists/foobarx/members?fields=members.id&count=total_members

This way I'm able to fetch over 15,000 subscribers on one page without error.

Abjuration answered 27/7, 2017 at 18:0 Comment(0)
D
5

Using offset and count parameters are correct as mentioned in some of the other answers, but becomes tedious for large lists.

A more efficient way, is to use a client for the MailChimp API. I used mailchimp3 for python. Using this, it's pretty easy to get all members on your list because it handles the pagination. Here's how you would do it.

from mailchimp3 import MailChimp

client = MailChimp('YOUR_USERNAME', 'YOUR_SECRET_KEY')
client.lists.members.all('YOUR_LIST_ID', get_all=True, fields="members.email_address")
Destructionist answered 23/1, 2017 at 1:48 Comment(0)
B
4

offset and count is the official way on the docs, but the problem is that has linear slowdown. It appears to be an n^2 solution, so if you have 20,000 items, you're in trouble. Their docs http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#read-get_lists_list_id_members warn you against using offset.

If you're scenario permits you to use other filters (like since_last_changed), then you can do it quickly. See What is the right syntax for "timeframe" in MailChimp API 3.0 for format for datetime.

Berryberryhill answered 4/8, 2016 at 21:38 Comment(0)
U
2

You can do it just with count, making an API call to the list root so in the next API call you include the count parameter and you have all your list members.

I ran into issues with this because I had a moderate list with 2600 members and MailChimp was throwing an error, but it worked with 1500 people.

So for a list bigger than 1500 members I use MailChimp export API bare in mind that this is going to get discontinued but I could not find any other acceptable solutions.

Alternatively for bigger lists (>1500) you could get the total of members and then make multiple api calls to the Member endpoint but I really dislike that :(

If anyone has a better alternative I would be really glad to hear it.

Undersecretary answered 26/10, 2016 at 11:35 Comment(0)
S
0

With MailChimp.Net.
Use the offset value.

List<Member> listMembers = new List<Member>();
IMailChimpManager manager = new MailChimpManager(MailChimpApiKey);
bool moreAvailable = true;
int offset = 0;
while (moreAvailable)
{
    var listMembers = manager.Members.GetAllAsync(yourListId, new MemberRequest
    {
        Status = Status.Subscribed,
        Limit = 250,
        Offset = offset
    }).ConfigureAwait(false);

    var Allmembers = listMembers.GetAwaiter().GetResult();
    foreach(Member member in Allmembers)
    {
        listMembers.Add(member);
    }
    if (Allmembers.Count() == 250)
        //if the count is < of 250 then it means that there aren't more results
        offset += 250;
    else
        moreAvailable = false;
}
Sassy answered 13/6, 2018 at 10:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.