I’m trying to use a database hook to send information to a MailChimp list.
The problem is that my code stops continuing at a certain point (processing the httpResponse)
In addition to this, the data is presumably rejected by MailChimp as it isn’t there when I look for it.
It may be an authentication issue but there’s no documentation online on setting up mailchimp and I cannot seem to find a way of doing basic authentication for my API_KEY other than what I have written, so I am in the dark.
Is anyone able to provide some insight as to what may be going wrong?
Thank you.
one cool option i find very useful in such cases is using this free service https://requestb.in/ which will let you debug the request you send as seen by the third party api you are trying to reach.
then i’d use an http request tool such as postman (chrome extension) to just validate the request you send, in terms of header api key etc are received just fine by the api you are trying to invoke.
once you are sure about the above 2, it is just a matter of configuring your fetch right within wix.
one last thing to thing about - are you implementing the above code in the backend section of wix code? otherwise it might be a classic CORS problem this api provider is not letting you use while your page is not served from the same website, which practically means it can not be invoked from a browser, just server, but i am not familiar with this api provider so you should check
Thanks for you reply Shlomi, the idea about Postman helped refine my parameters, and it is now working as intended.
This solution is tailored to add a subscriber to mail chimp after the data has been entered into the database:
import {fetch} from 'wix-fetch';
const API_KEY_64 = "API Key encoded with base64 in the format 'anystring:key'";
const LIST_ID = "List ID";
export function sendToMailChimp(data) {
fetch("https://<your dc>.api.mailchimp.com/3.0/lists/" + LIST_ID + "/members", {
method: 'post',
headers: {
'Authorization' : 'Basic ' + API_KEY_64,
'Content-Type' : 'application/json'
},
body: data
});
}
Ensure that the data you are entering is Stringify’ed into JSON form too:
import {sendToMailChimp} from 'backend/mailChimp.jsw';
export function ContactForm_afterInsert(item, context) {
// Only if user is subscribing
if(!item.mailChimp)
return item;
// Format data (field names must be those specified by MailChimp)
var data = {
"email_address":item.email,
"merge_fields": {
"FNAME":item.fName,
"LNAME":item.lName
},
"status":"subscribed"
};
// Stringify into JSON format
data = JSON.stringify(data, '\t');
// Process request
sendToMailChimp(data);
return item;
}
Hopefully this assists anyone that runs into a similar issue.
Laurence, it is best to do the integration in the backend code, as it also requires your secret token you do not want to share with the users. In such a case what impact do you have over the browser running the invocation of the code?
When you refer to the “embedded sign up” form are you referring to a Wix Code form you have created with the javascript above, or are you referring to the MailChimp embedded form options?
The Wix code form above works on desktop and mobile across Safari and Chrome in the tests I have done.
Hi Laurence, I don’t think the WixCode forum is the best place to be asking about MailChimp embedded form options, as I don’t think many developers on here would have experience with using MailChimp embed.
I would reccomend contacting MailChimp for support or hiring a web developer to look into your device functionality problem.
I got it to work. I just had a return statement in a wrong spot smh. I am wondering, what was context in ContactForm_afterInsert(item, context) used for?
Hi Andluism, you will need to modify the code to suit your needs depending on whether you are connecting to a subscriber form or a contact form with an opt-in to become a subscriber. The minimum you would need to change is the API key and List ID.
This code is only for collecting the subscribers via MailChimp right. Will they get an autoresponder email after the subscription? Or do you happen to use the code in combination with sending an email on subscription?
Well I have done that tutorial too, but sadly the email is only in text. I’m currently trying to understand how to sent a template / newsletter email on a form submission (not just plain text), but well pardon my dumbness in understanding SendGrid documentation #killmyself
I am not familiar with SendGrid, but you should be able to create an templated automated email within their system. At the very least theyshould allow you to code some HTML and CSS to make it look nice.