Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon can you provide a code snippet for updating subscriber information in Mailchimp API


can you provide a code snippet for updating subscriber information in Mailchimp API


Here is a code snippet in PHP that updates subscriber information in Mailchimp using the API:

php
<?php
require_once 'path/to/mailchimp-api/vendor/autoload.php';

use \DrewM\MailChimp\MailChimp;

// Initialize the Mailchimp API client
$mailchimp = new MailChimp(MAILCHIMP_API_KEY);
$mailchimp->setConfig(['server' => 'YOUR_SERVER_PREFIX']);

// Get the list ID
$listId = '**********';

// Get the subscriber ID
$subscriberId = 'md5:
 <script language='JavaScript' type='text/javascript'>
 <!--
 var prefix = 'm&#97;&#105;lt&#111;:';
 var suffix = '';
 var attribs = '';
 var path = 'hr' + 'ef' + '=';
 var addy49064 = 's&#117;bscr&#105;b&#101;r' + '&#64;';
 addy49064 = addy49064 + '&#101;x&#97;mpl&#101;' + '&#46;' + 'c&#111;m';
 document.write( '<a ' + path + '\'' + prefix + addy49064 + suffix + '\'' + attribs + '>' );
 document.write( addy49064 );
 document.write( '<\/a>' );
 //-->
 </script><script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '<span style=\'display: none;\'>' );
 //-->
 </script>This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 <script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '</' );
 document.write( 'span>' );
 //-->
 </script>';

// Update subscriber information
$mailchimp->patch("lists/$listId/members/$subscriberId", [
    'email_address' => '
 <script language='JavaScript' type='text/javascript'>
 <!--
 var prefix = 'm&#97;&#105;lt&#111;:';
 var suffix = '';
 var attribs = '';
 var path = 'hr' + 'ef' + '=';
 var addy43836 = 's&#117;bscr&#105;b&#101;r' + '&#64;';
 addy43836 = addy43836 + '&#101;x&#97;mpl&#101;' + '&#46;' + 'c&#111;m';
 document.write( '<a ' + path + '\'' + prefix + addy43836 + suffix + '\'' + attribs + '>' );
 document.write( addy43836 );
 document.write( '<\/a>' );
 //-->
 </script><script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '<span style=\'display: none;\'>' );
 //-->
 </script>This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 <script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '</' );
 document.write( 'span>' );
 //-->
 </script>',
    'status' => 'subscribed',
    'merge_fields' => [
        'FNAME' => 'First Name',
        'LNAME' => 'Last Name',
    ],
]);

?>

This code snippet updates the subscriber information for the subscriber with the specified ID. It sets the email address, status, and merge fields (first name and last name) for the subscriber.

Parameters:

- `$listId`: The ID of the Mailchimp list.
- `$subscriberId`: The ID of the subscriber to update.
- `$emailAddress`: The new email address for the subscriber.
- `$status`: The new status for the subscriber (e.g., "subscribed", "unsubscribed", etc.).
- `$mergeFields`: An array of merge fields to update for the subscriber.

Example Output:

json
{
    "status": "success",
    "msg": "OK",
    "result": {
        "email_address": "
 <script language='JavaScript' type='text/javascript'>
 <!--
 var prefix = 'm&#97;&#105;lt&#111;:';
 var suffix = '';
 var attribs = '';
 var path = 'hr' + 'ef' + '=';
 var addy97140 = 's&#117;bscr&#105;b&#101;r' + '&#64;';
 addy97140 = addy97140 + '&#101;x&#97;mpl&#101;' + '&#46;' + 'c&#111;m';
 document.write( '<a ' + path + '\'' + prefix + addy97140 + suffix + '\'' + attribs + '>' );
 document.write( addy97140 );
 document.write( '<\/a>' );
 //-->
 </script><script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '<span style=\'display: none;\'>' );
 //-->
 </script>This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 <script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '</' );
 document.write( 'span>' );
 //-->
 </script>",
        "status": "subscribed",
        "merge_fields": {
            "FNAME": "First Name",
            "LNAME": "Last Name"
        }
    }
}

This code snippet demonstrates how to update subscriber information in Mailchimp using the API.

what is the syntax for archiving a list member in Mailchimp via the API

The syntax for archiving a list member in Mailchimp via the API is:

bash
DELETE /lists/{list_id}/members/{subscriber_hash}

Replace `{list_id}` with the ID of the list you want to archive the member from and `{subscriber_hash}` with the MD5 hash of the lowercase version of the list member's email address.

For example:

bash
DELETE /lists/12345678/members/md5:
 <script language='JavaScript' type='text/javascript'>
 <!--
 var prefix = 'm&#97;&#105;lt&#111;:';
 var suffix = '';
 var attribs = '';
 var path = 'hr' + 'ef' + '=';
 var addy73482 = 's&#117;bscr&#105;b&#101;r' + '&#64;';
 addy73482 = addy73482 + '&#101;x&#97;mpl&#101;' + '&#46;' + 'c&#111;m';
 document.write( '<a ' + path + '\'' + prefix + addy73482 + suffix + '\'' + attribs + '>' );
 document.write( addy73482 );
 document.write( '<\/a>' );
 //-->
 </script><script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '<span style=\'display: none;\'>' );
 //-->
 </script>This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 <script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '</' );
 document.write( 'span>' );
 //-->
 </script>

This will archive the list member with the email address `
This e-mail address is being protected from spambots. You need JavaScript enabled to view it
` from the list with the ID `12345678`.

Citations:
[1] https://stackoverflow.com/questions/63584277/how-can-i-unarchive-a-member-using-the-mailchimp-api
[2] https://mailchimp.com/developer/marketing/api/list-members/
[3] https://mailchimp.com/developer/marketing/api/lists/add-list/
[4] https://mailchimp.com/developer/marketing/api/lists/get-list-info/
[5] https://github.com/Gemini-Commerce/mailchimp-marketing-go/blob/master/docs/ListsApi.md

what are the common fields to update for a subscriber in Mailchimp API

The common fields to update for a subscriber in Mailchimp API include:

1. Email Address: Update the subscriber's email address using the `email_address` field.
2. Status: Update the subscriber's status using the `status` field. This can be set to "subscribed", "unsubscribed", "cleaned", or "bounced".
3. Merge Fields: Update the subscriber's merge fields using the `merge_fields` field. This includes fields like `FNAME`, `LNAME`, `PHONE`, `ADDRESS`, etc.
4. Interests: Update the subscriber's interests using the `interests` field. This includes the categories and names of the interests.
5. Location: Update the subscriber's location using the `location` field. This includes the country, region, and city.
6. Profile: Update the subscriber's profile using the `profile` field. This includes fields like `FIRST_NAME`, `LAST_NAME`, `PHONE`, `ADDRESS`, etc.
7. Tags: Update the subscriber's tags using the `tags` field. This includes custom tags that you can create for your subscribers.

Here is an example of how to update a subscriber's information using the Mailchimp API:

bash
PATCH /lists/{list_id}/members/{subscriber_hash}

json
{
    "email_address": "
 <script language='JavaScript' type='text/javascript'>
 <!--
 var prefix = 'm&#97;&#105;lt&#111;:';
 var suffix = '';
 var attribs = '';
 var path = 'hr' + 'ef' + '=';
 var addy32022 = 'n&#101;w_&#101;m&#97;&#105;l' + '&#64;';
 addy32022 = addy32022 + '&#101;x&#97;mpl&#101;' + '&#46;' + 'c&#111;m';
 document.write( '<a ' + path + '\'' + prefix + addy32022 + suffix + '\'' + attribs + '>' );
 document.write( addy32022 );
 document.write( '<\/a>' );
 //-->
 </script><script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '<span style=\'display: none;\'>' );
 //-->
 </script>This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 <script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '</' );
 document.write( 'span>' );
 //-->
 </script>",
    "status": "subscribed",
    "merge_fields": {
        "FNAME": "New First Name",
        "LNAME": "New Last Name"
    },
    "interests": [
        {
            "category_id": "12345678",
            "name": "New Interest"
        }
    ]
}

This example updates the subscriber's email address, status, merge fields, and interests.

Citations:
[1] https://mailchimp.com/help/manage-audience-signup-form-fields/
[2] https://mailchimp.com/developer/marketing/api/list-members/
[3] https://mailchimp.com/developer/marketing/api/lists/batch-subscribe-or-unsubscribe/
[4] https://mailchimp.com/developer/marketing/api/lists/add-list/
[5] https://mailchimp.com/developer/marketing/api/lists/get-list-info/