Skip to main content

Overview

Custom actions can run on both the server, or on the client. It would be beneficial to run a custom action client-side if the agent owner wants more flexibility with the action code, or wants to send an API request on behalf of the user, from the client side.
Make sure to load the popup chat script before setting up client actions.

registerTools()

Create client action, load the ChatNode script and call the registerTools method with all the actions and include all the logic that will run when each action is called. The action names must match that written in the custom client action page.

Method Parameters

Each custom action method receives one parameters: args: Contains all the arguments defined in your custom client action configuration
window.chatnode.registerTools({
  get_weather: async (args) => {
    // args contains parameters defined in your custom action
    try {
      const response = await fetch(
        `https://wttr.in/${args.location}?format=j1`,
      );

      if (!response.ok) {
        throw new Error("Failed to fetch weather data.");
      }

      const data = await response.json();

      return { data, status: "success" };
    } catch (error) {
      // Return only the error message without any data
      return { status: "error", message: error.message };
    }
  },
});

Response Format

Custom client actions should return responses in the following format:

Success Response

When the action is successful, return both the data and status:
//data should be either JSON or string

// Example with JSON data
{
  data: {
    temperature: 72,
    condition: "sunny",
    humidity: 45
  },
  status: "success"
}

// Example with string data
{
  data: "The weather in New York is currently 72°F and sunny",
  status: "success"
}

Error Response

When an error occurs, return only the status and error:
{
  status: "error",
  error: "Error message here"
}

Important Notes

Multiple Registrations: Calling registerTools multiple times will override previous actions. Make sure to provide all desired actions in a single registerTools call. Environment Limitations: Client-side custom actions will not work in the ChatNode in-app chat, as these environments don’t support client-side code execution.
I