
How to Add Subscribers to Mailchimp API

Hello Everyone, in this article,Mangcoding will share information on how to add subscribers using the Mailchimp API. Besides that, we will also provide an example. Please read until the end to find out how this works well. Let’s get started!
In this example, we will use PHP and cURL to connect to the Mailchimp API. We will also provide a tutorial with a WordPress example.
Below, we have prepared the complete code, ready to use. Please pay attention to the following code:
// let's start with some variables
$api_key = 'YOUR MAILCHIMP API KEY HERE';
$email = '[email protected]'; // the user we are going to subscribe
$status = 'subscribed'; // we are going to talk about it in just a little bit
$merge_fields = array( 'FNAME' => 'Misha' ); // FNAME, LNAME or something else
$list_id = ''; // List / Audience ID
// start our Mailchimp connection
$connection = curl_init();
curl_setopt(
$connection,
CURLOPT_URL,
'https://' . substr( $api_key, strpos( $api_key, '-' ) + 1 ) . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/' . md5( strtolower( $email ) )
);
curl_setopt( $connection, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Authorization: Basic '. base64_encode( 'user:'.$api_key ) ) );
curl_setopt( $connection, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $connection, CURLOPT_CUSTOMREQUEST, 'PUT' );
curl_setopt( $connection, CURLOPT_POST, true );
curl_setopt( $connection, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt(
$connection,
CURLOPT_POSTFIELDS,
json_encode( array(
'apikey' => $api_key,
'email_address' => $email,
'status' => $status,
'merge_fields' => $merge_fields,
//'tags' => array( 'Coffee', 'Snowboard' ) // you can specify some tags here as well
) )
);
$result = curl_exec( $connection );
From the code above, let’s break down the entire example into smaller parts and explain each line of code.
- Online, we use the subscribed status because we want to subscribe the user. If your goal is to unsubscribe a specific user via the Mailchimp API, you can change the status to unsubscribed.
Or pending if you are required to send a confirmation email. - $result is a JSON object, so to process the result from the API, you need to use json_decode(). Example: print_r( json_decode( $results ) );
- You may also notice that the API key is used as part of the URL and for basic authentication.
- We will discuss how to get the API key and Audience ID.
$result = json_decode( $result );
if( 400 === $result->status ){
foreach( $result->errors as $error ) {
echo '
Error: ' . $error->message . '
';
}
} elseif( 'subscribed' === $result->status ){
echo "
Thank you, {$result->full_name}!
";
}
The first line of our code requires us to define our Mailchimp API key. We’re sure you already know how to get it, but just in case, here’s a reminder:
To access it, Log in to your Mailchimp dashboard and click on your profile picture in the bottom left corner of the screen. Then select Account & Billing.
After that, on the Account & Billing page, go to Extras and select API Keys, as shown below
Here’s how, log in to Mailchimp, then go to Audiences (Lists), click on the title of your list, then in the list menu go to Settings > List name and defaults, as shown below.
That’s the article on How to Add Subscribers to Mailchimp API from Mangcoding. Hopefully, this article is useful and gives you new knowledge. If you have constructive feedback or suggestions, please leave a comment or send them via Mangcoding’s Email and social media.