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

# Client Actions

## 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.

<Note>
  Make sure to load the [popup chat
  script](/developer-guides/javascript-sdk/quick-start) before setting up client
  actions.
</Note>

## `registerTools()`

[Create client action](/user-guides/actions/custom-action/client-action), [load the ChatNode script](/developer-guides/javascript-sdk/quick-start) 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

```javascript theme={null}
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`:

```javascript theme={null}
//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`:

```javascript theme={null}
{
  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.
