Widget Integration

The easiest way to add AI-powered UI generation to your app. Just drop in the widget component and you're ready to go.

Basic Usage

Add the N4iWidget component to your app with just an API key and endpoint.

TypeScript/TSX
import { N4iWidget } from "n4i-genui/react";

function App() {
  return (
    <N4iWidget
      apiKey={process.env.NEXT_PUBLIC_N4I_API_KEY}
      apiUrl="/api/n4i"
      theme="light"
      showInput={true}
      showSuggestions={true}
      placeholder="Describe the UI you want to create..."
    />
  );
}

Live Demo

Try out different widget configurations below.

Preview

Enter a prompt to generate UI

Pre-loaded UI

Display pre-generated UI without making API calls. Perfect for static content or cached responses.

TypeScript/TSX
import { N4iWidget } from "n4i-genui/react";

// Pre-generated or cached UI tree
const savedUITree = {
  id: "page-1",
  type: "Page",
  props: { title: "Dashboard" },
  children: [
    {
      id: "card-1",
      type: "Card",
      props: { title: "Welcome" },
      children: [
        { id: "text-1", type: "Text", props: { content: "Hello!" } }
      ]
    }
  ]
};

function App() {
  return (
    <N4iWidget
      ui={savedUITree}
      theme="light"
      showInput={false}  // Hide input when showing pre-loaded UI
      onAction={(actionId, nodeId) => {
        console.log("Button clicked:", actionId, nodeId);
      }}
    />
  );
}

Auto Generate on Mount

Automatically generate UI when the component mounts using initialPrompt.

TypeScript/TSX
import { N4iWidget } from "n4i-genui/react";

function Dashboard() {
  return (
    <N4iWidget
      apiKey={process.env.NEXT_PUBLIC_N4I_API_KEY}
      apiUrl="/api/n4i"
      initialPrompt="Create a sales dashboard with revenue metrics, 
        a line chart showing monthly trends, and a table of recent orders"
      showInput={false}   // Hide input after generation
      theme="dark"
      loadingComponent={<MyCustomLoader />}
      errorComponent={<MyCustomError />}
      onUIGenerated={(ui) => {
        console.log("UI generated:", ui);
        // Save to cache, analytics, etc.
      }}
    />
  );
}

All Widget Props

Complete reference for N4iWidget configuration options.

PropTypeDefaultDescription
apiKeystring-Your N4i API key
apiUrlstring"/api/v1/embed"API endpoint URL
defaultModelstring"n4i-default"AI model to use
uiUiNode-Pre-loaded UI tree
initialPromptstring-Auto-generate on mount
showInputbooleantrueShow prompt input
showSuggestionsbooleantrueShow suggestion buttons
placeholderstring"Describe..."Input placeholder
themestring | N4iTheme"light"Theme preset or custom
classNamestring""Additional CSS classes
styleCSSProperties-Inline styles
onActionfunction-Button click handler
onUIGeneratedfunction-UI generation callback
onErrorfunction-Error callback
loadingComponentReactNode-Custom loading UI
errorComponentReactNode-Custom error UI

Handling User Actions

Respond to button clicks and other interactive elements in generated UI.

TypeScript/TSX
import { N4iWidget } from "n4i-genui/react";
import { useRouter } from "next/navigation";

function App() {
  const router = useRouter();

  const handleAction = (actionId: string, nodeId: string) => {
    console.log("Action:", actionId, "from node:", nodeId);
    
    // Handle different actions
    switch (actionId) {
      case "submit-form":
        handleFormSubmit();
        break;
      case "navigate-dashboard":
        router.push("/dashboard");
        break;
      case "open-modal":
        setShowModal(true);
        break;
      default:
        console.log("Unknown action:", actionId);
    }
  };

  return (
    <N4iWidget
      apiKey={process.env.NEXT_PUBLIC_N4I_API_KEY}
      apiUrl="/api/n4i"
      onAction={handleAction}
    />
  );
}

Next Steps