Creating new subscriber validation error using MailChimp API v3.0

jacbar picture jacbar · Oct 6, 2015 · Viewed 10.1k times · Source

I've got problem with creating new member in the list using v3 API. I'm sending post request with json data :

url:

https://us10.api.mailchimp.com/3.0/lists/<list-id>/members

headers:

   Content-type: application/json
   Authorization: apikey <my-api-key>

Json body:

{
  "status": "pending",
  "email_address": "[email protected]",
  "merge_fields": {
    "FNAME": "John",
    "LNAME": "Smith",
    "REFERRER": "referrer",
    "REFERRAL": "referral"
  }
}

It's based on api docs and tutorials https://teamtreehouse.com/library/mailchimp-api/mailchimp-api/adding-new-members-to-your-list . But every response looks like:

{
  "type": "http://kb.mailchimp.com/api/error-docs/400-invalid-resource",
  "title": "Invalid Resource",
  "status": 400,
  "detail": "Your merge fields were invalid.",
  "instance": "",
  "errors": [
    {
      "field": "FNAME",
      "message": "Please enter a value"
    },
    {
      "field": "LNAME",
      "message": "Please enter a value"
    },
    {
      "field": "REFERRAL",
      "message": "Please enter a value"
    },
    {
      "field": "REFERRER",
      "message": "Please enter a value"
    }
  ]
}

What am I doing wrong? Is it a problem with MailChimp API?

Answer

Matt Doran picture Matt Doran · Feb 7, 2016

When adding a user to a list you have to add all fields that are 'required' in you list. To see your fields, log into mail chimp click 'Lists' then into your list. From the 'Settings' drop down select 'List fields and |MERGE| tags'.

enter image description here

Newsletter is an additional custom required field that i added. If i don't send it i will get the error you have because it's a required field. All additional fields need to be add inside the merge_fields array like 'NEWSLETTER'.

$dataCenter ='us10'; //last part of the api key
$auth = base64_encode( 'user:'.$apikey );
$data = array(
   'apikey'        => $apikey,
   'email_address' => $email,
   'status'        => 'subscribed',
   'merge_fields'  => ['NEWSLETTER' => '1']
);
$json_data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/f3e05535e8/members/');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
'Authorization: Basic '.$auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$result = curl_exec($ch);

If i had more custom fields in my form I would add them like so:

'merge_fields'  => [
    'NEWSLETTER' => '1',
    'ANOTHERFIELD' => 'this is example data',
]

Mail chimp documentation says it requires a 'merge_fields' object which when the array is converted to json it becomes a json object.

http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/