Velo Web Modules: Calling Backend Code from the Frontend

Visit the Velo by Wix website to onboard and continue learning.

Introduction to Web Modules

Web modules are exclusive to Velo and enable you to write functions that run server-side in the backend, and easily call them in your client-side code. With web modules you can import functions from the backend into files or scripts in page code or public files, knowing they will run server-side in the backend. Velo handles all the client-server communication required to enable this access. See the advanced tip below if you want to know how this communication is handled.

Advanced: How Web Modules Work - Behind the Scenes

When you import a web module on the client-side you get a proxy function to the web module function. This proxy function uses an XMLHttpRequest to invoke the function in the backend. The runtime listens to those invocations and calls the appropriate function.

The arguments and return value are serialized using JSON.

Hello World Example

The following example shows how you can call a backend function in your client-side code:

Copy Code
import { rotatingGreeting } from 'backend/helloModule';
$w.onReady(function () {
let callCount = 0;
$w('#button').onClick(async () => {
$w('#responseText').text = await rotatingGreeting(callCount++);

And here is the above code in action:

You can experiment with this example for yourself on our Hello Web Modules example page.

When to Use Web Modules

Web module functions contain code you would typically want or need to run in the backend. Your code may have security risks if it runs in the frontend, or you may want to access other web services. For example, let's say you want to enable your site visitor to send an email via a 3rd-party provider. You would store your API key to the 3rd-party service in the Secrets Manager, and write your function that sends the email in the backend, like this:

Copy Code
// Filename: backend/sendEmail.jsw (web modules need to have a *.jsw* extension)
import {getSecret} from 'wix-secrets-backend';
import {fetch} from 'wix-fetch';
// wix-fetch is the API we provide to make https calls in the backend
const API_KEY = await getSecret(<SECRET_NAME>);
export function sendEmail (address, subject, body) {
return fetch("" + API_KEY, {
method: 'post',
body: JSON.stringify({address, subject, body})
}).then(function(response) {
if (response.status >= 200 && response.status < 300)
return response.text();
throw new Error(response.statusText);

You need this code to run in the backend for two reasons:

  1. It includes your sensitive information, such as a reference to your API key to the 3rd-party service. You don't want to expose this to the client.
  2. You are calling a 3rd-party web service, which you can only do in the backend because of cross-origin resource sharing (CORS).

You would then import the function from your web module into your frontend file and use it:

Copy Code
import {sendEmail} from 'backend/sendEmail.jsw';
export function sendButton_onClick(event) {
.then(function() {
console.log("email was sent");

Using Web Module Functions in Backend

You can import a web module function into another module in the backend.

Calling a Function in a Web Module

Unlike regular modules that allow you to export functions, objects, and other items, you can only export functions from web modules. Web modules also always return a promise. This is true even if, in the implementation of the function, it returns a value. For example, if your web module function returns a value, like this:

Copy Code
// Filename: aModule.jsw (web modules need to have a *.jsw* extension)
export function multiply(factor1, factor2) {
return factor1 * factor2;

When you call the function, it still returns a promise that resolves to the value. So you need to use it like this:

Copy Code
import {multiply} from 'backend/aModule';
multiply(4,5).then(function(product) {
// Logs: 20

Debugging Web Modules

You can log messages to the console in web modules and they will be displayed in the client's console log, even though the code is running in the backend.

Web Module Permissions

Because Web Modules allow you to call your backend code from the frontend, it's important that you limit which visitors can access their functionality by setting their permissions. See About Web Module Permissions to learn more.

Was this helpful?