Website Integration
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
Direct Integration
Work with your existing frontend code, state and routing.
User Context Access
Use the signed-in user's ID and the browser's own APIs, which a server can't reach.
Enhanced User Experience
Change the page the visitor is on while they chat: open a product, fill a cart, start a tour.
Frontend Control
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:
- The agent decides to use the Action and collects the inputs it needs from the conversation.
- The Chat bubble calls the function you registered under the Action's name, in the visitor's browser.
- Your function returns a success or error result, which goes back to the agent.
- The agent continues the conversation using that result.
Setup Guide
Prerequisites
- 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.
Action type › Run client-side code. - 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.
Connect your website: the registration snippet.
Fill in the function with your own code. Register every client-side Action in one registerTools call:
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." };
},
});- 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 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.
Response Format
Your function must return one of two shapes:
Return status: "success" and data, which can be a string, an object or an array:
{
status: "success",
data: responseData
}// 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 }] }Return status: "error" and a non-empty error message:
{
status: "error",
error: "Descriptive error message"
}// 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:
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." };
}
},
});Best Practices
Error Handling
Catch errors and return a message the agent can pass on:
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." };
}
},
});Input Validation
Check inputs before you use them:
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 };
},
});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.
Troubleshooting
"Client tool is not registered"
- Check that the function name matches the Action's name exactly, including case.
- Make sure
registerToolsruns on every page where the Chat bubble appears. - Make sure your script runs after the embed code.
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.
The user details are empty
user_idis set only after the visitor is identified. See Identity verification.- Call
identifybefore the visitor reaches the Action.