# Client-Side Custom Forms > Show your own forms inside the chat, defined in your page's JavaScript, and send the answers to your systems ## Overview A custom form Action shows a form inside the conversation when the agent needs structured details, such as a demo request or a return. You create the Action in the dashboard, and your website defines the form's fields in JavaScript. The visitor fills it in without leaving the chat, and QwryAI can send each submission to your webhook. > [!NOTE] > > Custom forms run where your website's code runs, such as the Chat bubble on your site. The fields come from your page, so a form only appears where your registration code is loaded. ### Key Benefits * **Instant validation:** visitors see what's missing before they submit. * **Smoother conversations:** the agent collects several details at once instead of one question at a time. * **Full control:** the fields, labels, options and messages are yours, and can change per visitor. ### 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). ## Setup Guide **1. Create your custom form Action** In your agent, open **Actions**, select **Create Action** and choose **Custom form**. ![The Create Action gallery with the Custom form card, Create a custom form to collect information from your users.](annotated/01-custom-form-card.webp "Create Action › Custom form.") Under **General**, enter an **Action name** and explain when the agent should show the form, with a few example questions. Then save. ![The Custom form editor's General step with the Action name field, the when-to-use box and the note This action will be executed on the client.](annotated/02-custom-form-general.webp "Name the form and say when to use it.") > [!WARNING] > > **Test on your own site.** The fields come from your website's code, so the form can't appear in the dashboard's Playground. Install the [Chat bubble](/docs/developer-guides/website-integration/chat-bubble) on a test page and try the Action there. **2. Register your form schema** On your website, call `registerFormSchema` with a function named exactly like the Action. The function returns the form: > [!TIP] > > Register your forms in code that loads on every page, such as your main layout. ```javascript window.qwryai("registerFormSchema", { learn_more_form: async (args, user) => { return { fields: [ { name: "name", label: "Full name", type: "text", placeholder: "Enter your full name", validation: { required: { value: true, message: "Name is required" } }, }, { name: "email", label: "Email address", type: "email", placeholder: "Enter your email", validation: { required: { value: true, message: "Email is required" } }, }, { name: "message", label: "Message", type: "textarea", placeholder: "How can we help you?", validation: { required: { value: true, message: "Please enter your message" } }, }, ], submitButtonText: "Send message", successMessage: "Thank you! We'll get back to you soon.", errorMessage: "Failed to send your message. Please try again.", }; }, }); ``` > [!WARNING] > > **One registration replaces the last.** Each `registerFormSchema` call replaces every form registered before it, so register all your forms in one call. ![On a demo page, the agent shows the learn_more_form form in the chat with Full name, Email address and Message fields, the visitor fills it in and selects Send message, and the form shows Thank you! We'll get back to you soon.](walkthroughs/custom-form-submit.gif) > [!TIP] > > Your form is ready. The agent shows it whenever the Action fits the conversation. **3. Webhooks (optional)** To receive each submission on your own server, add a webhook under the Action's **Webhooks** step. See [Webhooks Integration](#webhooks-integration). **4. Choose channels** Under **Channels**, pick where the Action can run. The form can only appear where your website's code is loaded, such as the Chat bubble on your site. ![The Custom form editor's Channels step, listing the channels the Action can run on.](annotated/05-custom-form-channels.webp "Channels: where the form can appear.") ## Function Parameters Your form function receives the same two arguments as a [client-side custom Action](/docs/developer-guides/website-integration/client-side-custom-actions#function-parameters): **args** `object` *required* The inputs the agent collected for this Action, with the names you set under **Inputs**. Use them to fill in default values or decide which fields to show. ![The Custom form editor's Inputs step, where you add the inputs the agent collects before it shows the form.](annotated/03-custom-form-inputs.webp "Inputs: values the agent passes to your form.") **user** `object` Who the visitor is: `user_id`, `user_hash`, `anon_user_id` and `user_metadata`. `user_id` is set when the visitor is signed in with [identity verification](/docs/developer-guides/website-integration/identity-verification). > [!NOTE] > > `anon_user_id` is QwryAI's own ID for the visitor. You can ignore it. ## Complete Example A profile form that uses several field types and pre-fills what's already known: > [!WARNING] > > The key in `registerFormSchema` (here `update_profile`) must match the Action's name in the dashboard exactly. ```javascript window.qwryai("registerFormSchema", { update_profile: async (args, user) => ({ showLabels: true, submitButtonText: "Save profile", successMessage: "Your profile is updated.", errorMessage: "We couldn't save your profile. Please try again.", fields: [ { name: "fullName", label: "Full name", type: "text", defaultValue: user.user_metadata.name || "", validation: { required: { value: true, message: "Enter your name" }, minLength: { value: 2, message: "At least 2 characters" }, }, }, { name: "phone", label: "Mobile number", type: "phone", placeholder: "+15551234567", }, { name: "teamSize", label: "Team size", type: "number", validation: { min: { value: 1, message: "At least 1" }, max: { value: 5000, message: "At most 5,000" } }, }, { name: "plan", label: "Plan", type: "select", options: [ { value: "starter", label: "Starter" }, { value: "growth", label: "Growth" }, ], }, { name: "interests", label: "Interests", type: "groupmultiselect", options: { Support: [{ value: "helpdesk", label: "Helpdesk" }, { value: "voice", label: "Voice" }], Sales: [{ value: "outbound", label: "Outbound" }, { value: "leads", label: "Lead capture" }], }, }, { name: "avatar", label: "Profile photo", type: "image", }, { name: "accountId", label: "Account", type: "text", defaultValue: user.user_id, disabled: true, }, ], }), }); ``` ![The update_profile form inside the chat with Full name, Mobile number, Team size, Plan, Interests, Profile photo and Account fields and a Save profile button.](walkthroughs/custom-form-profile.gif) ## API Reference ### Form Schema | Property | Type | Default | Description | | - | - | - | - | | `fields` | array | required | The form's fields, in order. | | `submitButtonText` | string | `"Submit"` | The submit button's text. | | `showLabels` | boolean | `false` | Show each field's label above it. Without labels, the placeholder guides the visitor. | | `successMessage` | string | `"Form submitted successfully"` | Shown after a successful submission. | | `errorMessage` | string | `"Error submitting form"` | Shown when the submission fails. | ### Field Schema | Property | Type | Description | | - | - | - | | `name` | string | **Required.** The field's key in the submitted data. Must be unique in the form. | | `type` | string | **Required.** One of the [field types](#field-types). | | `label` | string | The field's label. Defaults to `name`. | | `placeholder` | string | Hint text inside the field. Defaults to the label. | | `defaultValue` | any | The field's starting value. | | `disabled` | boolean | Shows the field read-only. Disabled fields aren't validated. | | `validation` | object | Rules the visitor's answer must meet. See [Validation Rules](#validation-rules). | | `options` | array or object | The choices for selection fields. | ### Field Types > [accordion] Text Input Fields > > * `text`: a single line of text. > * `textarea`: several lines of text. > * `email`: an email address, checked for a valid format. > * `phone`: a phone number in international format, such as `+15551234567`. > [accordion] Numeric Fields > > * `number`: a number. Use `min` and `max` to limit it. > [accordion] Selection Fields > > * `select`: pick one option. > * `multiselect`: pick several options. > * `groupselect`: pick one option from named groups. > * `groupmultiselect`: pick several options from named groups. > > For `select` and `multiselect`, `options` is a list of `{ value, label }`. For the grouped types, `options` is an object whose keys are the group names and whose values are lists of `{ value, label }`. > [accordion] File Upload Fields > > * `image`: an image upload. JPEG, PNG, GIF and WebP files of up to 2 MB are accepted. The submitted data holds the uploaded image's address. ### Validation Rules Each rule is an object with a `value` and an optional `message` shown when the rule fails. > [accordion] Basic Validation > > ```javascript > validation: { > required: { value: true, message: "This field is required" }, > pattern: { value: "^[A-Z]{2}-\\d{5}$", message: "Use the format SL-20417" }, > } > ``` > [accordion] Length Validation > > ```javascript > validation: { > minLength: { value: 2, message: "At least 2 characters" }, > maxLength: { value: 80, message: "At most 80 characters" }, > } > ``` > [accordion] Numeric Validation > > ```javascript > validation: { > min: { value: 1, message: "At least 1" }, > max: { value: 100, message: "At most 100" }, > } > ``` > [accordion] Error Handling > > Set `defaultErrorMessage` for any rule without its own message, and for format checks on `email`, `phone`, `number` and `image` fields: > > ```javascript > validation: { > required: { value: true }, > defaultErrorMessage: "Please check this field", > } > ``` > > Without one, the form shows *Please enter a valid value*. ## Advanced Configuration ### Webhooks Integration **1. Open the Action's Webhooks step** Open your custom form Action and go to **Webhooks**: *Send form data to webhook (optional)*. ![The Custom form editor's Webhooks step with Send form data to webhook (optional), the Webhook URL field and the Create webhook button.](annotated/04-custom-form-webhooks.webp "Webhooks: send each submission to your server.") **2. Add your endpoint** Enter your endpoint's address under **Webhook URL** and select **Create webhook**. QwryAI creates a signing secret for it; copy it with **Copy secret** and keep it on your server. Each submission is sent as a `{action name}_custom_form.submit` event: ```json { "eventType": "learn_more_form_custom_form.submit", "chatbotId": "", "payload": { "conversationId": "fd5ae9e1-9b30-4503-b346-ccd4d7ef16a3", "data": { "name": "Maya Chen", "email": "maya@example.com", "message": "Can we get a demo for a team of 12?" }, "error": null } } ``` `error` holds a message when an image upload failed. To check the `x-qwryai-signature` header, see the [Webhook API Guide](/docs/developer-guides/webhooks). ## Troubleshooting > [accordion] The form doesn't appear > > * Check that the key in `registerFormSchema` matches the Action's name exactly. > * Make sure the registration code runs on the page where you're testing, after the embed code. > * Test on your website, not in the Playground. > * If the agent reaches the Action and nothing is registered, the browser console shows `Form [name] not found`. > [accordion] Validation doesn't work > > * Each rule needs a `value`, such as `required: { value: true }`. > * `pattern` takes a regular expression as a string; escape backslashes. > * Disabled fields are never validated. > [accordion] Options don't show > > * `select` and `multiselect` take a list of `{ value, label }`. > * The grouped types take an object of lists, keyed by group name. > [!TIP] > > Debug in your browser's console: call your form function yourself and check the object it returns. ## Next Steps > [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 | React to chat events as they happen. > [card] Webhook API Guide | /docs/developer-guides/webhooks | Verify and handle submissions on your server. > [card] Floating Initial Messages | /docs/developer-guides/website-integration/floating-initial-messages | Show the initial messages above the Chat bubble.