Briefly explain what’s not working or where you’re stuck
I’ve built a quiz as an HTML iFrame embed in Wix Studio. When a visitor opts in to a mailing list on the results screen, I need their email and a group ID sent to an external email API. Every approach I’ve tried is blocked — either by CORS because the iFrame runs on Wix’s filesusr domain, or by postMessage not reaching the Wix page code on the live site.
Working in
Wix Studio Editor, Dev Mode, Velo backend, Wix Automations
Site link
What I’m trying to do
When a visitor opts in via the quiz, I want their email address and a result group ID to be passed to a Velo backend function, which then calls an external email API to add them to the correct mailing list group.
What I’ve tried so far
- Direct fetch from iFrame to external API — blocked by CORS (origin is Wix’s filesusr domain)
- Direct fetch from iFrame to my Wix HTTP function — also blocked by CORS for the same reason
- postMessage using window.parent and window.top — message never reaches Wix page code on live site; confirmed by Wix Logs showing no entries when opt-in fires
- wix-fetch in page code — runs client-side, same CORS problem
- Importing a backend web.js function into page code and calling it from an onMessage handler — TypeError: (0, e.addSubscriberToGroup) is not a function from comlink.js, tried all import path variations
- Installed the wix automations and wix essentials packages, built a Wix Automation with a Velo code trigger and Run Velo code action — but still cannot get any signal from inside the iFrame to fire it
Extra context
Wix support confirmed the iFrame sandboxing is intentional and cannot be disabled, and suggested moving API calls to a backend — which is exactly what I am trying to do. They also mentioned Wix Blocks as a possible alternative. The Velo backend setup appears correct and the automation is active — the problem is purely that no data can get out of the sandboxed iFrame and into the Wix backend. Is Wix Blocks a realistic solution here, and if so how would I approach it?
The pattern that works on the live site is: iFrame → page code via postMessage, page code → backend web method, backend → external API. The backend call has no CORS problem because it runs server-side.
In the HTML embed (it has to be the “Embed HTML” element with your code in it, not an embedded URL):
window.parent.postMessage({ type: "optin", email, groupId }, "*");
In the page code of the page that contains the element:
import { addToList } from "backend/quiz.web";
$w.onReady(() => {
$w("#html1").onMessage(async (event) => {
if (event.data?.type === "optin") {
await addToList(event.data.email, event.data.groupId);
}
});
});
In backend/quiz.web.js:
import { Permissions, webMethod } from "wix-web-module";
import { fetch } from "wix-fetch";
export const addToList = webMethod(Permissions.Anyone, async (email, groupId) => {
// call the email API here, keep the API key in the Secrets Manager
});
Things that make postMessage look like it never arrives: using window.top instead of window.parent, registering onMessage in the masterPage code instead of the page that holds the element, the element ID in the code not matching, and testing without publishing. It also helps to validate the email in the backend, since the web method is callable by anyone.
Goran
Hi Barney, the iframe can’t reach Velo or the external API directly, so let the page do the sending. Inside the iframe, on opt-in, call window.parent.postMessage({ email, groupId }, ‘*’). On the page code (not the iframe), listen with the HTML component’s own handler: $w(‘#yourHtmlId’).onMessage((event) => { const { email, groupId } = event.data; … }). A plain window message listener in page code usually won’t receive it on the live site, which is likely why your logs were empty. Then call your backend file from that handler. In backend/subscribers.web.js the function needs the export keyword, export async function addSubscriberToGroup(email, groupId), and that’s probably what the “is not a function” TypeError is pointing at. Put the external API fetch() in that backend function, since server-side calls aren’t subject to CORS. You don’t need Wix Blocks for this. I haven’t run it on your site, so check the backend logs on the first real opt-in. If this helped, please mark it as Solved.