Mailchimp API Add Subscriber – Code Examples

Previously I did a lot of work with Mailchimp API in PHP while creating my Mailchimp and WordPress sync plugin, so why not share some working and ready code example with you guys.

In this tutorial examples I am going to use PHP and cURL in order to connect to Mailchimp API. I have a tutorial with WordPress examples as well.

The long story short, here is the ready to use code.

// let's start with some variables
$api_key = 'YOUR MAILCHIMP API KEY HERE';
$email = 'no-reply@rudrastyh.com'; // 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 );

Let’s break down this whole code example into small parts and describe every of them.

$result = json_decode( $result );

if( 400 === $result->status ){
	foreach( $result->errors as $error ) {
		echo '<p>Error: ' . $error->message . '</p>';
	}
} elseif( 'subscribed' === $result->status ){
	echo "<p>Thank you, {$result->full_name}!</p>";
}

Get Mailchimp API key

The very first lines of our code require us to specify Mailchimp API key. I am more than sure that you know how to get it, but just in case I will remind you.

Sign-in into your Mailchimp dashboard and click on your photo in the bottom left part of the screen. Then – Account & Billing.

Mailchimp dashboard

Then, in Account & billing page please go to Extras – API keys.

how to get Mailchimp api key

Everything else should be intuitively simple, I am sure you will figure it out.

Find out your Audience ID (former List ID)

Login to Mailchimp, go to Audiences (Lists) then click on the list title, then in list menu Settings > List name and defaults.

how to get Mailchimp audience ID
Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on X