Start a free Courses trial
to watch this video
We’ll look at how to add a single member to a list using the lists/subscribe method. We will cover the different parameters for submitting a new member as well as passing in generic merge field information.
For questions, reach out the the MailChimp Support Team: APIhelp@mailchimp.com
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upNow, you know how to get your list with a MailChimp API. 0:00 You even took it a step further using a get request to pull information about 0:02 a specific list. 0:06 Now, you'll get even more advanced by walking through the process of 0:08 adding a new member to a list. 0:11 If you're using the MailChimp web app and wanna view a list of your subscribers, 0:14 you'll need be to be looking at a specific list within your account. 0:17 The same general idea applies to the API. 0:20 In order to interact with a specific list, 0:22 you'll need to specify which list you're using with the list ID. 0:25 Again, the URL is broken down into a few parts, yourdatacenter, 0:29 which is the last part of your API key, .api.mailchimp.com., 0:34 that's the MailChimp API, /3.0, which is the API version that we're using, 0:39 /lists, that's our list resource, /list_id, that's our specific list. 0:47 To get a list of people who have marked your campaigns as spam, 0:53 create a new save segment, or add new subscribers to a list, 0:57 you should run your API calls through this list resource. 1:00 Since you want to a new member to a list, 1:04 you'll need to make an API call to the lists/list_id/membersendpoint. 1:06 With the members endpoint, 1:12 you'll have a noticeable difference between those interactions. 1:14 By using a get request, 1:16 the members endpoint returns a collection of your list subscribers and their data. 1:18 It's kinda like pulling up your MailChimp list and viewing your subscribers. 1:23 You request data from MailChimp, and then, MailChimp arranges and 1:27 displays that information back to you in a digestible form. 1:29 With a get request, you're asking for that same information about a list's members. 1:33 However, if you wanna subscribe a new member to your list, 1:36 you'll need to send information to MailChimp. 1:39 And in order to do that, you'll need to make a post request. 1:41 To provide some context, 1:45 let's think about the way the MailChimp web app handles adding a new subscriber. 1:47 Typically, when a new user is added to MailChimp, 1:51 they fill out a form which sends their information to a MailChimp list. 1:54 If you look at the code for the form, 1:58 you can see there's a specific post method included in the standard HTML form. 1:59 With the MailChimp API, you're doing something very similar, but 2:04 without using an HTML form. 2:07 Since the process is essentially the same, 2:10 you're gonna make sure to include some general information. 2:12 Email address. 2:16 It seems obvious, but definitely necessary. 2:17 Status, which can be pending, subscribed, unsubscribed, or clean. 2:19 We're mark them pending by default when a new post request is made. 2:25 Merge fields, which are subscriber specific pieces of data, like first or 2:29 last names. 2:33 Email type, which can be HTML or plain text. 2:34 All email addresses default to HTML. 2:38 Normally, when a new member signs up to your list, they fill out a form and 2:41 then receive a confirmation email. 2:44 After clicking on that to confirm, the member is added to your list and 2:47 sent a welcome email. 2:50 One advantage of using MailChimp's API is being able to control 2:53 which part of the opt-in process the newly added member goes through. 2:57 With the API, you can skip either or both parts of that default sign-up process. 3:01 However, it's worth noting that adding members to the list 3:06 without notifying them, or 3:09 getting their confirmation, may result in problems with MailChimp's compliance team. 3:11 So, for the purposes of this example, 3:15 you'll be using the normal double opt-in method. 3:17 To add a new subscriber, 3:20 you'll make a curl request to our member's endpoint with a new wrinkle. 3:21 You'll be including some JSON data to send to MailChimp. 3:25 We'll create a JSON object with the key's email address, status, and merge fields. 3:28 This is the data that describes our new subscriber to MailChimp. 3:34 The email address property will be our new subscriber's email address. 3:38 For this example, we'll use examples@mailchimp.com. 3:41 Status will be the member status. 3:49 In this case, we'll use pending, and merge fields. 3:51 Merge fields are a little more complicated. 3:56 This value will be another object where we can list multiple merge tags and 3:58 describe their values. 4:02 For instance, it may look something like this. 4:03 FNAME which stands for first name, will be Nate. 4:06 And LNAME which stands for last name, will be Treehouse. 4:10 In order to tell curl where this data goes, we need to add another argument. 4:15 We did this previously 4:19 with the authorization header that contained our API key. 4:20 For this example, we'll use dash d for data, and then add an RJ sign data. 4:22 This tells curl that we'll be sending information to MailChimp 4:28 making a post request. 4:31 The end results should look something like this, curl-H and 4:33 then authorization header,- lower case d and then our JSON data. 4:38 And then the URL to our member's endpoint. 4:43 When we submit this request, MailChimp should send us back a successful response 4:46 and a copy of the data that we just sent it. 4:50 To make sure your code was successful, check your inbox for the email. 4:56 Once you click on the confirmation link, 5:01 you can log into your MailChimp account and look at your new members profile. 5:03 If the data you sent matches the data the MailChimp has, you're good to go. 5:06 Once you click on the confirmation link in your email, 5:14 you can log into your MailChimp account and look at your new members profile. 5:17 If the data you sent matches the data the MailChimp has, you're good to go. 5:21 With the new version of MailChimp's API, in addition to adding new subscribers, 5:25 you can also add members directly as unsubscribed or cleaned. 5:29 If you're transitioning to MailChimp from another service, you can hold onto your 5:34 old data and not worry about accidentally emailing an invalid address, or 5:37 someone who may have previously opted out. 5:42 Now that you know how the API call works, 5:45 you can expand on its functionality, use your own custom HTML forms, or 5:47 even automatically sync members from another service to MailChimp. 5:52 If you have any questions, reach out to our support team at apihelp@mailchimp.com 5:56
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up