### 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 *|MC:SUBJECT|*

Newsletter Header

Main content goes here

Footer content

HTML }) puts "Created template ID: #{new_template['id']}" # Update template client.templates.update_template(new_template['id'], { name: 'Updated Newsletter Template', html: '

Updated Content

' }) # Delete template client.templates.delete_template(12345) ``` -------------------------------- ### Configure Mailchimp Marketing Ruby Client Source: https://context7.com/mailchimp/mailchimp-marketing-ruby/llms.txt Demonstrates how to initialize and configure the MailchimpMarketing client using API key (Basic Auth) or OAuth2. It also shows how to set custom timeouts for API requests. ```ruby require 'MailchimpMarketing' # Basic Auth with API Key client = MailchimpMarketing::Client.new client.set_config({ api_key: 'YOUR_API_KEY-us19', server: 'us19' }) # OAuth2 Authentication client = MailchimpMarketing::Client.new client.set_config({ access_token: 'YOUR_OAUTH_ACCESS_TOKEN', server: 'us19' }) # Shorthand configuration at initialization client = MailchimpMarketing::Client.new({ api_key: 'YOUR_API_KEY-us19', server: 'us19', timeout: 300 # Custom timeout in seconds }) # Configure timeouts for different operations client.set_config({ api_key: 'YOUR_API_KEY-us19', server: 'us19', timeout: 120, read_timeout: 180, write_timeout: 120, connect_timeout: 30 }) ``` -------------------------------- ### Configure Mailchimp Client Authentication Source: https://github.com/mailchimp/mailchimp-marketing-ruby/blob/master/README.md Shows how to configure the MailchimpMarketing client using different authentication methods: Basic Auth with an API key, OAuth2 with an access token, or a shorthand method passing configuration directly during client initialization. A server prefix is required for all methods. ```ruby client.set_config({ :api_key => 'YOUR_API_KEY', :server => 'YOUR_SERVER_PREFIX' }) ``` ```ruby client.set_config({ :access_token => 'YOUR_ACCESS_TOKEN', :server => 'YOUR_SERVER_PREFIX' }) ``` ```ruby client = MailchimpMarketing::Client.new({ :api_key => 'YOUR_API_KEY', :server => 'YOUR_SERVER_PREFIX' }) ``` -------------------------------- ### GET /ping Source: https://github.com/mailchimp/mailchimp-marketing-ruby/blob/master/README.md The ping endpoint is used to verify that the API connection is active and the credentials are valid. ```APIDOC ## GET /ping ### Description Checks the health of the Mailchimp API connection. ### Method GET ### Endpoint /ping ### Parameters None ### Request Example N/A ### Response #### Success Response (200) - **health_status** (string) - The status of the API connection. #### Response Example { "health_status": "Everything's Chimpy!" } ``` -------------------------------- ### Manage Ecommerce Resources with Mailchimp Ruby Source: https://context7.com/mailchimp/mailchimp-marketing-ruby/llms.txt Demonstrates how to retrieve orders, products, and customers, as well as create promotional rules and codes. It also covers the deletion of various e-commerce entities. ```ruby orders = client.ecommerce.get_store_orders(store_id, count: 100, has_outreach: true) order_details = client.ecommerce.get_order(store_id, 'ORDER001') products = client.ecommerce.get_all_store_products(store_id, count: 100) customers = client.ecommerce.get_all_store_customers(store_id, count: 100) promo_rule = client.ecommerce.add_promo_rules(store_id, { id: 'PROMO001', title: 'Summer Sale', description: '20% off all items', type: 'percentage', amount: 20.0, target: 'total', starts_at: '2024-06-01T00:00:00+00:00', ends_at: '2024-08-31T23:59:59+00:00' }) promo_code = client.ecommerce.add_promo_code(store_id, 'PROMO001', { id: 'CODE001', code: 'SUMMER20', redemption_url: 'https://mystore.example.com/discount/SUMMER20' }) client.ecommerce.delete_order(store_id, 'ORDER001') client.ecommerce.delete_store_product(store_id, 'PROD001') client.ecommerce.delete_store_customer(store_id, 'CUST001') client.ecommerce.delete_store(store_id) ``` -------------------------------- ### Manage Email Campaigns Source: https://context7.com/mailchimp/mailchimp-marketing-ruby/llms.txt Create, configure, send, and manage email campaigns. Includes support for setting content, scheduling, testing, and lifecycle control. ```ruby require 'MailchimpMarketing' client = MailchimpMarketing::Client.new({ api_key: 'YOUR_API_KEY-us19', server: 'us19' }) new_campaign = client.campaigns.create({ type: 'regular', recipients: { list_id: 'abc123def4' }, settings: { subject_line: 'Welcome!', title: 'Welcome Campaign' } }) campaign_id = new_campaign['id'] client.campaigns.set_content(campaign_id, { html: 'Hello!' }) client.campaigns.send(campaign_id) ``` -------------------------------- ### POST /verified-domains/{domain_name}/actions/verify Source: https://github.com/mailchimp/mailchimp-marketing-ruby/blob/master/README.md Manage verified domains for sending emails. ```APIDOC ## POST /verified-domains/{domain_name}/actions/verify ### Description Submit a domain for verification to be used as a sending domain. ### Method POST ### Endpoint /verified-domains/{domain_name}/actions/verify ### Parameters #### Path Parameters - **domain_name** (string) - Required - The domain name to verify. ### Response #### Success Response (200) - **status** (string) - The verification status. ``` -------------------------------- ### Retrieve Campaign Reports with Mailchimp Ruby Source: https://context7.com/mailchimp/mailchimp-marketing-ruby/llms.txt Provides methods to access campaign performance metrics, including open rates, click details, subscriber activity, and geographic data. Requires a configured Mailchimp client instance. ```ruby require 'MailchimpMarketing' client = MailchimpMarketing::Client.new({ api_key: 'YOUR_API_KEY-us19', server: 'us19' }) reports = client.reports.get_all_campaign_reports(type: 'regular', count: 50, since_send_time: '2024-01-01T00:00:00Z') report = client.reports.get_campaign_report('campaign123') click_details = client.reports.get_campaign_click_details('campaign123', count: 100) clickers = client.reports.get_subscribers_info('campaign123', 'link123', count: 100) open_details = client.reports.get_campaign_open_details('campaign123', count: 100) email_activity = client.reports.get_email_activity_for_campaign('campaign123', count: 500) subscriber_hash = Digest::MD5.hexdigest('subscriber@example.com'.downcase) activity = client.reports.get_email_activity_for_subscriber('campaign123', subscriber_hash) unsubscribes = client.reports.get_unsubscribed_list_for_campaign('campaign123', count: 100) recipients = client.reports.get_campaign_recipients('campaign123', count: 100) domain_stats = client.reports.get_domain_performance_for_campaign('campaign123') ecom_activity = client.reports.get_ecommerce_product_activity_for_campaign('campaign123', count: 50, sort_field: 'total_revenue') advice = client.reports.get_campaign_advice('campaign123') abuse_reports = client.reports.get_campaign_abuse_reports('campaign123') locations = client.reports.get_locations_for_campaign('campaign123', count: 100) ``` -------------------------------- ### Manage Automation Workflows Source: https://context7.com/mailchimp/mailchimp-marketing-ruby/llms.txt Retrieve and list automated email workflows such as welcome series or abandoned cart reminders. ```ruby require 'MailchimpMarketing' client = MailchimpMarketing::Client.new({ api_key: 'YOUR_API_KEY-us19', server: 'us19' }) automations = client.automations.list(status: 'sending', count: 50) automations['automations'].each do |auto| puts "#{auto['id']}: #{auto['settings']['title']} - #{auto['status']}" end ``` -------------------------------- ### Batch Webhooks API Source: https://github.com/mailchimp/mailchimp-marketing-ruby/blob/master/README.md Endpoints for managing batch webhooks, allowing for efficient processing of multiple webhook events. ```APIDOC ## DELETE /batch-webhooks/{batch_webhook_id} ### Description Deletes a batch webhook. ### Method DELETE ### Endpoint /batch-webhooks/{batch_webhook_id} ### Parameters #### Path Parameters - **batch_webhook_id** (string) - Required - The ID of the batch webhook to delete. ### Response #### Success Response (200) - **status** (string) - Indicates the success of the deletion operation. ``` ```APIDOC ## GET /batch-webhooks/{batch_webhook_id} ### Description Retrieves details for a specific batch webhook. ### Method GET ### Endpoint /batch-webhooks/{batch_webhook_id} ### Parameters #### Path Parameters - **batch_webhook_id** (string) - Required - The ID of the batch webhook to retrieve. ### Response #### Success Response (200) - **id** (string) - The ID of the batch webhook. - **url** (string) - The URL where the webhook events will be sent. - **created_at** (string) - The timestamp when the webhook was created. - **events** (object) - An object detailing the types of events to send. ``` ```APIDOC ## GET /batch-webhooks ### Description Lists all configured batch webhooks. ### Method GET ### Endpoint /batch-webhooks ### Response #### Success Response (200) - **batch_webhooks** (array) - A list of batch webhooks. - **id** (string) - The ID of the batch webhook. - **url** (string) - The URL where the webhook events will be sent. - **created_at** (string) - The timestamp when the webhook was created. - **total_items** (integer) - The total number of batch webhooks. ``` ```APIDOC ## PUT /batch-webhooks/{batch_webhook_id} ### Description Updates an existing batch webhook. ### Method PUT ### Endpoint /batch-webhooks/{batch_webhook_id} ### Parameters #### Path Parameters - **batch_webhook_id** (string) - Required - The ID of the batch webhook to update. ### Request Body - **url** (string) - Optional - The new URL for the webhook. - **events** (object) - Optional - An object detailing the types of events to send. ### Request Example ```json { "url": "https://new.example.com/webhook" } ``` ### Response #### Success Response (200) - **id** (string) - The ID of the updated batch webhook. - **url** (string) - The updated URL. - **created_at** (string) - The timestamp when the webhook was created. ``` ```APIDOC ## POST /batch-webhooks ### Description Creates a new batch webhook. ### Method POST ### Endpoint /batch-webhooks ### Request Body - **url** (string) - Required - The URL where the webhook events will be sent. - **events** (object) - Required - An object detailing the types of events to send. ### Request Example ```json { "url": "https://example.com/webhook", "events": { "subscribe": true, "unsubscribe": true } } ``` ### Response #### Success Response (200) - **id** (string) - The ID of the newly created batch webhook. - **url** (string) - The URL for the webhook. - **created_at** (string) - The timestamp when the webhook was created. ``` -------------------------------- ### POST /file-manager/files Source: https://github.com/mailchimp/mailchimp-marketing-ruby/blob/master/README.md Uploads a new file to the Mailchimp file manager. ```APIDOC ## POST /file-manager/files ### Description Upload a new file to the account's file manager. ### Method POST ### Endpoint /file-manager/files ### Parameters #### Request Body - **file_data** (binary) - Required - The file content to upload. ### Request Example POST /file-manager/files ### Response #### Success Response (200) - **id** (string) - The ID of the uploaded file. #### Response Example { "id": "file_456" } ```