> ## Documentation Index
> Fetch the complete documentation index at: https://docs.release0.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Using custom HTML and JavaScript with your Agent

> Learn how to integrate Release0 agents using HTML and JavaScript for web applications. Customize agent appearance, trigger actions, and handle events with ease using the provided code snippets and examples.

## Standard

You can get the standard HTML and JavaScript code by clicking on the “HTML & Javascript” button in the “Share” tab of your **agent dialog**. In this section, you can modify container dimensions.

Below is an example:

```html theme={null}
<script type="module">
  import Agent from 'https://cdn.jsdelivr.net/npm/@release0.com/js@1.1.9/dist/web.js'

  Agent.initStandard({
    agent: 'my-agent',
  })
</script>

<agent-standard style="width: 100%; height: 600px; "></agent-standard>
```

<Frame style={{ maxWidth: '400px' }}>
  <img src="https://mintcdn.com/release0-13d037a7/YWq2zuUGbHzFURAJ/images/integrations/javascript-standard.png?fit=max&auto=format&n=YWq2zuUGbHzFURAJ&q=85&s=a8cca8b04a32525a09e661a12004059d" alt="JavaScript standard" width="542" height="543" data-path="images/integrations/javascript-standard.png" />
</Frame>

This code is creating a container with a 100% width (will match parent width) and 600px height.

<Accordion title="See all possible settings">
  ```ts theme={null}
  type AgentProps = {
    id?: string;
    agent: string | any;
    isPreview?: boolean;
    resultId?: string;
    initialContext?: Record<string, unknown>;
    apiHost?: string;
    font?:
      | string
      | {
          type: "Google";
          family?: string | undefined;
        }
      | {
          type: "Custom";
          url?: string | undefined;
          family?: string | undefined;
          css?: string | undefined;
        };
    progressBarRef?: HTMLDivElement;
    startFrom?:
      | {
          type: "group";
          groupId: string;
        }
      | {
          type: "event";
          eventId: string;
        };
    sessionId?: string;
    theme?: {
      chatWindow?: {
        backgroundColor?: string;
        maxWidth?: string;
        maxHeight?: string;
      };
      button?: {
        size?: "medium" | "large" | `${number}px`;
        backgroundColor?: string;
        iconColor?: string;
        customIconSrc?: string;
        customCloseIconSrc?: string;
      };
      previewMessage?: {
        backgroundColor?: string;
        textColor?: string;
        closeButtonBackgroundColor?: string;
        closeButtonIconColor?: string;
      };
      placement?: "left" | "right";
    };
    previewMessage?: {
      avatarUrl?: string;
      message: string;
      autoShowDelay?: number;
    };
    autoShowDelay?: number;
    onNewInputelement?: (inputelement: any) => void;
    onAnswer?: (answer: { message: string; elementId: string }) => void;
    onInit?: () => void;
    onEnd?: () => void;
    onNewLogs?: (
      logs: {
        status: string;
        description: string;
        details?: unknown;
      }[],
    ) => void;
    onChatStatePersisted?: (isEnabled: boolean) => void;
    onScriptExecutionSuccess?: (message: string) => void;
  };
  ```
</Accordion>

### Multiple agents

To display multiple agents on the same page, assign unique `id`props to each:

```html theme={null}
<script type="module">
  import Agent from 'https://cdn.jsdelivr.net/npm/@release0.com/js@1.1.9/dist/web.js'

  Agent.initStandard({
    id: 'agent1'
    agent: 'my-agent',
  })

  Agent.initStandard({
    id: 'agent2'
    agent: 'my-agent-2',
  })
</script>

<agent-standard
  id="agent1"
  style="width: 100%; height: 600px; "
></agent-standard>
...
<agent-standard
  id="agent2"
  style="width: 100%; height: 600px; "
></agent-standard>
```

## Popup

Get the popup HTML and JavaScript code by clicking on the “HTML & Javascript” button in the “Share” tab of your agent dialog.

<Frame style={{ maxWidth: '400px' }}>
  <img src="https://mintcdn.com/release0-13d037a7/YWq2zuUGbHzFURAJ/images/integrations/javascript-popup.png?fit=max&auto=format&n=YWq2zuUGbHzFURAJ&q=85&s=ae6cd40098ffe53271314036c51e47cb" alt="JavaScript popus" width="549" height="396" data-path="images/integrations/javascript-popup.png" />
</Frame>

This will trigger the popup window automatically after **3 seconds**.

<Accordion title="See all possible settings">
  ```ts theme={null}
  type PopupProps = {
    id?: string;
    agent: string | any;
    isPreview?: boolean;
    resultId?: string;
    initialContext?: Record<string, unknown>;
    apiHost?: string;
    font?:
      | string
      | {
          type: "Google";
          family?: string | undefined;
        }
      | {
          type: "Custom";
          url?: string | undefined;
          family?: string | undefined;
          css?: string | undefined;
        };
    progressBarRef?: HTMLDivElement;
    startFrom?:
      | {
          type: "group";
          groupId: string;
        }
      | {
          type: "event";
          eventId: string;
        };
    sessionId?: string;
    theme?: {
      chatWindow?: {
        backgroundColor?: string;
        maxWidth?: string;
        maxHeight?: string;
      };
      button?: {
        size?: "medium" | "large" | `${number}px`;
        backgroundColor?: string;
        iconColor?: string;
        customIconSrc?: string;
        customCloseIconSrc?: string;
      };
      previewMessage?: {
        backgroundColor?: string;
        textColor?: string;
        closeButtonBackgroundColor?: string;
        closeButtonIconColor?: string;
      };
      placement?: "left" | "right";
    };
    previewMessage?: {
      avatarUrl?: string;
      message: string;
      autoShowDelay?: number;
    };
    autoShowDelay?: number;
    onNewInputelement?: (inputelement: any) => void;
    onAnswer?: (answer: { message: string; elementId: string }) => void;
    onInit?: () => void;
    onEnd?: () => void;
    onNewLogs?: (
      logs: {
        status: string;
        description: string;
        details?: unknown;
      }[],
    ) => void;
    onChatStatePersisted?: (isEnabled: boolean) => void;
    onScriptExecutionSuccess?: (message: string) => void;
    autoShowDelay?: number;
    theme?: {
      width?: string;
      backgroundColor?: string;
      zIndex?: number;
    };
    defaultOpen?: boolean;
    isOpen?: boolean;
    onOpen?: () => void;
    onClose?: () => void;
  };
  ```
</Accordion>

## Bubble

Click the “HTML & Javascript” button in the “Share” tab of your **agent dialog** to integrate the bubble.

<Frame style={{ maxWidth: '400px' }}>
  <img src="https://mintcdn.com/release0-13d037a7/YWq2zuUGbHzFURAJ/images/integrations/javascript-bubble.png?fit=max&auto=format&n=YWq2zuUGbHzFURAJ&q=85&s=29aec17b6ea93922f4d74148bcf8014f" alt="JavaScript Bubble" width="539" height="628" data-path="images/integrations/javascript-bubble.png" />
</Frame>

Here is an example:

```html theme={null}
<script type="module">
  import Agent from 'https://cdn.jsdelivr.net/npm/@release0.com/js@1.1.9/dist/web.js'

  Agent.initBubble({
    agent: 'my-agent',
    previewMessage: {
      message: 'I have a question for you!',
      autoShowDelay: 5000,
      avatarUrl: 'https://release0.com/images/logo512.png',
    }, // Defaults to undefined
    theme: {
      button: { backgroundColor: '#0042DA', iconColor: '#FFFFFF' },
      previewMessage: { backgroundColor: '#ffffff', textColor: 'black' },
      chatWindow: { backgroundColor: '#ffffff', maxWidth: '100%' },
    },
  })
</script>
```

This will display a bubble with a preview message after **5 seconds**.

<Accordion title="See all possible settings">
  ```ts theme={null}
  type BubbleProps = {
    id?: string;
    agent: string | any;
    isPreview?: boolean;
    resultId?: string;
    initialContext?: Record<string, unknown>;
    apiHost?: string;
    font?:
      | string
      | {
          type: "Google";
          family?: string | undefined;
        }
      | {
          type: "Custom";
          url?: string | undefined;
          family?: string | undefined;
          css?: string | undefined;
        };
    progressBarRef?: HTMLDivElement;
    startFrom?:
      | {
          type: "group";
          groupId: string;
        }
      | {
          type: "event";
          eventId: string;
        };
    sessionId?: string;
    theme?: {
      chatWindow?: {
        backgroundColor?: string;
        maxWidth?: string;
        maxHeight?: string;
      };
      button?: {
        size?: "medium" | "large" | `${number}px`;
        backgroundColor?: string;
        iconColor?: string;
        customIconSrc?: string;
        customCloseIconSrc?: string;
      };
      previewMessage?: {
        backgroundColor?: string;
        textColor?: string;
        closeButtonBackgroundColor?: string;
        closeButtonIconColor?: string;
      };
      placement?: "left" | "right";
    };
    previewMessage?: {
      avatarUrl?: string;
      message: string;
      autoShowDelay?: number;
    };
    autoShowDelay?: number;
    onNewInputelement?: (inputelement: any) => void;
    onAnswer?: (answer: { message: string; elementId: string }) => void;
    onInit?: () => void;
    onEnd?: () => void;
    onNewLogs?: (
      logs: {
        status: string;
        description: string;
        details?: unknown;
      }[],
    ) => void;
    onChatStatePersisted?: (isEnabled: boolean) => void;
    onScriptExecutionSuccess?: (message: string) => void;
    onOpen?: () => void;
    onClose?: () => void;
    onPreviewMessageClick?: () => void;
    onPreviewMessageDismissed?: () => void;
  };
  ```
</Accordion>

### Custom button position

You can adjust the position of the button with custom CSS. For example:

```css theme={null}
agent-bubble::part(button) {
  bottom: 60px;
}

agent-bubble::part(agent) {
  bottom: 140px;
  height: calc(100% - 140px)
}
```

If you have a preview message, you'll also have to manually position it:

```css theme={null}
agent-bubble::part(preview-message) {
  bottom: 140px;
}
```

## Commands

Here are the commands you can use to trigger your embedded agent:

* `Agent.open()`: Open popup or bubble

* `Agent.close()`: Close popup or bubble

* `Agent.toggle()`: Toggle the bubble or popup open/close state,

* `Agent.showPreviewMessage()`: Show preview message from the bubble,

* `Agent.hidePreviewMessage()`: Hide preview message from the bubble,

* `Agent.setPrefilledVariables(...)`: Set prefilled variables.
  Example:

  ```js theme={null}
  Agent.setPrefilledVariables({
    Name: 'Jhon',
    Email: 'john@gmail.com',
  })
  ```

  For more information, check out [Additional configuration](#additional-configuration).

* `Agent.setInputValue(...)`: Set the value in the currently displayed input.

You can bind these commands on a button element, for example:

```html theme={null}
<button onclick="Agent.open()">Contact us</button>
```

<Note>For each command you can pass an optional `id` prop to target a specific agent. I.e. `Agent.open({ id: 'my-bubble' })`</Note>

## Callbacks

If you need to trigger events on your parent website when the user interact with the agent, you can use the following callbacks:

```js theme={null}
Agent.initStandard({
  agent: 'my-agent',
  onNewInputelement: (
  ) => {
    console.log('New input element displayed', inputelement.id)
  },
  onAnswer: (answer) => {
    console.log('Answer received', answer.message, answer.elementId)
  },
  onInit: () => {
    console.log('Agent initialized')
  },
  onEnd: () => {
    console.log('Agent ended')
  },
})
```

## Additional configuration

You can prefill the agent variable values in your embed code by adding the `initialContext` option. Here is an example:

```js theme={null}
Agent.initStandard({
  agent: 'my-agent',
  initialContext: {
    'Current URL': 'https://release0.com',
    'User name': 'John Doe',
  },
})
```

It will prefill the `Current URL` variable with "[https://release0.com](https://release0.com)" and the `User name` variable with "John Doe". More info about variables: [here](../../../editor/variables).

Note that if your site URL contains query params (i.e. [https://r0.ar/myagent?User%20name=John%20Doe](https://r0.ar/myagent?User%20name=John%20Doe)), the variables will automatically be injected to the agent. So you don't need to manually transfer query params to the agent embed configuration.
