Mailchimp API "Marketing permission ID '' does not exist." while passing valid id

Viewed 1683

I use the Mailchimp API to first get the marketing_permission_id for my list. The response looks like this

[marketing_permission_id] => f878932739

This value is then used in a second api call to update the settings for a specific user. However, I get the error:

string(227) "{"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title":"Bad Request","status":400,"detail":"Marketing permission ID '' does not exist.", [...]

I double checked the data that is being send in the second call and it has the correct marketing_permission_id in there:

{"marketing_permissions":{"marketing_permission_id":"f878932739","enabled":true}} 

I followed the format from https://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#%20.

I don't understand what's going wrong. Hopefully someone here does.. :)

1 Answers

Just leaving this for future reference - there're not many hints on google for mailchimp API with marketing permissions... This is the basic function i use for adding a contact to a list.

function syncMailchimp($userdata,
    $apiKey, $listId)
{
    
    $dataCenter = substr($apiKey,
        strpos($apiKey, '-') +
        1);
    $url = 'https://'.$dataCenter.
    '.api.mailchimp.com/3.0/lists/'
    .$listId.
    '/members/';

// This is the interesting part //

    $json = json_encode([
      'email_address' => $userdata[
            'email'],
      'status' => $userdata['status'], // "subscribed","unsubscribed","cleaned","pending"
      'merge_fields' => [
        'FNAME' => $userdata[
                'firstname'
                ],
        'LNAME' => $userdata['lastname']
      ],
       'tags' => [
        'added'
       ],
// How to build the Array -->
       'marketing_permissions' =>
       array(
            0 =>
            array(
                'marketing_permission_id' =>
                '1e5142bbce',
                'enabled' =>
                true,
            ),
         ),
    ]);

    $ch = curl_init($url);

    curl_setopt($ch,
        CURLOPT_HTTPAUTH,
        CURLAUTH_BASIC);

    $headers = array(
        'Content-Type:application/json',
        'Authorization: apikey '
        .$apiKey
    );
    curl_setopt($ch,
        CURLOPT_HTTPHEADER,
        $headers);
    curl_setopt($ch,
        CURLOPT_RETURNTRANSFER,
        true);
    curl_setopt($ch,
        CURLOPT_TIMEOUT, 10);
    curl_setopt($ch,
        CURLOPT_CUSTOMREQUEST,
        'POST');
    curl_setopt($ch,
        CURLOPT_SSL_VERIFYPEER,
        false);
    curl_setopt($ch,
        CURLOPT_POSTFIELDS,
        $json);

    $result = curl_exec($ch);
    $httpCode = curl_getinfo($ch,
        CURLINFO_HTTP_CODE);
    curl_close($ch);

    return $result;
}

Related