> To build or manage a Wix site with an AI agent, follow https://www.wix.com/headless/skill.md
> It builds a new Wix site or puts your own site online, from a prompt, and keeps changing it afterwards. The pages are code in any web technology, and Wix runs the backend.
> It works through the Wix plugin (the Wix MCP and the Wix skills), the Wix CLI or no Wix login, and puts the site live on Wix hosting or on another host.
> Business features include a store, bookings, events and tickets, a restaurant, a blog, forms, members and many more.
> Read the Wix skills online at https://www.wix.com/skills
> 
> Portal Navigation:
> 
> - Append `.md` to any URL under `https://dev.wix.com/docs/` to get its markdown version.
> - Pages are either content pages (article or reference text) or menu pages (a list of links to child pages).
> - To get a menu page, truncate any URL to a parent path and append `.md` (e.g. `https://dev.wix.com/docs/sdk.md`, `https://dev.wix.com/docs/sdk/core-modules.md`).
> - If a reference page seems to be missing its schema (parameters, return types, or type definitions), append `?schema=true` to its `.md` URL (e.g. `https://dev.wix.com/docs/contexts/contexts/stores/category-info/use-category-info-context.md?schema=true`).
> - Top-level index of all portals: https://dev.wix.com/docs/llms.txt
> - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt

## Resource: webMethod()

## Article: webMethod()

## Article Link: https://dev.wix.com/docs/sdk/core-modules/web-methods/web-method.md

## Article Content:

# webMethod()

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

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

The `permissions` parameter is used to define which site visitors have permission to call the method produced by `webMethod()`. Import the `Permissions` enum from `@wix/web-methods` to define the permissions.

<blockquote class="caution">
<strong>Caution:</strong> Be careful when selecting a permission, as exposing a method with the wrong permissions can create security risks.
</blockquote>

The `options` parameter contains the `cache` object which allows you to temporarily store the return values from web methods on Wix's infrastructure. When there are significant updates made to your site's data, call [Invalidate Cache](https://dev.wix.com/docs/api-reference/site/viewer/cache/invalidate-cache.md) to clear caches, ensuring your site remains up to date for your visitors.

<blockquote class="important">
<strong>Important:</strong> The web method caching feature is currently only supported for extending sites. 
</blockquote>


Web methods must be defined in a file:

- With a `.web.js` extension when extending [sites](https://dev.wix.com/docs/develop-websites/articles/getting-started/about-developing-websites.md), or apps in [Wix Blocks](https://dev.wix.com/docs/build-apps/develop-your-app/develop-an-app-with-blocks/about-wix-blocks.md).
- With a `.web.ts` extension when developing apps using the [Wix CLI](https://dev.wix.com/docs/build-apps/develop-your-app/frameworks/wix-cli/supported-extensions/backend-extensions/web-methods/add-web-method-extensions-with-the-cli.md).


## Method Declaration

```ts
function webMethod(permissions: Permissions, webFunction: Function, options: Options): Function;
```

## Parameters

| Name                 | Type            | Description                                     |
| :------------------- | :-------------- | :-------------------------------------------------------------------- |
| `permissions`        | `Permissions`   | Permissions needed to call the method in frontend code.<br><br> Permission options:<br>- `Permissions.Anyone`: Any site visitor can call the method.<br>- `Permissions.Admin`: Only site admins can call the method.<br>- `Permissions.SiteMember`: Only site members can call the method.                                                                               |
| `webFunction`        | `Function`      | Method to wrap with `webMethod()`. This method must return [serializable](https://developer.mozilla.org/en-US/docs/Glossary/Serialization) results. This method can be asynchronous. <br><br> **Note:** You may need to [elevate the permissions](https://dev.wix.com/docs/sdk/articles/work-with-the-sdk/about-elevated-permissions.md) of API calls made in this method.       |
| `options`            | `Options`       | Web method options.         |
| `options.cache`      | `Cache`         | Criteria for caching the return value of the `webFunction` being passed.                                                                                 |
| `options.cache.tags` | `Array<string>` | **Required** A list of tags used for categorizing and identifying cached return values. These tags are essential for managing and invalidating caches when necessary.                                                                              |
| `options.cache.ttl`  | `Number`        | Specifies the Time to Live (TTL) for cached return values, measured in seconds. This value indicates how long data remains in the cache before it is considered stale. <br><br> Default: `604800` seconds (1 week).                                                              |

## Returns

`Function`

The method specified in the `webMethod()` call, wrapped with the `webMethod()` method. This wrapped method always returns a promise that resolves to the response from the original method you provided.

### Site example

#### Backend code

```javascript
import { webMethod, Permissions } from '@wix/web-methods';

export const multiply = webMethod(
  Permissions.Anyone,
  (a, b) => a * b,
);
```

#### Backend code with caching

```javascript
import { webMethod, Permissions } from '@wix/web-methods';

export const multiply = webMethod(
  Permissions.Anyone,
  (a, b) => a * b,
  {
    cache: {
      tags: ["multiply", "math"],
    },
  }
);
```

#### Frontend code

Import the web method, then call it. For example:

```javascript
import { multiply } from 'backend/my-web-method-file.web';

const result = await multiply(6, 7);
```

### Apps example

#### Backend code

```ts
import { webMethod, Permissions } from '@wix/web-methods';

export const multiply = webMethod(
  Permissions.Anyone,
  (a: number, b: number) => a * b,
);
```

#### Frontend code

Import the web method, then call it. For example:

```ts
import { multiply } from '<relative-path>/my-web-method-file.web';

const result = await multiply(6, 7);
```