Creating new subscriber validation error using MailChimp API v3.0
Asked Answered
Z

4

7

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?

Zennas answered 6/10, 2015 at 10:14 Comment(2)
I tried your JSON in MailChimp playground and at least it seems to work in there us5.api.mailchimp.com/playgroundBessie
How are you sending this data to that endpoint? with Curl or what? Because that errora messages is pretty much telling, that it is not getting your merge_fields values, only keys.Bessie
V
6

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/

Vitiligo answered 7/2, 2016 at 10:33 Comment(0)
I
1

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.

Invariant answered 6/2, 2017 at 16:48 Comment(0)
C
1

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)

Confiscatory answered 18/8, 2017 at 21:37 Comment(0)
O
0

You need to fill your REQUIRED merge tags or remove them,

Note: There is a (possible) a delay after removing REQUIRED merge fields.

I didn't stick around to see how long, i can CONFIRM after replicating the list that having required merge tags are required.

Obi answered 18/8, 2016 at 1:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.