Authentication
The Sunny Agents SDK supports two authentication modes: custom token exchange and passwordless authentication. With createSunnyChat, all authentication configuration is server-driven — you only specify the auth type and the server provides the necessary details automatically.
Authentication modes
Custom Token Exchange
For applications with custom authentication providers:
Features:
- Server-persisted conversations
- Access token authentication
- Automatic token refresh
- User-specific data
Passwordless Authentication
Email or SMS-based authentication without passwords. The SDK renders a verification UI directly in the chat:
Features:
- Email or SMS authentication
- No passwords required
- WebSocket-based (no page refreshes)
- Optional chat history migration
- Automatic token management
- In-chat verification UI
Authentication flow
When using createSunnyChat, auth configuration is fetched via HTTP from the /sdk/config endpoint before any WebSocket connection is established:
Step-by-step
Token Exchange:
- Create chat: Call
createSunnyChatwithauthType: "tokenExchange"— returns immediately - Fetch config: SDK calls
GET /sdk/configto retrieve token exchange settings (background) - Proactive connect: SDK connects WebSocket and establishes SDK session in the background
- Provide ID token: SDK calls your
idTokenProviderto get the ID token - Exchange token: SDK exchanges ID token for access token
- Upgrade auth: SDK sends
auth.upgrademessage with access token - Authenticated: SDK receives
auth.upgradedresponse — send button spinner becomes send arrow - First message: User sends a message — no waiting, auth is already complete
- Refresh: SDK automatically refreshes token when expired
Configuration options
Runtime auth type switching
You can switch the authentication type at runtime:
Authentication providers
Auth0 with React
Auth0 with Vanilla JavaScript
Firebase with React
Custom provider
Token refresh
The SDK automatically refreshes tokens before they expire using a check-on-send strategy: before every outgoing message, the SDK checks the access token’s JWT exp claim and, if the token is within 5 minutes of expiry, requests a fresh ID token from your idTokenProvider, exchanges it for a new access token, and sends an auth.refresh message to the server — all before the user’s message is sent. No timers or background polling are involved; refresh only happens when the user is actively sending messages.
No action is needed from your app. If your idTokenProvider needs to force-refresh, you can do so per provider:
Auth0:
Firebase:
Profile sync
You can pre-populate user data (profile, address, insurance, and dependents) during authentication by passing authUpgradeProfileSync to createSunnyChat. This data is sent alongside the auth.upgrade message, so the user doesn’t need to re-enter information your application already has.
See the Profile Sync guide for full details on all supported data types including insurance and dependents.
Best practices
- Secure storage: Store ID tokens securely (not in localStorage for sensitive apps)
- Error handling: Always handle authentication errors
- Token refresh: Let SDK handle token refresh automatically
Next steps
- Profile Sync - Pre-populate user data during authentication
- Key Concepts - Understand events, state management, and MCP approvals
- Artifacts - Parse and render rich content
- Overview - Understand SDK architecture