> 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 an Embedded Script in Studio Two

## Article: Build an Embedded Script in Studio Two

## Article Link: https://dev.wix.com/docs/studio-two/code-your-site/build-a-custom-frontend/embedded-scripts/build-an-embedded-script-in-studio-two.md

## Article Content:

# Build an Embedded Script in Studio Two

An [embedded script](https://dev.wix.com/docs/studio-two/code-your-site/build-a-custom-frontend/embedded-scripts/about-embedded-scripts.md) is a configuration file plus an HTML fragment. You generate the files first, then spend most of your time in the HTML file.

This article walks through that loop from a scaffolded script to one that logs a message, can carry a value you pass when you load it, and loads on a site.

Follow the instructions below to:

1. Scaffold the script.
2. Set the configuration.
3. Write the HTML.
4. Load the script.
5. Publish and check the script.

## Step 1 | Scaffold the script

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

For the full procedure, see [Add an Embedded Script Extension in Studio Two](https://dev.wix.com/docs/studio-two/code-your-site/build-a-custom-frontend/embedded-scripts/add-an-embedded-script-extension.md).

You can find the new files under `src/extensions/site/embedded-scripts/<script-folder>/`. You spend most of this article in `<script-folder>.html`. For what every file in the folder is for, see [Embedded Script Extension Files and Code](https://dev.wix.com/docs/site-extensions/embedded-scripts/embedded-script-extension-files-and-code.md).

## Step 2 | Set the configuration

`<script-folder>.extension.ts` is the configuration file you own. It declares the script ID, name, placement, script type, and path to the HTML file.

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

export default extensions.embeddedScript({
  id: 'cc5c768a-ab95-4b57-af11-612255b50d22',
  name: 'My Script',
  placement: 'HEAD',
  scriptType: 'ESSENTIAL',
  source: './extensions/site/embedded-scripts/my-script/my-script.html',
});
```

Keep `id` stable once other code depends on it. When your project has more than one embedded script, pass this value as `componentId` to [Embed Script](https://dev.wix.com/docs/api-reference/app-management/embedded-scripts/embed-script?apiView=SDK.md).

`placement` is `HEAD`, `BODY_START`, or `BODY_END`. `scriptType` is `ESSENTIAL`, `FUNCTIONAL`, `ANALYTICS`, or `ADVERTISING`. Those values match the **Placement** and **Script type** choices from **Generate**.

For every field, see [Embedded Script Extension Files and Code](https://dev.wix.com/docs/site-extensions/embedded-scripts/embedded-script-extension-files-and-code.md#configuration-fields).

## Step 3 | Write the HTML

`<script-folder>.html` is the fragment injected into the page at the placement you set. A generated script logs a greeting and loads a local module:

```html
<script>console.log('Hello {{name}}');</script>
<script type="module" src="./logger.ts"></script>
```

`{{name}}` is a [dynamic parameter](https://dev.wix.com/docs/overview/extensions-framework/site-extensions/embedded-scripts.md#dynamic-parameters). The platform replaces it when the script is embedded. The placeholder has to sit inside quotes, and the name has to match the name you pass to the [Embed Script API](https://dev.wix.com/docs/api-reference/app-management/embedded-scripts/embed-script?apiView=SDK.md).

`logger.ts` is a generated module in the same folder. The HTML loads it with `type="module"`. You can replace the module, or delete that script tag if the fragment doesn't need a separate file.

Step 4 calls `embedScript()` without parameters, so the fragment can't contain a `{{name}}` placeholder. Replace it before you continue. For example, to log a fixed message instead of a parameter:

1. Open `<script-folder>.html`.
2. Replace `console.log('Hello {{name}}');` with `console.log('Hello world!');`.
3. Save the file.

When the script runs on the site, the browser console logs **Hello world!**.

The script loads on your site only after `embedScript()` runs, so publishing alone isn't enough. Step 4 adds the code that runs it.

## Step 4 | Load the script

Call [Embed Script](https://dev.wix.com/docs/api-reference/app-management/embedded-scripts/embed-script?apiView=SDK.md) from your own code to load the script on the site. A dashboard page with a button is one way to do it.

To load the script from a dashboard page:

1. Add a dashboard page extension, or open one you already have. For how, see [Add a Dashboard Page Extension in Studio Two](https://dev.wix.com/docs/studio-two/code-your-site/build-a-custom-dashboard/dashboard-pages/add-a-dashboard-page-extension-in-studio-two.md).
2. In the page's `.tsx` file, import `embeddedScripts` from `@wix/app-management`, which a new project includes. If your project doesn't have it, install it. Then add a function that calls `embedScript()`:

   ```tsx
   import { embeddedScripts } from '@wix/app-management';

   const loadScript = async () => {
     await embeddedScripts.embedScript({});
   };
   ```

   The method requires an object as its first argument, so pass `{}` if the script has no dynamic parameters. For the parameters, including dynamic parameters, see [Embed Script](https://dev.wix.com/docs/api-reference/app-management/embedded-scripts/embed-script?apiView=SDK.md).

3. Add a button that calls the function. Add `Button` to the page's existing `@wix/design-system` import, then place the button in the page:

   ```tsx
   <Button onClick={loadScript}>Load script</Button>
   ```

4. If your project has more than one embedded script, pass the `id` from the script's configuration file as `componentId` in `options`:

   ```tsx
   await embeddedScripts.embedScript({}, { componentId: '<script-id>' });
   ```

The page now calls Embed Script when someone clicks the button.

## Step 5 | Publish and check the script

To publish and check the script:

1. Click **Publish** in the Studio Two editor. The dashboard page appears in the site dashboard only after you publish.
2. In the site dashboard, open the dashboard page and click the button.
3. Open the live site in a new browser tab. Use a private window, or add a cache-busting query param, to avoid a cached copy.
4. Open the browser's developer tools and check the **Elements** panel for your script's placement, such as the head or the start or end of the body. Alternatively, check the **Console** panel for any `console.log` output your script produces.

If your script's content appears in the page HTML at the placement you configured, the script is working.

A site serves the script content from the last time `embedScript()` ran. After you change the script and publish, click the button again so the site picks up the new content.

## See also

- [About Embedded Scripts](https://dev.wix.com/docs/studio-two/code-your-site/build-a-custom-frontend/embedded-scripts/about-embedded-scripts.md)
- [Add an Embedded Script Extension in Studio Two](https://dev.wix.com/docs/studio-two/code-your-site/build-a-custom-frontend/embedded-scripts/add-an-embedded-script-extension.md)
- [Embedded Script Extension Files and Code](https://dev.wix.com/docs/site-extensions/embedded-scripts/embedded-script-extension-files-and-code.md)
- [Embed Script](https://dev.wix.com/docs/api-reference/app-management/embedded-scripts/embed-script?apiView=SDK.md)
- [Embedded Scripts](https://dev.wix.com/docs/overview/extensions-framework/site-extensions/embedded-scripts.md)