Integrating MailChimp with WixCode using wix-fetch

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)

export function sendToMailChimp(data) {	
	return fetch("https://us4.api.mailchimp.com/3.0/lists/" + LIST_ID + "/members", {
		method: 'post',
		headers: {
			'Authentication': 'webForm:' + API_KEY,
			'content-type': 'application/json'
		},
		body: data
	}).then((httpResponse) => {console.log("Status: " + httpResponse.statusText);});
}

The console does not show the "Status: " message.
If I run the following, “Handle response” is printed but still not the "Status: " message:

export function sendToMailChimp(data) {	
	return fetch("https://us4.api.mailchimp.com/3.0/lists/" + LIST_ID + "/members", {
		method: 'post',
		headers: {
			'anystring': '' + API_KEY,
			'content-type': 'application/json'
		},
		body: data
	}).then("Handle response").then((httpResponse) => {console.log("Status: " + httpResponse.statusText);});
}

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.

1 Like

Toby,

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

goodluck,
Shlomi

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.

Hi Toby, Im trying to embed a Mail Chimp sign up into my blog posts to further build my list.

Im also running into a similar issue but have Zero knowledge with code and Java Script…

the embeded sign up form works if you are singing up from a mobile device but will not work from a desktop PC browser. Do you have any sugestions ?

Best,

Laurence

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 Toby,
I am refering to MailChimp embeded form options.

I have literally Zero knwoledge with code so i am really in the dark here, any help would be great.

Best

Laurence

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.

Can you explain everything that you did to get it to work? I can’t get it to work.

Hi Patrick, I explained the process and pasted the necessary code above.

Please first follow the instructions in this link to create the Wix Code form https://www.wix.com/code/home/tutorials-custom-form.

Once you have created the form, you can use the code above to integrate witht the MailChimp API.

If you are still having trouble then please post where you are getting stuck and we can try to help.

I’ve made a form and I can connect them to databases and stuff. I did the code above and replaced it with the info I need but nothing happens.

Could you confirm that when you submit the form it submits to your database?

Could you also please post the code as it is in Wix?

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?

Glad to hear you figured it out.

I have an opt-in check box in my form so if it is selected the user is sent to MailChimp.

Hey Toby, can I know which part of the code that need to be replaced by my own elements? Thanks!

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?

That is correct, this is just for the API integration, email is handled seperatley.

I believe you can set up welcome emails via MailChimp. Alternatively you can use this tutorial to use SendGrid to send an automated email on form submission Velo Tutorial: Sending an Email on Form Submission | Help Center | Wix.com

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.

Good luck!