Daniel Goetsch
Daniel Goetsch

Reputation: 141

How to create list member with ADDRESS using Mailchimp API v3

The ADDRESS is a Merge Field but the documentation (http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/) is not clear on how to pass it to the API, since it has sub fields for street, zip, city etc. Does anybody have an example how to do this?

Upvotes: 14

Views: 7421

Answers (6)

Sol Fried
Sol Fried

Reputation: 11

You can create your own Address Class and assign it as follows

  public class Address
    {
        [JsonProperty("addr1")]
        public string Address1 { get; set; }

        [JsonProperty("addr2")]
        public string Address2 { get; set; }

        [JsonProperty("city")]
        public string City { get; set; }

        [JsonProperty("state")]
        public string State { get; set; }

        [JsonProperty("zip")]
        public string Zip { get; set; }

        [JsonProperty("country")]
        public string Country { get; set; }

    }

     var address = new Address()
                                {
                                    Address1 = "1 Main Street,
                                    Address2 = "",
                                    City = "Brooklyn",
                                    State = "NY",
                                    Zip = "11299",
                                    Country = "US"
                                };

member.MergeFields["ADDRESS"] = address;

Upvotes: 1

Kammex
Kammex

Reputation: 31

It works for me (php)

$address = new stdClass();
$address->addr1 = $customer->address;
$address->city = $customer->city;
$address->state = $customer->state;
$address->zip = $customer->zip;
$address->country = $customer->country;

$options = [
    'status' => 'subscribed',
    'email_address' => $customer->email,
    'merge_fields' => [
        'FNAME'=> $customer->first_name,
        'LNAME'=> $customer->last_name,
        'PHONE' => $customer->phone,
        'ADDRESS' => $address,
    ]
];
...
curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($options));
...

Upvotes: 3

MatSnow
MatSnow

Reputation: 7537

Just in case someone runs into the same problem.

I created my own address-class and tried to set the ADDRESS-MergeField to a JSON-string as follows:

member.MergeFields["ADDRESS"] = Newtonsoft.Json.JsonConvert.SerializeObject(MyAddressObject);

This does not set the address, but also returns no error.


One need to assign a Dictionary<string, object> instead:

var adr = new Dictionary<string, object>
{
    { "addr1", "123 Whatever" },
    { "addr2", "" },
    { "city", "New York" },
    { "state", "NY" },
    { "zip", "12345" },
    { "country", "US" }
};

member.MergeFields["ADDRESS"] = adr;

Upvotes: 4

Brad123
Brad123

Reputation: 944

Python answer

I was getting error's because address line 1 was empty. Here's a copy and paste code that works:

added_user= {}
added_user.update({'email_address': "[email protected]"})
#status options: "subscribed", "unsubscribed", "cleaned", "pending", or "transactional"
added_user.update({'status': 'subscribed'})
address_dict = {
            "addr1": "line 1",
            "addr2": "made up address",
            "city": "Kirkland",
            "state": "WA",
            "zip": "12345",
            "country": "US",
            "language": "en"
        }

added_user.update({'merge_fields':{ 'FNAME': "Corona",'LNAME': "Virus", 'ADDRESS':address_dict}})
client.lists.members.create(list_id=list_id,data=added_user)

Upvotes: 1

Adiyya Tadikamalla
Adiyya Tadikamalla

Reputation: 977

Merge fields can pass like this:

'FNAME' => $contact->first_name,
'LNAME' => $contact->last_name,
'ADDRESS' => (Object)[
        'addr1' => $contact->fulladdress,
        //'addr2' => $contact->address2,
        'city' => $contact->city,
        'state' => $contact->state_region,
        'zip' => $contact->zip_postal_code,
        'country' => $contact->country->shortcode,
        'language' => $contact->language->code,
 ],

Upvotes: 4

Patrik Beck
Patrik Beck

Reputation: 2505

This JSON works:

{
  "addr1" : "first line",
  "addr2" : "second line",
  "city" : "city",
  "state": "state",
  "zip": "zip code",
  "country": "country"
}

It's documented in schema here: https://us1.api.mailchimp.com/schema/3.0/Lists/Members/MergeField.json

Upvotes: 24

Related Questions