> 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: Build a Function Library in Studio Two

## Article: Build a Function Library in Studio Two

## Article Link: https://dev.wix.com/docs/studio-two/code-your-site/code-model/function-libraries/build-a-function-library-in-studio-two.md

## Article Content:

# Build a Function Library in Studio Two

A [function library](https://dev.wix.com/docs/studio-two/code-your-site/code-model/function-libraries/about-function-libraries.md) is a manifest plus an implementation file. You generate the files first, then declare each function in the manifest and export a matching function from the implementation.

This article walks through that loop from a scaffolded library to one with your own functions.

Follow the instructions below to:

1. Scaffold the library.
2. Declare functions in the manifest.
3. Add a module specifier.
4. Implement the functions.

## Step 1 | Scaffold the library

Start from a generated function library rather than an empty folder, so the file structure and registration are already in place.

For the full procedure, see [Add a Function Library Extension in Studio Two](https://dev.wix.com/docs/studio-two/code-your-site/code-model/function-libraries/add-a-function-library-extension-in-studio-two.md).

You can find the new files under `src/extensions/site/function-libraries/<extension-folder>/`. The remaining steps use 2 of them: the manifest, `<extension-folder>.extension.ts`, and the implementation, `<extension-folder>.ts`. For what every file in the folder is for, see [Function Library Extension Files and Code](https://dev.wix.com/docs/site-extensions/function-libraries/function-library-extension-files-and-code.md).

## Step 2 | Declare functions in the manifest

`<extension-folder>.extension.ts` is the manifest you own. It names the library and lists each function a Wix user can connect in the editor.

A generated library includes a working `formatPrice` function as a starting point:

```ts
import { extensions } from '@wix/custom-extensions';

export default extensions.functionLibrary({
  id: 'a1b2c3d4-e5f6-7890-abcd-ef1234567890',
  name: 'My Function Library',
  type: 'companionApp.myFunctionLibrary',
  functions: {
    formatPrice: {
      displayName: 'Format price',
      description: 'Formats an amount as a price string',
      parameters: [
        {
          dataType: 'number',
          displayName: 'Amount',
        },
        {
          dataType: 'text',
          displayName: 'Currency symbol',
          optional: true,
        },
      ],
      returns: {
        dataType: 'text',
        displayName: 'Formatted price',
      },
    },
  },
  resources: {
    client: {
      url: './extensions/site/function-libraries/my-function-library/my-function-library.ts',
    },
  },
});
```

Keep the `id` field stable once other code or editor bindings depend on it. Change `name` when you want a different label in the editor.

Add an entry to `functions` for every function you want to expose. The key is the function name. The `displayName` field is what a Wix user sees when connecting the function, and `description` says what it does. Each parameter's `dataType` or `domEvent`, and the return value's `dataType`, must match the implementation.

For every field, see [About the Function Library Manifest](https://dev.wix.com/docs/site-extensions/function-libraries/about-the-function-library-manifest.md) and [Function Reference](https://dev.wix.com/docs/site-extensions/function-libraries/function-reference.md).

### Declare an event handler

For a function that handles an element event, such as a button click, declare its event parameter with `domEvent` instead of `dataType`. For example, the following function scrolls the page to the top when a button is clicked:

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

For every event type, see [Event parameters](https://dev.wix.com/docs/site-extensions/function-libraries/function-reference.md#event-parameters).

## Step 3 | Add a module specifier

The generated manifest has no module specifier. Add `moduleSpecifier` directly under `resources`, starting with `@/` and followed by the library folder name:

```ts
resources: {
  client: {
    url: './extensions/site/function-libraries/my-function-library/my-function-library.ts',
  },
  moduleSpecifier: '@/my-function-library',
},
```

The module specifier must be unique in your site. After Wix registers the library, don't change it. For the rules, see [Function Library Resources Reference](https://dev.wix.com/docs/site-extensions/function-libraries/function-library-resources-reference.md#module-specifier).

## Step 4 | Implement the functions

Write the functions in `<extension-folder>.ts`. Export one named function per key in the manifest's `functions` map. Parameter order, requiredness, and types must match the manifest.

The generated implementation for `formatPrice` looks like this:

```ts
/**
 * This file contains the utility functions exposed by this function library.
 *
 * Every exported function must be described in the `functions` map of the extension
 * file, so that it can be used in the Wix editor.
 */
export function formatPrice(amount: number, currencySymbol = '$'): string {
  return `${currencySymbol}${amount.toFixed(2)}`;
}
```

Replace `formatPrice` or add more exports as your library grows. If you add a function to the implementation, add a matching entry to the manifest before a Wix user can connect it. For every rule that keeps the 2 files aligned, see [Function Library Extension Files and Code](https://dev.wix.com/docs/site-extensions/function-libraries/function-library-extension-files-and-code.md#implementation).

### Implement an event handler

Make an event parameter an optional React `SyntheticEvent`, and write the function so it also works without the event. A button's **On Click** calls the function without it. For what other event handlers pass, see [Event parameters](https://dev.wix.com/docs/site-extensions/function-libraries/function-reference.md#event-parameters).

Not every element offers event handlers. To see which handlers an element has, select it in the editor and open the inspector's **Settings** tab > **Advanced** > **Event handlers**.

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

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

### Publish the library

When the library behaves the way you want, click **Publish** to ship it with your site.

## See also

- [About Function Libraries](https://dev.wix.com/docs/studio-two/code-your-site/code-model/function-libraries/about-function-libraries.md)
- [Add a Function Library Extension in Studio Two](https://dev.wix.com/docs/studio-two/code-your-site/code-model/function-libraries/add-a-function-library-extension-in-studio-two.md)
- [Function Library Extension Files and Code](https://dev.wix.com/docs/site-extensions/function-libraries/function-library-extension-files-and-code.md)
- [About the Function Library Manifest](https://dev.wix.com/docs/site-extensions/function-libraries/about-the-function-library-manifest.md)
- [Function Libraries](https://dev.wix.com/docs/overview/extensions-framework/site-extensions/function-libraries.md)