### Quick Start Mailchimp API Call Source: https://github.com/mailchimp/mailchimp-marketing-ruby/blob/master/README.md A basic example of how to initialize the MailchimpMarketing client, set configuration with API key and server prefix, and make a ping request to verify connectivity. Handles potential API errors. ```ruby require 'MailchimpMarketing' begin client = MailchimpMarketing::Client.new() client.set_config({ :api_key => 'YOUR_API_KEY', :server => 'YOUR_SERVER_PREFIX' }) result = client.ping.get() p result rescue MailchimpMarketing::ApiError => e puts "Error: #{e}" end ``` -------------------------------- ### Install MailchimpMarketing Gem Source: https://github.com/mailchimp/mailchimp-marketing-ruby/blob/master/README.md Demonstrates how to install the MailchimpMarketing Ruby gem using RubyGems, Git, or a local gem file. Ensure you have Ruby and RubyGems installed. ```shell gem install MailchimpMarketing ``` ```shell gem 'MailchimpMarketing', :git => 'https://github.com/mailchimp/mailchimp-marketing-ruby.git' ``` ```shell gem build MailchimpMarketing.gemspec gem install ./MailchimpMarketing-3.0.80.gem gem 'MailchimpMarketing', '~> 3.0.80' ``` -------------------------------- ### Install Mailchimp Marketing Ruby Gem Source: https://context7.com/mailchimp/mailchimp-marketing-ruby/llms.txt Instructions for installing the MailchimpMarketing gem using RubyGems, Bundler, or directly from a Git repository. ```ruby gem install MailchimpMarketing ``` ```ruby gem 'MailchimpMarketing', '~> 3.0.80' ``` ```ruby gem 'MailchimpMarketing', git: 'https://github.com/mailchimp/mailchimp-marketing-ruby.git' ``` -------------------------------- ### Execute Batch Operations in Ruby Source: https://context7.com/mailchimp/mailchimp-marketing-ruby/llms.txt Shows how to bundle multiple API requests into a single batch operation to improve performance. Includes starting a batch, checking status, and managing existing batches. ```ruby batch = client.batches.start({ operations: [ { method: 'POST', path: '/lists/abc123def4/members', body: '{"email_address":"user1@example.com","status":"subscribed"}' }, { method: 'POST', path: '/lists/abc123def4/members', body: '{"email_address":"user2@example.com","status":"subscribed"}' }, { method: 'PATCH', path: '/lists/abc123def4/members/md5hash123', body: '{"merge_fields":{"FNAME":"Updated"}}' } ] }) batch_id = batch['id'] status = client.batches.status(batch_id) all_batches = client.batches.list(count: 50) client.batches.delete_request(batch_id) ``` -------------------------------- ### Manage Mailchimp Automation Workflows Source: https://context7.com/mailchimp/mailchimp-marketing-ruby/llms.txt Demonstrates how to perform CRUD operations on automation workflows and their associated emails. Includes methods to start, pause, and manage subscriber queues within specific workflow emails. ```ruby automation = client.automations.get('workflow123') new_automation = client.automations.create({ recipients: { list_id: 'abc123def4' }, trigger_settings: { workflow_type: 'welcomeSeries' }, settings: { title: 'Welcome Series', from_name: 'Welcome Team', reply_to: 'hello@example.com' } }) client.automations.update_workflow_email(workflow_id, 'email123', { settings: { subject_line: 'Welcome to our community!', preview_text: 'We are glad you joined us' }, delay: { amount: 1, type: 'day', direction: 'after', action: 'signup' } }) client.automations.start_all_emails(workflow_id) client.automations.archive(workflow_id) ``` -------------------------------- ### GET /lists Source: https://github.com/mailchimp/mailchimp-marketing-ruby/blob/master/README.md Retrieves a list of all audience lists in the account. ```APIDOC ## GET /lists ### Description Retrieve all audience lists associated with the authenticated account. ### Method GET ### Endpoint /lists ### Parameters None ### Request Example GET /lists ### Response #### Success Response (200) - **lists** (array) - A collection of list objects. #### Response Example { "lists": [ { "id": "123", "name": "My List" } ] } ``` -------------------------------- ### Manage Mailchimp Segments and Tags Source: https://context7.com/mailchimp/mailchimp-marketing-ruby/llms.txt Covers the creation and management of segments and tags for targeted subscriber communication. Includes examples for creating saved segments, static segments, and retrieving member-specific tags. ```ruby segments = client.lists.list_segments(list_id, count: 100) new_segment = client.lists.create_segment(list_id, { name: 'Active VIP Customers', options: { match: 'all', conditions: [{ condition_type: 'EmailActivity', field: 'campaign_activity', op: 'member_opened', value: 3 }] } }) client.lists.batch_segment_members({ members_to_add: ['add1@example.com'], members_to_remove: ['remove@example.com'] }, list_id, segment_id) subscriber_hash = Digest::MD5.hexdigest('subscriber@example.com'.downcase) member_tags = client.lists.get_list_member_tags(list_id, subscriber_hash) ``` -------------------------------- ### Manage Mailchimp Lists (Audiences) with Ruby Source: https://context7.com/mailchimp/mailchimp-marketing-ruby/llms.txt Code examples for interacting with the Mailchimp Lists API using the Ruby client. This includes fetching all lists, retrieving specific list details, creating new lists, and adding/updating/upserting list members (subscribers). ```ruby require 'MailchimpMarketing' client = MailchimpMarketing::Client.new({ api_key: 'YOUR_API_KEY-us19', server: 'us19' }) # Get all lists with pagination lists = client.lists.get_all_lists( count: 10, offset: 0, sort_field: 'date_created', sort_dir: 'DESC' ) puts "Total lists: #{lists['total_items']}" lists['lists'].each { |list| puts "#{list['id']}: #{list['name']}" } # Get specific list details list = client.lists.get_list('abc123def4', include_total_contacts: true) puts "List: #{list['name']} - #{list['stats']['member_count']} members" # Create a new list new_list = client.lists.create_list({ name: 'My Newsletter', contact: { company: 'My Company', address1: '123 Main St', city: 'Atlanta', state: 'GA', zip: '30308', country: 'US' }, permission_reminder: 'You signed up for this newsletter on our website.', campaign_defaults: { from_name: 'Newsletter Team', from_email: 'newsletter@example.com', subject: '', language: 'en' }, email_type_option: true }) puts "Created list ID: #{new_list['id']}" # Add a new subscriber subscriber = client.lists.add_list_member('abc123def4', { email_address: 'subscriber@example.com', status: 'subscribed', # subscribed, unsubscribed, cleaned, pending merge_fields: { FNAME: 'John', LNAME: 'Doe' }, tags: ['new-customer', 'website-signup'] }) puts "Added subscriber: #{subscriber['email_address']}" # Update existing subscriber (use MD5 hash of lowercase email) require 'digest' subscriber_hash = Digest::MD5.hexdigest('subscriber@example.com'.downcase) updated = client.lists.update_list_member('abc123def4', subscriber_hash, { merge_fields: { FNAME: 'Jonathan' }, status: 'subscribed' }) # Add or update subscriber (upsert) upserted = client.lists.set_list_member('abc123def4', subscriber_hash, { email_address: 'subscriber@example.com', status_if_new: 'subscribed', merge_fields: { FNAME: 'John', LNAME: 'Doe' } }) # Get list members with filters members = client.lists.get_list_members_info('abc123def4', status: 'subscribed', count: 100, sort_field: 'last_changed', sort_dir: 'DESC' ) ``` -------------------------------- ### E-commerce Integration with Mailchimp Ruby API Source: https://context7.com/mailchimp/mailchimp-marketing-ruby/llms.txt This section provides Ruby code examples for integrating e-commerce data with Mailchimp. It demonstrates how to manage stores, products, customers, orders, and abandoned carts, including creating, updating, and retrieving these resources. ```ruby require 'MailchimpMarketing' client = MailchimpMarketing::Client.new({ api_key: 'YOUR_API_KEY-us19', server: 'us19' }) # List all connected stores stores = client.ecommerce.stores(count: 50) # Create a new store new_store = client.ecommerce.add_store({ id: 'store_001', list_id: 'abc123def4', name: 'My Online Store', domain: 'mystore.example.com', email_address: 'store@example.com', currency_code: 'USD', primary_locale: 'en', timezone: 'America/New_York', platform: 'Custom' }) store_id = 'store_001' # Add a product product = client.ecommerce.add_store_product(store_id, { id: 'PROD001', title: 'Awesome T-Shirt', handle: 'awesome-tshirt', url: 'https://mystore.example.com/products/awesome-tshirt', description: 'The most awesome t-shirt ever', type: 'Apparel', vendor: 'My Brand', image_url: 'https://mystore.example.com/images/tshirt.jpg', variants: [ { id: 'VAR001', title: 'Small', url: 'https://mystore.example.com/products/awesome-tshirt?size=s', price: 29.99, inventory_quantity: 100 }, { id: 'VAR002', title: 'Medium', url: 'https://mystore.example.com/products/awesome-tshirt?size=m', price: 29.99, inventory_quantity: 150 }, { id: 'VAR003', title: 'Large', url: 'https://mystore.example.com/products/awesome-tshirt?size=l', price: 29.99, inventory_quantity: 75 } ] }) # Add a customer customer = client.ecommerce.add_store_customer(store_id, { id: 'CUST001', email_address: 'customer@example.com', opt_in_status: true, first_name: 'Jane', last_name: 'Smith', address: { address1: '456 Oak Ave', city: 'Chicago', province: 'IL', postal_code: '60601', country: 'US' } }) # Add or update customer (upsert) client.ecommerce.set_store_customer(store_id, 'CUST001', { id: 'CUST001', email_address: 'customer@example.com', opt_in_status: true, orders_count: 5, total_spent: 500.00 }) # Add an order order = client.ecommerce.add_store_order(store_id, { id: 'ORDER001', customer: { id: 'CUST001' }, currency_code: 'USD', order_total: 89.97, tax_total: 7.20, shipping_total: 5.99, processed_at_foreign: '2024-01-15T14:30:00+00:00', financial_status: 'paid', fulfillment_status: 'shipped', shipping_address: { name: 'Jane Smith', address1: '456 Oak Ave', city: 'Chicago', province: 'IL', postal_code: '60601', country: 'US' }, lines: [ { id: 'LINE001', product_id: 'PROD001', product_variant_id: 'VAR002', quantity: 3, price: 29.99 } ] }) # Add an abandoned cart cart = client.ecommerce.add_store_cart(store_id, { id: 'CART001', customer: { id: 'CUST001', email_address: 'customer@example.com', opt_in_status: true }, currency_code: 'USD', order_total: 59.98, checkout_url: 'https://mystore.example.com/checkout/CART001', lines: [ { id: 'CARTLINE001', product_id: 'PROD001', product_variant_id: 'VAR001', quantity: 2, price: 29.99 } ] }) ``` -------------------------------- ### GET /templates/{template_id} Source: https://github.com/mailchimp/mailchimp-marketing-ruby/blob/master/README.md Operations for managing individual templates. ```APIDOC ## GET /templates/{template_id} ### Description Retrieve, update, or delete a specific template by its ID. ### Method GET / PATCH / DELETE ### Endpoint /templates/{template_id} ### Parameters #### Path Parameters - **template_id** (string) - Required - The unique identifier for the template. ### Response #### Success Response (200) - **id** (string) - The template ID. - **name** (string) - The name of the template. ``` -------------------------------- ### GET /template-folders Source: https://github.com/mailchimp/mailchimp-marketing-ruby/blob/master/README.md Operations for managing template folders in Mailchimp. ```APIDOC ## GET /template-folders ### Description Retrieve a list of template folders or create a new one. ### Method GET / POST ### Endpoint /template-folders ### Parameters None ### Response #### Success Response (200) - **folders** (array) - List of template folder objects. ``` -------------------------------- ### Perform Mailchimp API Health Check (Ping) Source: https://context7.com/mailchimp/mailchimp-marketing-ruby/llms.txt Example of how to use the MailchimpMarketing client to ping the API endpoint to verify connectivity and authentication. It includes basic error handling for API errors. ```ruby require 'MailchimpMarketing' client = MailchimpMarketing::Client.new({ api_key: 'YOUR_API_KEY-us19', server: 'us19' }) begin response = client.ping.get puts response # => {"health_status"=>"Everything's Chimpy!"} rescue MailchimpMarketing::ApiError => e puts "Connection failed: #{e.message}" end ``` -------------------------------- ### Manage Templates with Mailchimp Ruby API Source: https://context7.com/mailchimp/mailchimp-marketing-ruby/llms.txt This section details how to manage email templates using the Mailchimp Marketing Ruby client. It covers listing all templates, retrieving specific template details, getting default content, creating new templates with HTML, updating existing templates, and deleting templates. ```ruby require 'MailchimpMarketing' client = MailchimpMarketing::Client.new({ api_key: 'YOUR_API_KEY-us19', server: 'us19' }) # List all templates templates = client.templates.list( type: 'user', # user, base, gallery count: 50, sort_field: 'date_edited', sort_dir: 'DESC' ) templates['templates'].each do |t| puts "#{t['id']}: #{t['name']} (#{t['type']})" end # Get template details template = client.templates.get_template(12345) puts "Template: #{template['name']}" puts "Created: #{template['date_created']}" # Get template default content (rendered HTML) content = client.templates.get_default_content_for_template(12345) puts content['html'] # Create a new template new_template = client.templates.create({ name: 'Monthly Newsletter Template', folder_id: 'folder123', html: <<~HTML
Main content goes here
Footer content