Skip to main content
Functions define the behavior behind interactive widget elements like buttons and forms. You attach a function to an element by referencing its function name, and the function’s type determines what happens when a user triggers it.

Function Types

Each function type serves a different purpose. Select the type that matches the action you want to perform.
Makes a request to an external API. The configuration is never visible to the end user.
Sends files from the visitor’s browser straight to your endpoint as multipart/form-data, one request per file. Chatbase never stores the files. Use it with the Dropzone and FileInput components, or attach it to a Button to open the visitor’s file picker. See the File Uploads guide for the endpoint contract and security practices.Besides the widget’s variables, the endpoint, headers, query parameters and form fields can use {{conversationId}}, {{chatbotId}}, {{widgetId}}, and {{file.name}}, {{file.size}}, {{file.type}}.
Sends a message to the parent webpage. This is designed for widgets embedded inside iframes that need to communicate with the host page.Handling the function on your pageRegister a handler on your website with registerWidgetFunctions. The key must match the function’s name in the widget. The handler receives the function’s Inputs as one object and returns a result. Only Inputs reach the handler, so to pass an additional input, add an input that references it. This example assumes the function’s Inputs are classId set to {{classId}} and email set to {{email}}:
The result must be one of these two shapes. Anything else is treated as an error.Each field in data can be used as a template token in the On Success follow-up. In the example above, a Set Variables follow-up can save {{bookingId}} to a widget variable.When Wait for Response is off, the widget doesn’t wait for the handler and runs On Success right away, so the handler’s return value is ignored:
  • Register every widget function in a single registerWidgetFunctions call. Calling it again replaces the functions registered earlier.
  • Return { status: 'error', error } instead of throwing. On the website widget, a handler that throws sends no response, so the widget waits the full 60 seconds before failing.
  • Only successful results expose data to follow-ups. On Failure cannot read fields from the handler’s response.
Using Center Stage? Register the same handlers with window.chatbaseCenterStage('registerWidgetFunctions', { ... }).
Sends a message into the current conversation.
Closes the widget. There are two ways to use this:
  1. Directly — Attach it to a button so the user can close the widget on click.
  2. As a follow-up — Use it as the follow-up action on another function (e.g., close the widget after a successful API call).
Dismissal state is persisted. If a user dismisses a widget, it stays dismissed even after a page refresh.
Updates widget data values without making any external calls. Useful for storing user selections or transforming data between steps.

Loading Behavior

When a function runs, you can control how the UI indicates progress.

Additional Inputs

Sometimes a function needs extra context that depends on where it is called. For example, you might have a list of classes, each with a “Book” button that should pass a different classId to the same function.
Once provided, you can reference additional inputs as template tokens anywhere the function accepts them — in the URL, headers, body, or parameters. For the example above, use {{classId}} to insert the value.
Additional inputs are not variables. They only exist for the duration of the function execution. If you need to persist a value, use a Set Variables action in On Start or in a follow-up that runs after the function.

After a Function Runs

Every function supports a follow-up action that runs after it completes. Some functions can also run one before it starts.
  • Server Function and Client Function provide three follow-up slots: On Start, On Success, and On Failure.
  • Upload File Function provides the same three slots. On start runs once per upload, before any file is sent. On success and On failure run once for every file, and in them {{file.url}}, {{file.name}}, {{file.error}} and any Return field are available as template tokens.
  • Send Message, Link, Dismiss, and Set Variables functions provide a single On Execute slot.
Each follow-up action can do one of the following: On Start runs before the call is made. The variables it sets are applied to the widget right away, so a state like Loading shows while the request is in flight, and they are already available as template tokens in that same request. If On Start dismisses the widget, the function still runs. Values set by any follow-up are saved with the conversation, so they survive a page reload. A common pattern is a status variable that drives states: Then add Loading, Loaded, and Error states whose conditions check fetchStatus equal to loading, loaded, or error.
Reset any value On Start sets in both On Success and On Failure. Otherwise a failed call can leave the widget stuck on its loading view.

Template Tokens

Template tokens are placeholders written as {{fieldName}} that get replaced with current values at runtime. You can use them in any setting that supports dynamic content, including URLs, headers, body content, messages, and variables. In a Server function body, a token inside quotes, such as "{{message}}", is escaped automatically when its value would break the JSON, for example a quote or a line break a visitor typed into a form.
For follow-up actions on Server and Client functions, template tokens can also reference fields from the response data. This lets you capture API results and store them as widget variables for use elsewhere.

Results

Every function produces one of two outcomes: