MailChimp API

I've been doing a lot of work with the MailChimp API lately and figured it would be good to have a page dedicated to it here for future reference because it's not very well documented online anywhere else... Hoping this will help some people who also have to use MailChimp's API.

Understanding MailChimp Lists and the API

If you've ever dealt with MailChimp, you know that it is based on lists which can be segmented, grouped and managed all from the API as needed. Next I will demonstrate how to get a list of MailChimp lists and information on each list that exists.

Getting MailChimp List Information from API

Here's the code I created to get all MailChimp Lists and information on each list from the MailChimp API using PHP code:

<h2>MailChimp List Info:</h2>
<?php
//Get API key :

$mcAPIkey ='Enter_your_own_API_key_HERE';
//get info regarding MC lists
//use following url 'https://usX.api.mailchimp.com/3.0/lists'

$dc = substr( $mcAPIkey, strpos( $mcAPIkey, '-' ) + 1 ); // datacenter, it is the part of your api key - us5, us8 etc
$args = array(
     'headers' => array(
        'Authorization' => 'Basic ' . base64_encode( 'user:'. $mcAPIkey )
    )
);

$response = wp_remote_get( 'https://'.$dc.'.api.mailchimp.com/3.0/lists/', $args );
$body = json_decode(wp_remote_retrieve_body($response));

$rcode = wp_remote_retrieve_response_code($response);
$tot_itms = $body->total_items;
echo "Response code was $rcode ...$tot_itms items found!<br /><hr />";
echo "<pre><code>";
print_r($body);
echo "</code></pre><hr />";
?>

The above code would return something like this for a MailChimp account that has one saved list:

MailChimp List Info:

Response code was 200 ...1 items found!


stdClass Object
(
    [lists] => Array
        (
            [0] => stdClass Object
                (
                    [id] => xxxxxxxxx
                    [web_id] => xxxxxx
                    [name] => List Name
                    [contact] => stdClass Object
                        (
                            [company] => Company Name
                            [address1] => 1001 Your Road
                            [address2] => 
                            [city] => Adolphus
                            [state] => KY
                            [zip] => 42120
                            [country] => US
                            [phone] => 
                        )

                    [permission_reminder] => You signed up to learn more about how our team can better serve you.
                    [use_archive_bar] => 1
                    [campaign_defaults] => stdClass Object
                        (
                            [from_name] => First Last
                            [from_email] => your_email@domain.com
                            [subject] => 
                            [language] => en
                        )

                    [notify_on_subscribe] => 
                    [notify_on_unsubscribe] => 
                    [date_created] => 2018-02-25T02:01:38+00:00
                    [list_rating] => 0
                    [email_type_option] => 
                    [subscribe_url_short] => http://eepurl.com/dBXF4r
                    [subscribe_url_long] => https://yourcompany.us13.list-manage.com/subscribe?u=bf405c1xxxxxxxxb48df64c3&id=xxxxxxxxxx
                    [beamer_address] => us13-5df785xxxxx-xxx@inbound.mailchimp.com
                    [visibility] => pub
                    [double_optin] => 1
                    [marketing_permissions] => 
                    [modules] => Array
                        (
                        )

                    [stats] => stdClass Object
                        (
                            [member_count] => 3
                            [unsubscribe_count] => 0
                            [cleaned_count] => 0
                            [member_count_since_send] => 5
                            [unsubscribe_count_since_send] => 0
                            [cleaned_count_since_send] => 0
                            [campaign_count] => 2
                            [campaign_last_sent] => 
                            [merge_field_count] => 4
                            [avg_sub_rate] => 1
                            [avg_unsub_rate] => 0
                            [target_sub_rate] => 0
                            [open_rate] => 0
                            [click_rate] => 0
                            [last_sub_date] => 2018-04-28T20:26:38+00:00
                            [last_unsub_date] => 
                        )

                    [_links] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [rel] => self
                                    [href] => https://us13.api.mailchimp.com/3.0/lists/edxxxxxxxxx
                                    [method] => GET
                                    [targetSchema] => https://us13.api.mailchimp.com/schema/3.0/Definitions/Lists/Response.json
                                )

                            [1] => stdClass Object
                                (
                                    [rel] => parent
                                    [href] => https://us13.api.mailchimp.com/3.0/lists
                                    [method] => GET
                                    [targetSchema] => https://us13.api.mailchimp.com/schema/3.0/Definitions/Lists/CollectionResponse.json
                                    [schema] => https://us13.api.mailchimp.com/schema/3.0/CollectionLinks/Lists.json
                                )

                            [2] => stdClass Object
                                (
                                    [rel] => update
                                    [href] => https://us13.api.mailchimp.com/3.0/lists/edxxxxxxxxx
                                    [method] => PATCH
                                    [targetSchema] => https://us13.api.mailchimp.com/schema/3.0/Definitions/Lists/Response.json
                                    [schema] => https://us13.api.mailchimp.com/schema/3.0/Definitions/Lists/PATCH.json
                                )

                            [3] => stdClass Object
                                (
                                    [rel] => batch-sub-unsub-members
                                    [href] => https://us13.api.mailchimp.com/3.0/lists/xxxxxxxxxx
                                    [method] => POST
                                    [targetSchema] => https://us13.api.mailchimp.com/schema/3.0/Definitions/Lists/BatchPOST-Response.json
                                    [schema] => https://us13.api.mailchimp.com/schema/3.0/Definitions/Lists/BatchPOST.json
                                )

                            [4] => stdClass Object
                                (
                                    [rel] => delete
                                    [href] => https://us13.api.mailchimp.com/3.0/lists/xxxxxxxxxxxx
                                    [method] => DELETE
                                )

                            [5] => stdClass Object
                                (
                                    [rel] => abuse-reports
                                    [href] => https://us13.api.mailchimp.com/3.0/lists/xxxxxxxx/abuse-reports
                                    [method] => GET
                                    [targetSchema] => https://us13.api.mailchimp.com/schema/3.0/Definitions/Lists/Abuse/CollectionResponse.json
                                    [schema] => https://us13.api.mailchimp.com/schema/3.0/CollectionLinks/Lists/Abuse.json
                                )

                            [6] => stdClass Object
                                (
                                    [rel] => activity
                                    [href] => https://us13.api.mailchimp.com/3.0/lists/xxxxxxxxx/activity
                                    [method] => GET
                                    [targetSchema] => https://us13.api.mailchimp.com/schema/3.0/Definitions/Lists/Activity/Response.json
                                )

                            [7] => stdClass Object
                                (
                                    [rel] => clients
                                    [href] => https://us13.api.mailchimp.com/3.0/lists/xxxxxxxxxxxxx/clients
                                    [method] => GET
                                    [targetSchema] => https://us13.api.mailchimp.com/schema/3.0/Definitions/Lists/Clients/Response.json
                                )

                            [8] => stdClass Object
                                (
                                    [rel] => growth-history
                                    [href] => https://us13.api.mailchimp.com/3.0/lists/xxxxxxxxxxxxxxxx/growth-history
                                    [method] => GET
                                    [targetSchema] => https://us13.api.mailchimp.com/schema/3.0/Definitions/Lists/Growth/CollectionResponse.json
                                    [schema] => https://us13.api.mailchimp.com/schema/3.0/CollectionLinks/Lists/Growth.json
                                )

                            [9] => stdClass Object
                                (
                                    [rel] => interest-categories
                                    [href] => https://us13.api.mailchimp.com/3.0/lists/xxxxxxxxxx/interest-categories
                                    [method] => GET
                                    [targetSchema] => https://us13.api.mailchimp.com/schema/3.0/Definitions/Lists/InterestCategories/CollectionResponse.json
                                    [schema] => https://us13.api.mailchimp.com/schema/3.0/CollectionLinks/Lists/InterestCategories.json
                                )

                            [10] => stdClass Object
                                (
                                    [rel] => members
                                    [href] => https://us13.api.mailchimp.com/3.0/lists/xxxxxxxxx/members
                                    [method] => GET
                                    [targetSchema] => https://us13.api.mailchimp.com/schema/3.0/Definitions/Lists/Members/CollectionResponse.json
                                    [schema] => https://us13.api.mailchimp.com/schema/3.0/CollectionLinks/Lists/Members.json
                                )

                            [11] => stdClass Object
                                (
                                    [rel] => merge-fields
                                    [href] => https://us13.api.mailchimp.com/3.0/lists/xxxxxxxxxx/merge-fields
                                    [method] => GET
                                    [targetSchema] => https://us13.api.mailchimp.com/schema/3.0/Definitions/Lists/MergeFields/CollectionResponse.json
                                    [schema] => https://us13.api.mailchimp.com/schema/3.0/CollectionLinks/Lists/MergeFields.json
                                )

                            [12] => stdClass Object
                                (
                                    [rel] => segments
                                    [href] => https://us13.api.mailchimp.com/3.0/lists/xxxxxxxxxxxx/segments
                                    [method] => GET
                                    [targetSchema] => https://us13.api.mailchimp.com/schema/3.0/Definitions/Lists/Segments/CollectionResponse.json
                                    [schema] => https://us13.api.mailchimp.com/schema/3.0/CollectionLinks/Lists/Segments.json
                                )

                            [13] => stdClass Object
                                (
                                    [rel] => webhooks
                                    [href] => https://us13.api.mailchimp.com/3.0/lists/xxxxxxxx/webhooks
                                    [method] => GET
                                    [targetSchema] => https://us13.api.mailchimp.com/schema/3.0/Definitions/Lists/Webhooks/CollectionResponse.json
                                    [schema] => https://us13.api.mailchimp.com/schema/3.0/CollectionLinks/Lists/Webhooks.json
                                )

                            [14] => stdClass Object
                                (
                                    [rel] => signup-forms
                                    [href] => https://us13.api.mailchimp.com/3.0/lists/xxxxxxxxxxxx/signup-forms
                                    [method] => GET
                                    [targetSchema] => https://us13.api.mailchimp.com/schema/3.0/Definitions/Lists/SignupForms/CollectionResponse.json
                                    [schema] => https://us13.api.mailchimp.com/schema/3.0/CollectionLinks/Lists/SignupForms.json
                                )

                            [15] => stdClass Object
                                (
                                    [rel] => locations
                                    [href] => https://us13.api.mailchimp.com/3.0/lists/xxxxxxxxxxx/locations
                                    [method] => GET
                                    [targetSchema] => https://us13.api.mailchimp.com/schema/3.0/Definitions/Lists/Locations/CollectionResponse.json
                                    [schema] => https://us13.api.mailchimp.com/schema/3.0/CollectionLinks/Lists/Locations.json
                                )

                        )

                )

        )

    [total_items] => 1
    [_links] => Array
        (
            [0] => stdClass Object
                (
                    [rel] => self
                    [href] => https://us13.api.mailchimp.com/3.0/lists
                    [method] => GET
                    [targetSchema] => https://us13.api.mailchimp.com/schema/3.0/Definitions/Lists/CollectionResponse.json
                    [schema] => https://us13.api.mailchimp.com/schema/3.0/CollectionLinks/Lists.json
                )

            [1] => stdClass Object
                (
                    [rel] => parent
                    [href] => https://us13.api.mailchimp.com/3.0/
                    [method] => GET
                    [targetSchema] => https://us13.api.mailchimp.com/schema/3.0/Definitions/Root/Response.json
                )

            [2] => stdClass Object
                (
                    [rel] => create
                    [href] => https://us13.api.mailchimp.com/3.0/lists
                    [method] => POST
                    [targetSchema] => https://us13.api.mailchimp.com/schema/3.0/Definitions/Lists/Response.json
                    [schema] => https://us13.api.mailchimp.com/schema/3.0/Definitions/Lists/POST.json
                )

        )

)

Leave a Reply

Your email address will not be published. Required fields are marked *