Call Backend Code from the Frontend

Web modules allow you to write backend functions that you can easily call from the frontend. The following describes how to create, export, and call web module functions, also known as web methods.

Step 1 | Add a web module

The way you add a web module file depends on which IDE you're using.

To add a web module file to your site in the editor:

  1. Navigate to Public & Backend in code sidebar.

  2. Hover over the Backend heading, click the plus icon , and then select Add web module.
    This action creates a web module file with a .web.js extension.

To add a web module file in the Wix IDE or your local IDE:

  • Create a new file in the backend-modules directory and name it with the .web.js extension.

Step 2 | Define a web method the backend

Add an exported web method in the file you created above. The web method wraps an inner function that contains the logic you want to call from the frontend.

To create a web method, call the webMethod() function and define the web method's permissions and its inner function:

  1. Add the necessary imports:

    Copy
  2. Call the webMethod() function and store its returned value in an exported variable.

    Copy
  3. Pass the webMethod() function a permissions value as the first argument:

    Copy
  4. Pass the webMethod() function an inner function as the second argument:

    Copy

Step 3 | Call the function from the frontend

To call a web method from the frontend:

  1. Import the exported web method from the web module you created above:

    Copy
  2. Call the imported function:

    Copy

    Remember, web module functions are always asynchronous.

Was this helpful?
Yes
No