Skip to main content
Voiceflow’s chat widget API gives you programmatic control over the widget’s behavior. You can customize how conversations persist, pass in user data, trigger intents, send proactive messages, and listen for events.

Understanding the default snippet

When you copy the widget code from Widget in the sidebar, you get a snippet like this:
The chat.load() function initializes the widget. You can extend it with additional configuration options described in this guide.

Choosing which environment the widget loads

By default, the widget respects your project’s traffic split: each new session is routed to an environment based on the split you’ve configured, so the widget works with A/B tests and gradual rollouts without any extra configuration. To force the widget to load a specific environment, pass that environment’s alias as versionID. Copy the alias from the Alias column in SettingsEnvironments.
When versionID is set, the widget loads that environment’s live version and ignores the traffic split. If the environment has no published version yet, the widget won’t render any messages.
If your project was created before environments launched and has not yet been migrated, the legacy development, staging, and production values continue to work as versionID. After migrating, update your snippet to use your new environment aliases (or remove versionID to use the traffic split).

Setting the runtime URL

This feature is only available to Enterprise customers who are hosted on a Private Cloud.
The url property defines the runtime endpoint the widget communicates with. Private Cloud customers should replace the default URL with their dedicated runtime endpoint provided by Voiceflow.

Configuring chat persistence

Chat persistence determines whether users return to an ongoing conversation or start fresh when they reload the page, open a new tab, or come back later. Configure this with the persistence property inside the assistant object.

Passing a custom user ID

You can assign a unique ID to each user with the userID property. This ID becomes available in your agent as the built-in {user_id} variable, which is useful for identifying users across sessions and personalizing interactions.

Passing custom variables

You can pre-fill variables when the widget loads using the launch.event.payload field. These values populate the last_event system variable and can be accessed using Code step.
These values can be accessed using a Code step in the format shown below. Many users use an initialization workflow to run logic based on these values.
Note that the Code step can’t create new variables. You’ll need to create user_name and user_email as variables in your project first. Also, last_event is updated on each user interaction (eg: when the widget loads, an intent triggers, or a button is clicked), so capture these values at the start of the conversation.

Annotating transcripts with metadata

You can pass user profile information that appears in your Transcripts. This metadata is for labeling conversations in the UI only and isn’t available to your agent’s logic.

API methods

Once the widget script loads, it registers the API as window.voiceflow.chat. You can use these methods to control the widget programmatically.

Examples

Open the widget automatically after 3 seconds:
Trigger an intent when a button is clicked:

Sending proactive messages

Proactive messages appear outside the chat window before users open it. They’re useful for prompting engagement, such as offering help or announcing promotions. These messages don’t appear in transcripts and don’t consume credits.

Pushing messages

Use window.voiceflow.chat.proactive.push() to send one or more messages:

Clearing messages

Use window.voiceflow.chat.proactive.clear() to hide all proactive messages:

Triggering messages based on user behavior

You can trigger proactive messages when users perform specific actions. For example, show a message when someone visits a certain page:

Listening for events

The widget emits events you can listen for using the message event listener. Voiceflow events are stringified JSON objects with a type beginning with voiceflow:.

Allowing dangerous HTML elements

Only enable dangerous HTML if you understand XSS risks and are using your own trusted code. Learn more about XSS vulnerabilities.
By default, certain HTML elements like <script> and <iframe> are blocked in messages for security reasons. You can enable them, but doing so introduces cross-site scripting (XSS) vulnerabilities.
The following elements are allowed by default: