Add TGLiveChat to your React app
One npm install. Drop the component anywhere in your tree. Works with React 16.8+, Next.js App Router, Vite, CRA, and React 19.
Option A — npm package
Install the package
Add your widget ID to the environment
Copy your widget ID from the dashboard and add it to your environment file. Use the prefix your framework requires:
.env.local — Next.js
.env — Vite
.env — Create React App
React / Vite / CRA — drop the component in your root
Import and render TGLiveChat in App.tsx. No props needed — it reads the env var automatically.
Next.js App Router — create a client wrapper
TGLiveChat uses browser APIs so it needs a "use client" boundary:
components/LiveChat.tsx
app/layout.tsx
Component props
widgetIdstring?Your widget ID. Optional if the env var is set.colorstringAccent hex color override (e.g. "#FF5500").privacyUrlstringLink to your privacy policy — shows a GDPR consent checkbox.autoOpenbooleanOpen the chat window automatically on page load.dir"ltr" | "rtl"Force text direction. Auto-detected by default.titlestringOverride the header title.subtitlestringOverride the header subtitle.agentPhotostringOverride the agent avatar URL.containerIdstringMount inside a specific element ID instead of body.serverUrlstringCustom backend URL for self-hosted deployments.Option B — script tag (no npm)
Next.js — via next/script
Use in your root app/layout.tsx. No client wrapper needed.
Vite / CRA — via index.html
Open index.html in the project root and paste before </body>:
Troubleshooting
Widget doesn't appear
Open DevTools Console — a missing ID logs an error telling you exactly which env var to set. Make sure your env var has the right prefix for your framework (NEXT_PUBLIC_, VITE_, or REACT_APP_) and that you restarted the dev server after adding it. If you have a Content Security Policy, add cdn.tglivechat.com to script-src and connect-src.
Next.js: "useState can only be used in a Client Component"
You're importing TGLiveChat directly in a Server Component. Create a wrapper file with "use client" at the top and import TGLiveChat there. Then import the wrapper in your layout.
Widget mounts twice in development
React StrictMode intentionally mounts components twice in development to surface side-effect bugs. The package handles this correctly — the second mount reuses the existing script and calls tglcBoot() directly. You'll see one widget, not two.
Messages not reaching Telegram
Open your dashboard and check that Telegram is connected (green badge). If you're routing to a group, make sure the bot is still an admin with Manage Topics permission.