Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating new subscriber validation error using MailChimp API v3.0

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?

like image 938
jacbar Avatar asked Oct 06 '15 10:10

jacbar


3 Answers

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/

like image 156
Matt Doran Avatar answered Oct 23 '22 05:10

Matt Doran


Additionally (to missing required fields) I ran into this issue by trying to add emails with upper case letters. So this might also occur, if the data you're sending has a wrong format.

like image 44
Tobias Lorenz Avatar answered Oct 23 '22 04:10

Tobias Lorenz


I think the json is ok. Though it says merge fields, the test email address is not appropriate.

I can make a 400 error code in two ways:

  • attempt to add an email address that is already on the list
  • attempt to add a fake address ([email protected]) that does not validate or something...

If you want a to make a fake address use @freddiesjokes.com
ex// [email protected]

I tried the @example.com too, Hope this helps future people (Answered almost 2 years after asked)

like image 1
Who's there... Me Avatar answered Oct 23 '22 04:10

Who's there... Me