# Code Editor > Write Widget code, define the schema and preview with examples in the code editor. The **Code** tab is where you write and edit a Widget by hand. It has two parts: the **main editor** at the top, where you write the Widget's layout, and the **footer panel** at the bottom, where you define the schema and manage example data. ![The Code tab of the Order status Widget: the Widget code editor at the top with the Card layout, the Schema tab in the footer panel with the Zod schema, and the preview of the order status card on the right.](annotated/01-code-tab.webp "The Code tab: main editor above, footer panel below.") ## Main Editor The main editor is where you write the Widget's layout with the built-in [components](/docs/developer-guides/widgets/components/layout). Its toolbar has: * **JSON**: switches between **Widget code** (the JSX layout) and **Widget JSON** (the same Widget as its rendered element tree). You can edit either view. * **Widget images**: upload images and insert their URLs. See [Widget images](#widget-images). * **Copy code**: copies the editor's contents to your clipboard. The editor highlights JSX syntax so the structure is easy to read. ![The main editor switched to Widget JSON, showing the Card element and its children as JSON, with the JSON button pressed.](annotated/02-json-view.webp "JSON shows the Widget as its element tree.") > [!TIP] > > The [component pages](/docs/developer-guides/widgets/components/layout) list every component with its props and defaults. ## Code Syntax The code editor uses a JSX-like syntax. These are the main patterns for working with data in a Widget. ### Variables Use a data field by putting its name in curly braces. Each variable is a field in the schema. ```jsx <Text value={estimatedDelivery} size="sm" color="secondary" /> ``` ### Nested Data Reach into nested objects with dot notation. ```jsx <Text value={customer.name} /> <Text value={customer.email} /> ``` ### Loops Repeat elements for each entry in an array with `.map()`. ```jsx {items.map((item) => ( <Row key={item.id} gap={3}> <Image src={item.image} size={40} /> <Text value={item.name} size="sm" /> </Row> ))} ``` ### String Interpolation Mix fixed text with data fields using backticks and `${}`. ```jsx <Text value={`Qty ${item.quantity}`} /> ``` ### Conditions and Expressions Show an element only when a value is set with `&&`, choose between two values with a ternary, and compare or calculate with the usual operators. Method calls other than `.map()` aren't supported, so format values such as prices in your data. ```jsx {trackingUrl && <Badge label="Tracking available" color="info" />} <Icon name={step.done ? "circle-check" : "circle"} color={step.done ? "success" : "tertiary"} /> <Text value={`$${(price * quantity)}`} /> ``` ## Schema The **Schema** tab in the footer panel defines the data fields the AI agent provides when it shows the Widget. Write it in **Zod**, or switch to **JSON** to write it as a JSON Schema. Use the **Zod** / **JSON** switch on the right of the footer panel to change format. ![The Schema tab with JSON selected, showing the JSON Schema with orderNumber, status and trackingUrl properties.](annotated/03-schema-json.webp "The schema as a JSON Schema.") > [!NOTE] > > The schema tells the AI agent what data to pass into the Widget. Each field in the schema is a variable you can use in the code. ## Default Example The **Default** tab holds the starting values for every schema field. The preview uses them when the Widget first shows, and visitors see them for any field no other source provides. ```json { "orderNumber": "QW-10482", "status": "Shipped", "estimatedDelivery": "Mar 9, 2026" } ``` ![The Default tab of the footer panel with the Order status Widget's default values: order number QW-10482, status Shipped, a tracking URL, the estimated delivery and the timeline steps.](annotated/04-default.webp "Default holds the starting values.") > [!TIP] > > On a Widget attached to a **Call API + show widget** action, the footer offers **Use test response from** followed by the action's name. It replaces Default with the data the action returned in its last successful test. ## Named Examples Named examples let you preview other sets of data without changing the default values. Use them to see how the Widget looks with, say, 1 item against 5, or an order that's still processing against one that's been delivered. **1. Add an example** Click **Add example** (**+**) in the footer panel. The new tab, **Example 1**, starts as a copy of Default. **2. Edit its values** Change the values in the example's tab. Each example keeps its own data. **3. Switch between examples** Click an example's tab to update the preview with its data. Click the **×** on a tab to close that example. ![The footer panel with a new Example 1 tab next to Schema and Default, holding a copy of the default values.](annotated/05-add-example.webp "A new example starts as a copy of Default.") > [!NOTE] > > Named examples are only for previewing in the builder. In conversations, the data comes from the Action. See [Where Widget Data Comes From](/docs/developer-guides/widgets/overview#where-widget-data-comes-from). ## Widget Images Click **Widget images** in the toolbar to manage the images hosted for this Widget. Upload a PNG, JPEG, WebP or GIF of up to 5 MB, up to 5 images per Widget. Each image has **Copy URL**, **Insert**, which puts its URL into the code, and **Delete image**. ![The Widget images dialog with its Upload image button, the note PNG, JPEG, WebP or GIF up to 5 MB and 0/5 used, and No images yet.](annotated/06-images.webp "Upload, copy and insert hosted images.") ## Next steps > [card] Functions | /docs/developer-guides/widgets/functions | Decide what each button and form does. > [card] States | /docs/developer-guides/widgets/states | Show or hide parts of the Widget based on its data. > [card] Layout components | /docs/developer-guides/widgets/components/layout | Every component you can use in the code.