How to add an AI chatbot to a React app

Install @feedbotai/react, render <FeedbotWidget botId="..." /> once at the root of your app, and an AI chatbot trained on your docs appears on every page. If you'd rather not add a dependency, paste one script tag into index.html instead.

  • Setup: 5 minutes
  • Free plan, no card
  • Updated

People who search for “react chatbot” usually want one of two things: a component library to build their own chat UI, or a working AI assistant they can drop into an existing app. This page covers the second. If you want the first, skip to build or embed.

What you need

  • A Feedbot bot (the Free plan works) and its bot id from Dashboard → Bot → Install.
  • A React app: Vite, Create React App, Remix, Gatsby or anything else that renders React in the browser. For Next.js, see the Next.js guide.

Option 1: the @feedbotai/react package

  1. Install the package:

    Terminal window
    npm install @feedbotai/react
    # or: pnpm add @feedbotai/react
  2. Add your bot id to an environment variable. In Vite that’s .env:

    Terminal window
    VITE_FEEDBOT_BOT_ID=YOUR_BOT_ID

    In Create React App use REACT_APP_FEEDBOT_BOT_ID and process.env.REACT_APP_FEEDBOT_BOT_ID. The bot id isn’t a secret, so hardcoding it is fine too.

  3. Render the widget once, in the root component, next to your routes:

    src/App.tsx
    import { FeedbotWidget } from "@feedbotai/react";
    export default function App() {
    return (
    <>
    <AppRoutes />
    <FeedbotWidget botId={import.meta.env.VITE_FEEDBOT_BOT_ID} />
    </>
    );
    }
  4. Run the app. A chat button appears in the bottom-right corner. Ask it something to confirm it answers.

The component renders nothing into your tree. It injects https://w.feedbotai.com/v1/feedbot.js once and keeps identity, metadata and open state in sync with your props. Other props: position, primaryColor, mode and container (inline chat), open (controlled), and the callbacks onReady, onOpen, onClose, onMessage. position, primaryColor, mode and container are read once, when the script loads.

Option 2: one script tag in index.html

No package, no build step. Paste this before </body> in index.html (Vite) or public/index.html (CRA):

<script src="https://w.feedbotai.com/v1/feedbot.js" data-bot-id="YOUR_BOT_ID" async></script>

You can still use the useFeedbot() hook with the script tag: it works with or without a mounted <FeedbotWidget>.

Open the chat from a button

import { useFeedbot } from "@feedbotai/react";
export function PricingHelp() {
const { open, sendMessage } = useFeedbot();
return (
<>
<button onClick={open}>Chat with us</button>
<button onClick={() => sendMessage("Which plan fits a team of 5?")}>Ask about plans</button>
</>
);
}

The hook returns open, close, toggle, sendMessage, identify, setMetadata, reset and registerAction. Calls made before the script has loaded are queued, so you don’t need to wait for a ready event. The full command list is in the JavaScript API docs.

Identify logged-in users

Pass the signed-in user and a hash computed on your server:

<FeedbotWidget
botId={import.meta.env.VITE_FEEDBOT_BOT_ID}
user={user ? { id: user.id, email: user.email, name: user.name } : null}
userHash={feedbotHash} // from your API, never computed in the browser
metadata={user ? { plan: user.plan } : undefined}
/>

The hash is HMAC-SHA256(identitySecret, userId) as hex. In a Vite or CRA app there’s no server in the bundle, so return it from your backend, for example from the /me endpoint you already call after login. Setting user back to null on logout calls reset, and the next person on that device starts a fresh conversation. Verified users see their chat history on any device. Code for Node, Python and PHP is in Identify users.

Show the chat on some routes only

Render <FeedbotWidget> inside the layout for the routes that need it, for example your dashboard layout but not the marketing pages. To hide it after it has loaded, there is no hide command; the cleanest approach is to mount the widget only in the layouts where you want it from the start.

Should you build a React chatbot or embed one?

We make Feedbot, so weigh this with that in mind. Building makes sense when the chat is your product: a custom AI app, a chat with your own model, or a UI that has to match a design system pixel for pixel. Good starting points:

Tool What it gives you You still build
Vercel AI SDK (useChat) Streaming hooks for React, model provider adapters UI, retrieval over your docs, storage, handoff
React ChatBotify A configurable chat component with flows AI backend, knowledge base
chatscope chat UI kit Message list, input, typing indicator components Everything behind the UI

Embedding makes sense when you want a support or sales assistant on an existing app this week. Feedbot gives you the UI, answers from your knowledge base, lead capture, human handoff to Telegram, Slack, Discord or email, and an inbox. The trade-off is less control over the chat’s look beyond the dashboard settings.

Troubleshooting

Symptom Cause Fix
No chat button Bot id missing or env variable undefined Log import.meta.env.VITE_FEEDBOT_BOT_ID. Vite only exposes variables that start with VITE_. Restart the dev server after editing .env.
Console: “data-bot-id is required” Empty botId prop Same as above.
Button shows but requests fail Domain not in Allowed domains Add localhost and your production domain in Bot → Install → Allowed domains, or leave the list empty while testing.
Props like position don’t change They’re read once, when the script loads Set them in the dashboard, or reload the page after changing them.
Blocked by Content Security Policy Strict CSP Allow https://w.feedbotai.com in script-src and https://api.feedbotai.com in connect-src.
User shows as unverified Hash made from a different value Hash exactly the id you pass in user, with the current identity secret.

Train it on your app’s docs

On the Free plan the bot learns from text and FAQ you paste in. From Starter you can import your docs site or help center by URL or sitemap, and upload PDF, DOCX or Markdown files. Single-page apps that render content with JavaScript are imported through a real browser. See knowledge base for limits per plan.

Next steps

Questions

Can't find an answer? Ask the bot in the corner, or write to us.

Is there an npm package for a React chatbot widget?

Yes. @feedbotai/react exports a FeedbotWidget component and a useFeedbot() hook. It injects the Feedbot loader once, renders nothing itself, and works with React 17 and later.

Do I need to build the chat UI myself?

No. The widget ships its own chat UI inside a Shadow DOM, so your CSS can't break it and its CSS can't leak into your app. You set colors, position and texts in the dashboard.

Does the chat survive route changes in a single-page app?

Yes. Mount FeedbotWidget once in the root component, outside your router outlet. The widget lives on the document, so React Router navigation doesn't reload it or lose the conversation.

How do I open the chatbot from my own button in React?

Call useFeedbot() in any component and use open(), toggle() or sendMessage(text). sendMessage opens the chat and sends the text as the visitor, which is handy for 'Ask about pricing' buttons.

Will the widget slow down my React app?

The loader is about 6 KB gzipped and loads the chat itself only when the visitor hovers or clicks the button, or when the browser is idle. It doesn't add to your bundle beyond the small wrapper package.

Can the bot know which user is logged in?

Yes. Pass user and userHash props. The hash is an HMAC-SHA256 of the user id made with your identity secret on your server, so nobody can impersonate another user from the browser.

Keep exploring

Put one agent on your site today

Start on the free plan. Upgrade when conversations grow.