Browse docs
Use the widget with React and Next.js
Add the Axoryn widget to a React or Next.js app with the script tag, then identify signed-in users and reset on logout from your components.
This page is for developers adding Axoryn to a React single-page app or a Next.js site. There is no npm package to install: you load the same script tag as every other site and drive it through window.Axoryn from your components.
Add the script
React (Vite, Create React App and similar)
Paste the snippet right before </body> in index.html (or public/index.html):
<script src="https://api.axoryn.io/widget.js"
data-project-key="YOUR_WIDGET_KEY" async></script>
Next.js
Add the Script tag inside <body> in app/layout.tsx (or pages/_document.tsx):
import Script from 'next/script'
export default function RootLayout({ children }) {
return (
<html>
<body>
{children}
<Script src="https://api.axoryn.io/widget.js"
data-project-key="YOUR_WIDGET_KEY"
strategy="afterInteractive" />
</body>
</html>
)
}
Copy the snippet with your key from the project home (Put the widget on your site) or from the Install section of Account settings.
Route changes
The widget lives outside your React tree, so client-side navigation does not unmount or reload it. An open conversation stays open while the visitor moves between pages. Proactive messages that target a URL are checked again on every pushState, replaceState, popstate and hashchange, so URL rules work in a single-page app without extra code.
Wait for window.Axoryn
The script loads asynchronously, so window.Axoryn may not exist yet when your first effect runs. Use a small helper that waits for it:
// axoryn.ts
type AxorynApi = NonNullable<Window['Axoryn']>
export function whenAxoryn(timeoutMs = 10000): Promise<AxorynApi> {
return new Promise((resolve, reject) => {
const started = Date.now()
const check = () => {
if (window.Axoryn) return resolve(window.Axoryn)
if (Date.now() - started > timeoutMs) return reject(new Error('Axoryn widget did not load'))
setTimeout(check, 100)
}
check()
})
}
For TypeScript, add the Window declaration from the Widget JavaScript API reference.
Identify the signed-in user
Call identify() when a user signs in and reset() when they sign out. A hook keyed on the user id does both:
import { useEffect, useRef } from 'react'
import { whenAxoryn } from './axoryn'
type SupportUser = { id: string; email: string; name: string; axorynHash: string }
export function useAxorynIdentity(user: SupportUser | null) {
const hadUser = useRef(false)
useEffect(() => {
let cancelled = false
whenAxoryn()
.then((Axoryn) => {
if (cancelled) return
if (user) {
Axoryn.identify({
user_id: user.id,
email: user.email,
name: user.name,
user_hash: user.axorynHash, // computed on your server
})
hadUser.current = true
} else if (hadUser.current) {
// Signed out: the next person on this browser starts fresh.
Axoryn.reset()
hadUser.current = false
}
})
.catch(() => {})
return () => {
cancelled = true
}
}, [user?.id])
}
Use it once near the root of your app, for example in the component that holds your auth state:
function App() {
const user = useCurrentUser() // your auth hook
useAxorynIdentity(user)
return <Routes>{/* ... */}</Routes>
}
Compute user_hash on your server and send it to the browser with the user’s profile. Never put the project’s secret key in your React bundle. Server code for Node.js, Ruby, Python and PHP is in Identify your users.
Tip: If
identify()is called with a differentuser_idthan the last user on this browser, the widget resets on its own first. Still callreset()on logout, so an anonymous visitor after logout never sees the previous user’s chat.
Open the chat from a button
Open the panel from your own UI with open(), or send a question straight away with ask():
import { whenAxoryn } from './axoryn'
export function HelpButton() {
return (
<button type="button" onClick={() => whenAxoryn().then((I) => I.open())}>
Chat with us
</button>
)
}
export function AskAboutBilling() {
return (
<button type="button" onClick={() => whenAxoryn().then((I) => I.ask('I have a question about my invoice'))}>
Ask about billing
</button>
)
}
open('text') fills the message box without sending. ask('text') sends it as the visitor’s first message in a new conversation.
Start the widget only where you need it
To show the widget only in part of your app, for example only after sign-in, add data-auto-init="false" to the script tag, then start and remove it from a component:
import { useEffect } from 'react'
import { whenAxoryn } from './axoryn'
export function SupportWidget({ projectKey }: { projectKey: string }) {
useEffect(() => {
let cancelled = false
whenAxoryn().then((Axoryn) => {
if (!cancelled) void Axoryn.init({ projectKey })
})
return () => {
cancelled = true
window.Axoryn?.destroy()
}
}, [projectKey])
return null
}
destroy() removes the launcher and panel. Mounting the component again calls init() and brings it back. In React development mode with Strict Mode, effects run twice; the second init() starts the widget again after the first cleanup, so this is safe.
Language
To match your app’s language setting, pass locale to init(), set data-locale on the script tag, or call setLocale() when the user changes language:
whenAxoryn().then((Axoryn) => Axoryn.setLocale('pt-BR'))
The widget supports en, pt-BR and es.