Reputation: 4050
In my app I want to auto-subscribe users to a MailChimp list when they are created. All of the documentation I have found focuses on MailChip API 2.0 and Gibbon 1.x.
Here is what I have so far:
I created a .env file with:
MAILCHIMP_API_KEY = "my_api_key"
MAILCHIMP_LIST_ID = "my_list_id"
I created an initializer called gibbon.rb with:
Gibbon::Request.api_key = ENV["MAILCHIMP_API_KEY"]
Gibbon::Request.timeout = 15
Then created a Job:
class SubscribeUserToMailingListJob < ActiveJob::Base
queue_as :default
def perform(user)
gibbon = Gibbon::Request.new
# I am getting errors from this line
gibbon.lists(ENV["MAILCHIMP_LIST_ID"]).members.create(body: {email_address: user.email, status: "subscribed", merge_fields: {FNAME: user.name, LNAME: ""}})
end
end
In my user.rb I've this after check
after_create :subscribe_user_to_mailing_list
And added this under private:
def subscribe_user_to_mailing_list
SubscribeUserToMailingListJob.perform_later(self)
end
I am getting a 400 error from gibbon.lists(ENV["MAILCHIMP_LIST_ID"]).members.create(body: {email_address: user.email, status: "subscribed", merge_fields: {FNAME: user.name, LNAME: ""}})
********EDIT********
Per this conversation.
I think the error was occurring because the email address already existed in the list. The first time I got an error, the email address was added to the MailChimp list but the user was not created in my app due to another bug. Now when I try to create a user with the same email address the error 400 pops up because the email already exists. This also prevents the user from being created in my app.
Can somebody with a little more experience please verify if this is correct?
Upvotes: 2
Views: 808
Reputation: 4643
You're correct -- if you're making a POST
call and the thing you're posting already exists, you'll get an error back. Until the API supports PUT
, update or subscribe is a two-step operation. Check to see if the subscriber exists and then use the appropriate method. Or, you can just try to PATCH
first and do a POST
if that fails with a 404.
Upvotes: 1