Search code examples
mailchimpmembers

mailchimp 3.0 authentication fails


Authentication fails for the following, which gets posted to mailchimp using CURL, where API_KEY is defined as a string containing my key. Similar code used to work just fine for mailchimp v2:

$params = array ('apikey' => API_KEY,
                 'email_address' => $email,
                 'status' => 'pending',
                 'merge_fields' => array ('fname' => $first_name,
                                          'lname' => $last_name
                                         )
                );

The error is:

{"type":"http://kb.mailchimp.com/api/error-docs/401-api-key-missing",
 "title":"API Key Missing",
 "status":401,"detail":
 "Your request did not include an API key.",
 "instance":"(long number)"
}

Solution

  • That's not how authentication works in v3.0. From the documentation:

    The easiest way to authenticate is using HTTP Basic Auth. Enter any string as the username and supply your API Key as the password. Your HTTP client library should have built-in support for basic authorization.

    If you're using PHP, every HTTP library knows how to do this. I'd recommend Guzzle or PHP Requests, but even basic cURL in PHP can do basic auth easily.

    The interests syntax looks okay, except that the value should be a boolean instead of a string.