In order to add a contact to MailChimp via the MailChimp API, you will need the following info, to begin with:
- An active MailChimp account.
- A valid MailChimp API key.
- The ListId to which you wish to add contacts to.
- Your Data Center code.
Let’s see how we can add contacts to MailChimp via the MailChimp API.
To begin with, assuming you already have an active MailChimp account, your first step should be to create a MailChimp API. You can do that by going to https://us2.admin.mailchimp.com/account/api/.
You will see a screen as below:
Create your MailChimp API key here and paste it somewhere for the time being.
The next thing that we will need to add a new contact to MailChimp using the API is the ListId/Audience Id. In order for you to use a list Id/Audience Id, you will have to have a List first (or your default list). In order to fetch your Audience Id, on the MailChimp dashboard, go to All Contacts->Setting->Audience Name and defaults. Here, you will find your Audience Id.
For more info on how to get the Audience Id of a MailChimp list, refer to this article here.
Lastly, you will need your Data Center code for your account. It can be different for you than what I have for mine. Pull it from the address bar of your browser. Refer to the section marked in red in the image below. That is your Data Center code which you will need to make an API call to the MailChimp API.
Adding a New Contact To MailChimp via the API
Now that we have all the required information, we are ready to go ahead and see how to add a new contact via the API to MailChimp. To start with, let’s fire up Postman.
If you don’t know what Postman is, take a quick break and get some basic info about the same before proceeding, as for this example, most probably, I will not write any code to show as an example.
Testing connection to the API
Since my Data Center is us2, the testing API endpoint in my case, will be https://us2.api.mailchimp.com/3.0/. In order to make a successful call to the API, you will need to use Basic Auth on Postman.
On successful ping, you will get a JSON response as shown above.
Adding a Record to MailChimp List via API
Now that we know our basic API connection is working, let’s go and add a new record to our MailChimp list.
The API endpoint to do that is https://<dc>.api.mailchimp.com/3.0/lists/list_id/members/. We need to set the authentication the same way it is shown in the image above, in Postman.
Additionally, in this case, we also need to provide the data to be added to the list in json format, under the body section of Postman. Please refer to the image below:
The merge_fields are optional however, the email_address and status fields are compulsory.
Please note that in the endpoint URI, you also need to provide the ListId/AudienceId (masked here in the image above). Also note that while the previous request was of GET type, this request to add data via the MailChimp API is of POST type.
If everything is done correctly, this will work and you will be returned an id of the newly inserted data in your MailChimp list.