Sign users in server-side so the widget loads pre-authenticated. The user never sees a login screen - the token is fetched behind the scenes before the widget renders.
| Attribute authentication | Token authentication | |
|---|---|---|
| User identity | Client-side via data-* attributes (email, userId, etc.) | Server-verified via API key |
| Backend required | No | Yes |
| Token flow | None - attributes passed directly | Your backend calls Returning.AI, returns a short-lived token |
| User experience | Seamless - user identified by attributes | Seamless - already signed in |
| Best for | Quick integrations, internal tools, controlled environments | Logged-in areas, client portals, trader dashboards |
Your server calls the Returning.AI sign-in API with your secret API key and the current user's email. It receives a short-lived token that the SDK will use to authenticate the widget session.
Keep your API key server-side
Never expose WIDGET_API_KEY in client-side code. It should only exist in environment variables on your backend.
app.post('/api/widget-auth', async (req, res) => {
const response = await fetch(
'https://prod-widgets.returning.ai/widget/{community_id}/signin',
{
method: 'POST',
headers: {
'returningai-api-key': process.env.WIDGET_API_KEY,
'email': req.user.email,
'Content-Type': 'application/json',
},
}
)
const data = await response.json()
res.json({ token: data.token })
})Add the auth-url attribute to your widget tag. The SDK will POST to that URL on load, receive the token, and authenticate automatically.
<rai-channel-widget
widget-id="YOUR_WIDGET_ID"
widget-url="YOUR_WIDGET_URL"
auth-url="/api/widget-auth"
></rai-channel-widget>How the flow works
auth-url attribute.Tokens are short-lived (typically around 5 minutes). The SDK handles renewal automatically - when a token is about to expire, it re-calls your auth-url endpoint to fetch a fresh one. No extra code is needed on your side.
If the refresh fails (e.g. the user's session has expired on your backend), the widget fires an rai-error event that you can listen for to prompt re-authentication.