vanilla_skies
vanilla_skies

Reputation: 415

Mailchimp API cutting off members

I am using this rails gem to access the Mailchimp API. https://bitbucket.org/mailchimp/mailchimp-api-ruby

mailchimp = Mailchimp::API.new(ENV['MAILCHIMP-API-KEY'])

My list has 59 members and I can confirm that from

newsletter_members = mailchimp.lists.members(listID)['total']

but when I try to access the data hash, it only returns 25 objects?

newsletter_members = mailchimp.lists.members(listID)['data'].size

Any ideas as to why?

I am basically trying to find if a email exists in my mail chimp list and this code is breaking because I am not getting all the members

mailchimp = Mailchimp::API.new(ENV['MAILCHIMP-API-KEY'])
listID = 'my-list-id'
email_array = []
newsletter_members = mailchimp.lists.members(listID)['data']
# the email array is cut short..not getting all members
newsletter_members.each do |member|
  email_array <<  member['email']
end
#returns true or false
member_exists = email_array.include?(user_email)

Upvotes: 0

Views: 439

Answers (1)

Dylan Markow
Dylan Markow

Reputation: 124419

Mailchimp's API defaults to return 25 items at a time. You can request more (though the limit is 100), or you can make multiple requests for "pages" of results.

To request 100 results:

mailchimp.lists.members(listID, 'subscribed', {limit: 100})

To request the next page of results (keep in mind the first page (results 1-25) is 0, second (26-50) is 1, etc.):

mailchimp.lists.members(listID, 'subscribed', {start: 1}) # Then to get 51-75 you'd start at 2        

See the source code for an overview of the available options.

You can also look at Mailchimp's API docs for the lists/members endpoint to see available options, defaults, etc.

Upvotes: 2

Related Questions