webMethod( )


Defines a backend function that can be called from the frontend.

The webMethod() function is a wrapper used to export functions from backend code that can be called from the frontend.

The permissions parameter is used to define the permissions needed to call the function in frontend code. Import the Permissions enum from the wix-web-module module to define the permissions. The permission options are:

  • Permissions.Anyone: Any site visitor can call the function.
  • Permissions.Admin: Only site admins can call the function.
  • Permissions.SiteMember: Only site members can call the function.

The cache object, found in the options parameter, allows you to cache or temporarily store the return value of web methods. Use the ttl property to define the duration, in seconds, for which you want the return value to be cached. If the Time To Live (TTL) is not set, the default value is 604800 seconds, which is approximately 1 week.

Assign identifiers to caches using the tags property of the cache object. Tags enable you to specify cached return values that may require invalidation due to significant changes in your data. To invalidate web method caches, use the invalidateCache() function from wix-cache-backend. Once invalidated, the return value is re-cached the next time your backend function is called. Learn more about web method caching.

Important:

  • The tags property is required for caching. Without tags, nothing is cached.
  • The invalidateCache() method from wix-cache-backend is currently in developer preview.

Web methods must be defined in files with a .web.js extension.

Method Declaration
Copy
Method Parameters
permissionsPermissionsRequired

Permissions needed to call the function in frontend code.


_functionFunctionRequired

Function to export.


optionsOptions

Caching options.

Returns
Return Type:Promise<Function>
Errors

This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.

Did this help?