[RUBY] Story of touching mailchimp-api v3

About this article

I touched the new mailchimp api recently I will explain the introduction method and how to use it quickly.

Until now, only the old sdk of here was prepared, so it was like when it could not be used. (maybe)

This time, the api has been renewed and a new official sdk has been released, so I've touched it for a transfer, so I'll keep it as a record. https://mailchimp.com/developer/blog/

Environment, prerequisites, etc.

(environment)

(Prerequisite) --You already have a mailchimp account

(API to touch)

Advance preparation

Add Gem

gem 'MailchimpMarketing'

Creating an API key for mailchimp

It was in a place that was a little difficult to understand.

Account> Extras> API Key> Create A Key button

A key will be generated, so this is OK

Client preparation

I wrote it if I look at the reference, but ...


mailchimp = MailchimpMarketing::Client.new
mailchimp.set_config({
  :api_key => 'YOUR_API_KEY',
  :server => 'YOUR_SERVER_PREFIX'
})

mailchimp.ping.get
# {"health_status": "Everything's Chimpy!"}OK if

YOUR_SERVER_PREFIX adds the characters after - in the API key. If it is xxxxxxxxxxxxx-us8, then ʻus8`

I wrote it in here, but I didn't read it properly and the domain of the service If I put it in properly, I couldn't connect and I got stuck for about an hour. (Stupid)

Basic operation method

#List list
mailchimp.lists.get_all_lists

#Add list
#email ... the email you want to add_address
#* If the address already exists, it will be an exception, so it is better to use put depending on the case.
mailchimp.lists.add_list_member(
  'list_id',
  {
    email_address: email
  }
)

#List put
#email ・ ・ ・ email you want to put_address
# ※subscriber_hash corresponds to the MD5 hash of the email address.
mailchimp.lists.set_list_member(
 'list_id',
  Digest::MD5.hexdigest(email),
  {
    email_address: email,
    status_if_new: "subscribed"
  }
)

#Get members
#email ・ ・ ・ Email you want to search_address
#* If it does not exist, it will be an exception, so it is better not to use it for existence confirmation.
client.lists.get_list_member('list_id', Digest::MD5.hexdigest(email))

#Search for members from the list
#* I can't search by email or MD hash ... It's subtle, get_list_You can use member ...
#Unique when registered_email_An item called id is generated, so use it for email-based existence verification.
mailchimp.lists.get_list_members_info(
  MAIL_CHIMP_LIST_ID,
  {
    unique_email_id: unique_email_id,
    status: 'subscribed'
  }
)

#Member update
#Email you want to update
mailchimp.lists.update_list_member(
 'list_id',
  Digest::MD5.hexdigest(email),
  {
    status: 'unsubscribed' #Unsubscribe
  }
)

Reference source

mailchimp Official Reference

Recommended Posts

Story of touching mailchimp-api v3
Story of passing Java Gold SE8
The story of @ViewScoped consuming memory