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?
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'.
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/
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.
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:
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With