> ## Documentation Index
> Fetch the complete documentation index at: https://kb.hosting.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Using the Mailchimp API with PHP

> Learn to use the Mailchimp API with PHP through step-by-step instructions and helpful links.

This article discusses how to use the Mailchimp API (application programming interface) with PHP.

<Warning>
  **Important**

  This article assumes that you have already signed up for and configured a Mailchimp account. For information about how to do this, please see [this article](/docs/signing-up-for-a-mailchimp-account).
</Warning>

## About the Mailchimp API

The Mailchimp API (application programming interface) provides a way to work with campaigns and lists programmatically. This article demonstrates various ways to use the API with PHP.

## Using the official API wrapper

To access your Mailchimp account using PHP, you use code that provides a "wrapper" to the API. There are several unofficial API wrappers for Mailchimp that use a simplified interface. The procedure below, however, demonstrates how to use the official Mailchimp API wrapper.

To do this, follow these steps:

1. Log in to your account [using SSH](/docs/using-ssh-secure-shell).

2. At the command prompt, type the following commands:

   ```bash theme={null}
   cd ~
   git clone https://bitbucket.org/mailchimp/mailchimp-api-php.git
   ```

   After the *git clone* command completes, there is a new *mailchimp-api-php* directory.

3. Type the following command:

   ```bash theme={null}
   cp -R mailchimp-api-php/src mailchimp
   ```

   > 📘 Note
   >
   > This command copies the API source code to the separate *mailchimp* directory (you can actually use any directory name you want). Doing so enables you to pull future updates in the *mailchimp-api-php* directory for further testing, without overwriting source code referenced directly by your applications.

4. To use the Mailchimp API in your PHP code, you must include the *Mailchimp.php* file. Then you can create a **Mailchimp** object and work with the API. For example, the following code sample demonstrates how to add a new subscriber to an existing list:

   ```php theme={null}
   <?php

   require('mailchimp/Mailchimp.php');    // You may have to modify the path based on your own configuration.

   $api_key = "Add your Mailchimp API key here";
   $list_id = "Add your list ID here";

   $Mailchimp = new Mailchimp( $api_key );
   $Mailchimp_Lists = new Mailchimp_Lists( $Mailchimp );

   try 
   {
       $subscriber = $Mailchimp_Lists->subscribe(
           $list_id,
           array('email' => 'kellykoe@example.com'),      // Specify the e-mail address you want to add to the list.
           array('FNAME' => 'Kelly', 'LNAME' = 'Koe'),   // Set the first name and last name for the new subscriber.
           'text',    // Specify the e-mail message type: 'html' or 'text'
           FALSE,     // Set double opt-in: If this is set to TRUE, the user receives a message to confirm they want to be added to the list.
           TRUE       // Set update_existing: If this is set to TRUE, existing subscribers are updated in the list. If this is set to FALSE, trying to add an existing subscriber causes an error.
       );
   } 
   catch (Exception $e) 
   {
       echo "Caught exception: " . $e;
   }

   if (! empty($subscriber['leid']) )
   {
       echo "Subscriber added successfully.";
   }
   else
   {
       echo "Subscriber add attempt failed.";
   }
   ?>
   ```

## More information

For more information about the Mailchimp API, please visit [https://apidocs.mailchimp.com](https://apidocs.mailchimp.com).

## Related articles

[Signing up for a Mailchimp account](/docs/signing-up-for-a-mailchimp-account)
