Facebook icon Twitter icon LinkedIn icon Icon For Mail

Mailchimp API - Add Subscribers to list in PHP

Mailchimp API - Add Subscribers to list in PHP

This article assumes that you already have a Mailchimp account. The API call in the example is made using PHP and Postman is used for API testing.

Create Mailchimp API keys

Go to mailchimp select account, you will find extras dropdown.

Mailchimp Account Extras API Key Menu

Select API keys and in the bottom left you will find a button Create A Key. Click on it and your api key is created.

Mailchimp Create New API Key

You have to copy the API Key under the API Key header.

MailChimp API Key Section

Mailchimp API Key and URL

https://<dc>.api.mailchimp.com/3.0

Replace <dc> part of the URL to the last part of your MailChimp API key, for example us2.

If the api key is XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-us20, url will be https://us20.api.mailchimp.com/3.0

Set the authorization header with your api key as follows.

apikey XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-us20

Create three APIs through Postman

  1. Get lists
  2. Create list
  3. Add member to the list

Get Lists

As you can see in the above image, we just need to do GET request to get the mailing lists.

https://us20.api.mailchimp.com/3.0/lists
{
    "lists": [
        {
            "id": "bxxxxxx1",
            "web_id": 123456,
            "name": "Your company name",
            "contact": {},
            "permission_reminder": "You are receiving this email because you opted in via our website.",
            //...
        }
    ]
}

As you can see the above mailing list takes emails from the Pop up form of the website.

Create New List

If we want to create a new list to add users via api we need to send a POST requst to lists api. To create a list, set the authorization header and content-type to JSON and add body with JSON data

{
    "name": "",
    "contact": {
        "company": "",
        "address1": "",
        "address2": "",
        "city": "",
        "state": "",
        "zip": "",
        "country": "",
        "phone": ""
    },
    "permission_reminder": "",
    "use_archive_bar": "",
    "campaign_defaults": {
        "from_name": "",
        "from_email": "",
        "subject": "",
        "language": ""
    },
    "notify_on_subscribe": "",
    "notify_on_unsubscribe": "",
    "email_type_option": "",
    "visibility": "",
    "double_optin": ""
}

Once you get the list id , you can send a GET request to list all the members in the list.

https://us20.api.mailchimp.com/3.0/lists/<list_id>/members

Add Member to list

Finally to add a member, set the authorization header and content-type to JSON. and add body with JSON data

{
    "email_address":"abc@gmail.com", 
    "status":"subscribed",  
    "merge_fields": {
        "FNAME": "John Doe",
        "PHONE": "97******20"
    }
}

Create a form with the required details for a user. Process the request in php and use the below code to add subscribers to the mailing list.

// API to mailchimp ########################################################
$list_id = 'bxxxxxx1';
$authToken = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-us20';
// The data to send to the API

$postData = array(
    "email_address" => $_POST["email"], 
    "status" => "subscribed", 
    "merge_fields" => array(
    "FNAME"=> $_POST["name"],
    "PHONE"=> $_POST["phone"])
);

// Setup cURL
$ch = curl_init('https://us20.api.mailchimp.com/3.0/lists/'.$list_id.'/members/');
curl_setopt_array($ch, array(
    CURLOPT_POST => TRUE,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_HTTPHEADER => array(
        'Authorization: apikey '.$authToken,
        'Content-Type: application/json'
    ),
    CURLOPT_POSTFIELDS => json_encode($postData)
));
// Send the request
$response = curl_exec($ch);
// #######################################################################

You can fetch the $list_id from the listing api https://us20.api.mailchimp.com/3.0/lists

Sending Custom Fields

Mailchimp allows you to add custom fields to your audience forms. In the next article, we shall see how to add an audience text field and make a request to Mailchimp using PHP.