Skip to content
QwryAI
Documentation
Pages

Widgets

Functions

Decide what happens when visitors use a Widget: API calls, messages, links, uploads and data updates.

Functions are the behavior behind a Widget's buttons, forms and fields. You attach a function to an element by its Function name, and the function's Function type decides what happens when a visitor uses that element.

Manage functions in the builder's Functions tab.

The Functions tab lists every function in the Widget.
jsx
<Button label="Track package" onClickAction={{ functionName: "trackPackage" }} />

Click a function to open it, and Add function to create one.

Open a function to edit its type and settings.
Add function adds a Call API function to fill in.

Function Types

Each type does a different job. Pick the one that matches what the element should do.

The Function type menu.
Call API

Sends a request to an API endpoint and uses the response. The request runs on QwryAI's servers, so visitors never see its settings.

SettingDescription
URLThe endpoint to call. Supports template tokens.
MethodGET, POST, PUT, PATCH or DELETE.
HeadersKey-value pairs sent as request headers. Supports template tokens.
ParametersQuery parameters added to the URL. Supports template tokens.
BodyThe request body. Supports template tokens.
Return schemaResponse fields to save into Widget variables, each with a type (Text, Number, Boolean, Date or Object) and an Array option. Populate from API calls your endpoint and fills these in from its response.
On startFollow-up that runs before the request is sent.
On successFollow-up when the request succeeds.
On failureFollow-up when the request fails.
Additional inputsValues passed in where the function is used. See Additional Inputs.
Upload file

Uploads files from the visitor's browser straight to your endpoint, one request per file. QwryAI doesn't store the files. Attach it to a Button to open the visitor's file picker. See File Uploads for the endpoint contract.

SettingDescription
EndpointYour upload URL and method, POST or PUT. It must start with https://. Supports template tokens.
File fieldThe multipart field the file is sent under. Default file.
Target fieldThe Widget variable that stores the uploaded file's URL.
Accepted typesComma-separated extensions or MIME types, for example .pdf, image/*. Empty accepts any type.
URL fieldPath in your JSON response that holds the file's URL, for example data.url.
Max size (MB)Size limit per file, up to 30 MB.
Allow multipleLets the visitor pick more than one file.
Max filesHow many files they can pick, up to 5. Shown when Allow multiple is on.
HeadersRequest headers. They're sent from the visitor's browser, so visitors can see them. Content-Type isn't allowed.
Query paramsQuery parameters added to the URL. Supports template tokens.
Form fieldsExtra multipart fields sent with every file. Supports template tokens.
Return schemaResponse fields to save into Widget variables.
On startFollow-up that runs once, before the first file is sent.
On success (per file)Follow-up that runs for each uploaded file.
On failure (per file)Follow-up that runs for each file that failed.
Additional inputsValues passed in where the function is used. See Additional Inputs.

Besides the Widget's variables, the endpoint, query parameters and form fields can use {{conversationId}}, {{chatbotId}} and {{widgetId}}.

Run client-side code

Runs a function on your own website, in the visitor's browser. Use it to reach your page's own data or code, such as a cart.

SettingDescription
Wait for responseWaits for your function's result, for up to 60 seconds.
InputsKey-value pairs passed to your function. Supports template tokens.
Return schemaFields from your function's data to save into Widget variables.
On startFollow-up that runs before your function is called.
On successFollow-up when your function succeeds.
On failureFollow-up when it fails or times out.
Additional inputsValues passed in where the function is used. They're passed to your function along with Inputs.

Handling the function on your page

Register a handler on your website with registerTools, the same call used for client-side Actions. The key must match the function's name in the Widget. The handler gets the function's inputs as one object and returns a result. This example assumes the function's Inputs are sku set to {{sku}} and quantity set to {{quantity}}:

JavaScript
window.qwryai("registerTools", {
  addToCart: async ({ sku, quantity }) => {
    try {
      const response = await fetch("/cart/add", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ sku, quantity }),
      });
      if (!response.ok) {
        return { status: "error", error: "Could not add to cart" };
      }
      const cart = await response.json();
      return { status: "success", data: { cartCount: cart.itemCount } };
    } catch {
      return { status: "error", error: "Could not add to cart" };
    }
  },
});

The result must have one of these two shapes. Anything else counts as an error.

ResultShape
Success{ status: "success", data: { ... } }
Error{ status: "error", error: "message" }

Add a field from data, such as cartCount, to the Return schema to save it into a Widget variable of the same name, ready for the Widget and its On success follow-up.

When Wait for response is off, the Widget doesn't wait for your handler and runs On success straight away, so the handler's result is ignored.

Send message

Sends a message to the chat on the visitor's behalf.

SettingDescription
Message templateThe message. Insert values with {{fieldName}} template tokens.
Hide message from chatSends the message in the background without showing it in the chat.
On executeFollow-up that runs with the message.
Additional inputsValues passed in where the function is used. See Additional Inputs.
Open link

Opens a web page in a new tab, an email, or a phone call.

SettingDescription
Link typeWebsite (an https:// address), Email (mailto:) or Phone (tel:).
URLThe address to open. Supports template tokens.
On executeFollow-up that runs after the link opens.
Additional inputsValues passed in where the function is used. See Additional Inputs.
Open embedded page

Opens an HTTPS page in a window inside the chat, such as a booking page.

SettingDescription
Page URLAn https:// address or a Widget variable. Supports template tokens.
Modal titleOptional title shown above the page.
On successFollow-up after the page opens.
On failureFollow-up when it can't open.
Dismiss widget

Closes the Widget. Use it in two ways:

  1. Directly: attach it to a button so the visitor can close the Widget.
  2. As a follow-up: close the Widget after another function, for example after a successful API call.
Set variables

Updates the Widget's variables without any outside call. Use it to store a visitor's choice or move between views.

SettingDescription
VariablesKey-value pairs to set. Values support template tokens.
Additional inputsValues passed in where the function is used. See Additional Inputs.
Submit to action

Sends the form's values to the Collect data action that showed this Widget, so the agent can carry on with them.

SettingDescription
On successFollow-up after the values are sent.
On failureFollow-up when they can't be sent.

This works only when the Widget is shown by a Collect data action.

Loading Behavior

Choose how the Widget shows that a function is running by setting loadingBehavior in the element's action:

jsx
<Button label="Refresh" onClickAction={{ functionName: "refreshStatus", loadingBehavior: "container" }} />
ValueBehavior
autoThe default. A form submit covers the Widget with a loading overlay; a button shows its own spinner.
noneNo loading indicator.
selfThe element that was used shows a spinner.
containerThe whole Widget shows a loading overlay.

Additional Inputs

Sometimes a function needs a value that depends on where it's used. For example, a list of order items might each have a Return button that passes a different sku to the same function.

jsx
{items.map((item) => (
  <Button
    key={item.sku}
    label={`Return ${item.name}`}
    onClickAction={{ functionName: "startReturn", additionalInputs: { sku: item.sku } }}
  />
))}

Declare each one under Additional inputs in the function, with a type of string, number or boolean. Then use it as a template token anywhere the function accepts one, such as the URL, headers, body or parameters. For the example above, write {{sku}}.

After a Function Runs

Functions can run a follow-up when they finish, and some before they start.

  • Call API, Run client-side code and Upload file have three slots: On start, On success and On failure. For Upload file, On start runs once before the first file, and On success (per file) and On failure (per file) run for every file, with {{file.url}}, {{file.name}}, {{file.size}}, {{file.type}} and {{file.error}} available as template tokens.
  • Open embedded page and Submit to action have On success and On failure.
  • Send message and Open link have one On execute slot.

Each slot can do one of these:

Follow-upDescription
NoneNothing else happens.
Dismiss widgetCloses the Widget. The dismissal is remembered after a reload.
Set variablesUpdates Widget variables. Values can use template tokens, including fields the Return schema just saved.

On start runs before the call is made, and the variables it sets apply to the Widget straight away, so a Loading state shows while the request runs.

A common pattern is a status variable that drives states:

SlotSet variables
On startfetchStatus to loading
On successfetchStatus to loaded
On failurefetchStatus to error

Then add Loading, Loaded and Error states whose conditions check that fetchStatus is equal to loading, loaded or error.

Template Tokens

Template tokens are placeholders written as {{fieldName}} that are replaced with current values when the function runs. Use dot notation for nested values, such as {{customer.email}}. They work in any setting that takes dynamic content, including URLs, headers, bodies, messages and variables.

When a setting holds only a token, such as "{{items}}", the value keeps its type: a number stays a number and a list stays a list.

Results

Every function ends one of two ways:

OutcomeDescription
SuccessThe function finished. Call API and Run client-side code save any Return schema fields, then run On success.
ErrorThe function failed. On failure runs if it's set; otherwise the visitor sees the error.

Next steps

Explore the documentation