> 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: Function Reference

## Article: Function Reference

## Article Link: https://dev.wix.com/docs/site-extensions/function-libraries/function-reference.md

## Article Content:

# Function Reference

The `functions` section of a function library manifest defines the functions the library exposes. It's a map where each key is a function name and each value describes that function's parameters, return type, and execution characteristics.

## Example

The following example declares a function that capitalizes text:

```ts
functions: {
  capitalize: {
    parameters: [
      { dataType: "text", displayName: "Text to capitalize" },
    ],
    returns: { dataType: "text" },
    async: false,
  },
}
```

## Function fields

A function has the following fields:

| Field | Required | Description |
| --- | --- | --- |
| `parameters` | No | Array of [parameter definitions](#parameter-fields). Maximum 10. Defaults to no parameters. |
| `returns` | No | [Return type definition](#return-type-fields). Defaults to [void](#void-functions). |
| `async` | No | Whether the function returns a Promise. Defaults to `false`. |
| `displayName` | No | Name of the function shown in the editor. Maximum 100 characters. |
| `description` | No | Function description. Maximum 300 characters. |

## Parameter fields

Each entry in `parameters` describes one argument the function accepts:

| Field | Required | Description |
| --- | --- | --- |
| `dataType` | Exactly one of `dataType` and `domEvent` | The [type of value](https://dev.wix.com/docs/site-extensions/editor-react-components/editor-react-types/overview/about-editor-react-types.md) the parameter accepts. It can't be `schema`, `container`, or a deprecated event handler type, such as `onClick`. For an element event, use `domEvent` instead. |
| `domEvent` | Exactly one of `dataType` and `domEvent` | Category of DOM event, such as a click, that marks the parameter as the event the function handles. See [Event parameters](#event-parameters). |
| `displayName` | No | Name of the parameter shown in the editor. Maximum 100 characters. |
| `description` | No | Parameter description. Maximum 100 characters. |
| `optional` | No | Whether the [parameter is optional](#optional-parameters). Only trailing parameters can be optional because JavaScript arguments are positional. Defaults to `false`. |
| `defaultValue` | No | Default the editor shows as a hint. Not passed at runtime, so your implementation must handle the missing value. Used only when `optional` is `true`. |
| `arrayItems` | When `dataType` is `arrayItems` | Defines what elements in the [array](#arrays) look like. |
| `data` | When `dataType` is `data` | Defines the fields inside the [nested object](#nested-objects). |
| `function` | When `dataType` is `function` | Defines the parameters and return type of a [callback parameter](#callback-parameters). |
| `textEnum` | When `dataType` is `textEnum` | List of allowed [string options](#text-enums). |
| `link` | When `dataType` is `link` | Configuration for the [link parameter](#link-parameters), such as supported link types. |
| `changeEvent` | No | The kind of input that triggers a `CHANGE` event. Used only when `domEvent` is `CHANGE`. See [Change events](#change-events). |

### Optional parameters

Set `optional` to `true` to make a parameter optional. Only trailing parameters can be optional because JavaScript arguments are positional. Use `defaultValue` to give the editor a hint to display for the missing value.

The following example declares `roundNumber` with an optional parameter and a default value:

```ts
functions: {
  roundNumber: {
    parameters: [
      { dataType: "number", displayName: "Value" },
      { dataType: "number", displayName: "Decimal Places", optional: true, defaultValue: 0 },
    ],
    returns: { dataType: "number" },
  },
}
```

### Arrays

For array parameters, set `dataType` to `'arrayItems'`. Then define the array elements under `arrayItems.item`.

```ts
functions: {
  sum: {
    parameters: [
      {
        dataType: "arrayItems",
        displayName: "Numbers to sum",
        arrayItems: {
          item: { dataType: "number" },
        },
      },
    ],
    returns: { dataType: "number" },
  },
}
```

### Nested objects

For complex object parameters, set `dataType` to `'data'`. Then define each field of the object under `data.items`.

```ts
functions: {
  formatUser: {
    parameters: [
      {
        dataType: "data",
        displayName: "User data",
        data: {
          items: {
            firstName: { dataType: "text", displayName: "First Name" },
            lastName: { dataType: "text", displayName: "Last Name" },
          },
        },
      },
    ],
    returns: { dataType: "text" },
  },
}
```

### Text enums

For a fixed set of string options, set `dataType` to `'textEnum'` and list the choices under `textEnum.options`.

```ts
functions: {
  getNextStatus: {
    parameters: [
      {
        dataType: "textEnum",
        displayName: "Current Status",
        textEnum: {
          options: [
            { value: "pending", displayName: "Pending" },
            { value: "processing", displayName: "Processing" },
          ],
        },
      },
    ],
    returns: { dataType: "textEnum" },
  },
}
```

### Callback parameters

A parameter can itself be a function. Set its `dataType` to `'function'`, then describe the callback's parameters and return type under `function`.

```ts
functions: {
  retryOperation: {
    parameters: [
      {
        dataType: "function",
        displayName: "Task to run",
        function: {
          parameters: [
            { dataType: "number", displayName: "Attempt Number" },
          ],
          returns: { dataType: "booleanValue" },
        },
      },
    ],
    returns: { dataType: "booleanValue" },
  },
}
```

### Link parameters

For a parameter that accepts a navigation link, set `dataType` to `'link'`. The optional `link` field restricts which kinds of links are allowed. For the available fields, see [Link](https://dev.wix.com/docs/site-extensions/editor-react-components/editor-react-types/reference/link.md).

```ts
functions: {
  navigateTo: {
    parameters: [
      {
        dataType: "link",
        displayName: "Destination",
        link: {
          linkTypes: ["externalLink", "pageLink"],
        },
      },
    ],
  },
}
```

### Event parameters

To handle an element event, such as a click, give a parameter `domEvent` instead of `dataType`. A Wix user connects the function to the element in **Settings > Advanced > Event handlers**. The editor asks for values for the function's other parameters, not for the event parameter. Wix passes those values first and the event after them, so make the event parameter the last parameter.

Make the event parameter optional, and write the function so it also works without the event:

- For **On Click**, Wix calls the function without the event, so the event parameter is `undefined`.
- For **On Double Click** and **On Mouse In**, the function receives a React synthetic event that wraps the DOM event. It has the DOM event's common properties and methods, such as `target`, `clientX`, and `preventDefault()`. Its `nativeEvent` property holds the DOM event itself.

`domEvent` accepts the following values, one for each category of DOM event:

| Value | Related DOM event | Replaces deprecated data type | Use for |
| --- | --- | --- | --- |
| `POINTER` | [`PointerEvent`](https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent) | `onClick` | Clicks and taps. |
| `CHANGE` | [`Event`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/change_event) | `onChange` | Value changes in inputs, selections, and checkboxes. |
| `KEYBOARD` | [`KeyboardEvent`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent) | `onKeyPress`, `onKeyUp` | Key presses and releases. |
| `FOCUS` | [`FocusEvent`](https://developer.mozilla.org/en-US/docs/Web/API/FocusEvent) | None | Focus and blur. |
| `SUBMIT` | [`SubmitEvent`](https://developer.mozilla.org/en-US/docs/Web/API/SubmitEvent) | `onSubmit` | Form submissions. |

For example, the following function handles a click:

```ts
functions: {
  scrollToTop: {
    displayName: "Scroll to Top",
    description: "Scrolls the page back to the top when the site visitor clicks",
    parameters: [
      { domEvent: "POINTER", displayName: "Click Event" },
    ],
  },
}
```

The implementation declares the parameter as an optional React `SyntheticEvent` and doesn't depend on it:

```ts
import type { SyntheticEvent } from 'react';

export function scrollToTop(event?: SyntheticEvent): void {
  window.scrollTo({ top: 0, behavior: "smooth" });
}
```

### Change events

For a `CHANGE` parameter, `changeEvent.inputType` names the kind of input that triggers the event. It accepts `text`, `checkbox`, `number`, and `date`:

```ts
functions: {
  isChecked: {
    parameters: [
      {
        domEvent: "CHANGE",
        changeEvent: { inputType: "checkbox" },
        displayName: "Checkbox Change",
      },
    ],
    returns: { dataType: "booleanValue" },
  },
}
```

## Return type fields

The `returns` field describes a function's return value:

| Field | Required | Description |
| --- | --- | --- |
| `dataType` | Yes | The [type of value](https://dev.wix.com/docs/site-extensions/editor-react-components/editor-react-types/overview/about-editor-react-types.md) this function returns. |
| `displayName` | No | Name of the return value shown in the editor. |
| `description` | No | Description of the return value. |
| `arrayItems` | When `dataType` is `arrayItems` | Defines what elements in the returned [array](#arrays) look like. |
| `data` | When `dataType` is `data` | Defines the fields inside the returned [nested object](#nested-objects). |
| `function` | When `dataType` is `function` | Defines the parameters and return type of the returned [callback](#callback-parameters). |
| `textEnum` | When `dataType` is `textEnum` | List of allowed [string options](#text-enums). |
| `link` | When `dataType` is `link` | Configuration for the returned [link](#link-parameters), such as supported link types. |

A return type can't use the `schema` or `container` data types, the deprecated event handler data types, or `domEvent`.

## Void functions

If a function doesn't return a value, omit `returns`.

```ts
functions: {
  logMessage: {
    parameters: [
      { dataType: "text", displayName: "Message to log" },
    ],
  },
}
```

## Async functions

Set `async` to `true` for a function whose implementation returns a Promise. The `returns` field describes the resolved value's type, not the Promise.

The editor doesn't offer an async function for an element property, only for event handlers. Keep a function that computes a property value synchronous.

```ts
functions: {
  fetchData: {
    parameters: [
      { dataType: "webUrl", displayName: "API URL" },
    ],
    returns: { dataType: "text" },
    async: true,
  },
}
```

## See also

- [About Function Libraries](https://dev.wix.com/docs/site-extensions/function-libraries/about-function-libraries.md)
- [About Function Libraries in Studio Two](https://dev.wix.com/docs/studio-two/code-your-site/code-model/function-libraries/about-function-libraries.md)
- [About the Function Library Manifest](https://dev.wix.com/docs/site-extensions/function-libraries/about-the-function-library-manifest.md)
- [Function Library Resources Reference](https://dev.wix.com/docs/site-extensions/function-libraries/function-library-resources-reference.md)
- [About Editor React Types](https://dev.wix.com/docs/site-extensions/editor-react-components/editor-react-types/overview/about-editor-react-types.md)