# Client-Side Custom Actions > Run your own code in the visitor's browser when the agent needs it, and send the result back to the conversation A client-side custom Action runs on your website instead of on QwryAI's servers. When the agent decides to use the Action, the Chat bubble calls a function you registered on your page; your function does the work, such as reading the cart, calling your own API with the visitor's session or opening part of your app, and returns a result the agent uses in its reply. ## Benefits of Client-Side Actions > [card] Direct Integration | /docs/developer-guides/website-integration/client-side-custom-actions#setup-guide | Work with your existing frontend code, state and routing. > [card] User Context Access | /docs/developer-guides/website-integration/client-side-custom-actions#function-parameters | Use the signed-in user's ID and the browser's own APIs, which a server can't reach. > [card] Enhanced User Experience | /docs/developer-guides/website-integration/client-side-custom-actions#advanced-examples | Change the page the visitor is on while they chat: open a product, fill a cart, start a tour. > [card] Frontend Control | /docs/developer-guides/website-integration/client-side-custom-actions#response-format | Call your API with the visitor's own cookies and session, with no extra keys. ## How Client-Side Actions Work When a visitor's message calls for a client-side Action: 1. The agent decides to use the Action and collects the inputs it needs from the conversation. 2. The Chat bubble calls the function you registered under the Action's name, in the visitor's browser. 3. Your function returns a success or error result, which goes back to the agent. 4. The agent continues the conversation using that result. ## Setup Guide ### Prerequisites > [!NOTE] > > A website with the QwryAI Chat bubble installed and working. New to QwryAI? Start with [Your first AI agent](/docs/user-guides/quick-start/your-first-agent). Client-side Actions run only where your code runs: on your website, not in the dashboard's Playground. **1. Create your custom Action** In your agent, open **Actions** and select **Create Action**. Choose **Custom actions**, then: * Under **General**, give the Action a name and describe when the agent should use it. * Under **Action type**, choose **Run client-side code**: *This action will run custom code on the client.* * Under **Inputs**, add the values the agent should collect from the visitor, such as an order number. ![The Custom Action editor's Action type step with Call API, Run client-side code, Call API + show widget and Show widget, and the pointer on Run client-side code.](annotated/01-action-type.webp "Action type › Run client-side code.") **2. Register your Action functions** The **Inputs** step shows **Connect your website**, with a snippet already named after your Action. Copy it, add it to your site after the Chat bubble embed code, then tick **I've added this code to my website.** ![The Connect your website panel with the window.qwryai registerTools snippet, its Copy button and the checkbox I've added this code to my website.](annotated/02-connect-website.webp "Connect your website: the registration snippet.") Fill in the function with your own code. Register every client-side Action in one `registerTools` call: ```javascript window.qwryai("registerTools", { get_order_status: async (args, user) => { try { // args holds the inputs you defined in the dashboard const response = await fetch(`/api/orders/${encodeURIComponent(args.orderNumber)}`, { credentials: "include", }); if (!response.ok) throw new Error("Order not found"); const order = await response.json(); return { status: "success", data: { status: order.status, eta: order.estimatedDelivery }, }; } catch (error) { return { status: "error", error: error.message }; } }, open_size_guide: async () => { document.getElementById("size-guide").showModal(); return { status: "success", data: "The size guide is open on the page." }; }, }); ``` > [!TIP] > > Register all your Actions in a single `registerTools` call. Each call replaces the functions registered before it. > [!WARNING] > > The function name must match the Action's name exactly. If the agent calls an Action your page hasn't registered, it gets the error `Client tool is not registered: `. **3. Choose channels and save** Under **Channels**, choose where the Action can run, then save. Client-side Actions only work on channels that run your website's code, such as the Chat bubble. ## Function Parameters Every client-side Action function receives two arguments: **args** `object` *required* The inputs the agent collected, with the names you set under **Inputs**. **user** `object` Who the visitor is. **user\_id** `string | null` The signed-in user's ID, from [identity verification](/docs/developer-guides/website-integration/identity-verification) or `setUser`'s `externalUserId`. `null` for anonymous visitors. **user\_hash** `string | null` A `user_hash` you passed to `setUser`, if any. QwryAI doesn't check it. **anon\_user\_id** `string | null` QwryAI's ID for the visitor. You can ignore it. **user\_metadata** `object` The visitor's custom attributes, from the identity token or `setUser`. Empty when there are none. > [!NOTE] > > Treat `user` as a hint, not proof. Anything in the browser can be changed by the visitor, so check permissions again on your server before you return private data. ## Response Format Your function must return one of two shapes: > [tab] Success Response > > Return `status: "success"` and `data`, which can be a string, an object or an array: > > ```javascript > { > status: "success", > data: responseData > } > ``` > > ```javascript > // Object > { status: "success", data: { status: "Shipped", eta: "2026-12-04" } } > > // String > { status: "success", data: "Your order shipped on Monday and arrives on Thursday." } > > // Array > { status: "success", data: [{ name: "Linen shirt", price: 49 }, { name: "Chino", price: 59 }] } > ``` > [tab] Error Response > > Return `status: "error"` and a non-empty `error` message: > > ```javascript > { > status: "error", > error: "Descriptive error message" > } > ``` > > ```javascript > // Not found > { status: "error", error: "We couldn't find an order with that number." } > > // Not signed in > { status: "error", error: "The visitor needs to sign in to see their orders." } > ``` > > If your function throws, or returns anything else, QwryAI reports an error to the agent for you. ## Advanced Examples This example uses the browser's geolocation API to find the visitor's nearest store: ```javascript window.qwryai("registerTools", { find_nearest_store: async () => { try { const position = await new Promise((resolve, reject) => navigator.geolocation.getCurrentPosition(resolve, reject) ); const { latitude, longitude } = position.coords; const response = await fetch(`/api/stores/nearest?lat=${latitude}&lon=${longitude}`); const store = await response.json(); return { status: "success", data: { name: store.name, address: store.address } }; } catch { return { status: "error", error: "The visitor didn't share their location." }; } }, }); ``` ![On a demo store page, the visitor asks Which store is closest to me?, the browser asks to share location, and the agent replies with the nearest store's name and address.](walkthroughs/client-action-nearest-store.gif) ## Best Practices > [accordion] Error Handling > > Catch errors and return a message the agent can pass on: > > ```javascript > window.qwryai("registerTools", { > my_action: async (args) => { > try { > if (!args.requiredParam) { > return { status: "error", error: "Required parameter is missing" }; > } > const result = await performAction(args); > return { status: "success", data: result }; > } catch (error) { > console.error("Action failed:", error); > return { status: "error", error: "Action failed. Please try again." }; > } > }, > }); > ``` > [accordion] Input Validation > > Check inputs before you use them: > > ```javascript > window.qwryai("registerTools", { > calculate_shipping: async ({ weight, destination }) => { > if (!weight || weight <= 0) return { status: "error", error: "A valid weight is required" }; > if (!destination) return { status: "error", error: "A destination is required" }; > const result = await calculateShipping(weight, destination); > return { status: "success", data: result }; > }, > }); > ``` > [accordion] Performance Optimization > > * Return only what the agent needs; smaller results answer faster. > * Set timeouts on your own API calls so the agent isn't left waiting. > * Cache data that rarely changes in the browser. > [!WARNING] > > Never put API keys or secrets in your Action code. It runs in the visitor's browser, where anyone can read it. Call your own backend and let it hold the secrets. > [!NOTE] > > The [event listeners](/docs/developer-guides/website-integration/chatbot-event-listeners) `tool-call` and `tool-result` fire for each client-side Action, which helps while you debug. ## Troubleshooting > [accordion] "Client tool is not registered" > > * Check that the function name matches the Action's name exactly, including case. > * Make sure `registerTools` runs on every page where the Chat bubble appears. > * Make sure your script runs after the embed code. > [accordion] No response from the Action > > * Make sure your function returns `{ status, data }` or `{ status, error }` on every path. > * Check the browser console for errors in your function. > * Add timeouts to slow network calls. > [accordion] The user details are empty > > * `user_id` is set only after the visitor is identified. See [Identity verification](/docs/developer-guides/website-integration/identity-verification). > * Call `identify` before the visitor reaches the Action. ## Next Steps > [card] Client-Side Custom Forms | /docs/developer-guides/website-integration/client-side-custom-forms | Show your own forms inside the chat. > [card] Chat Interface Control | /docs/developer-guides/website-integration/chat-interface-control | Open, close and reset the chat from your code. > [card] Event Listeners | /docs/developer-guides/website-integration/chatbot-event-listeners | Watch Action calls and results as they happen. > [card] Custom Action | /docs/user-guides/chatbot/actions/custom-action | Every Custom Action type in the dashboard.