Auth for Chat
Using Tokens
How to use authentication tokens
With the Widget
<script src="https://cdn.work1.ai/widget/v1/workone-widget.min.js"></script>
<script>
async function initChat() {
const response = await fetch('/api/chat/token')
const { token } = await response.json()
new WorkOneWidget({
token: token,
position: 'bottom-right',
theme: 'light'
})
}
initChat()
</script>With the React SDK
import { useState, useEffect } from 'react'
import { WorkOneProvider, ChatWidget } from '@workone/react-sdk'
function App() {
const [token, setToken] = useState(null)
useEffect(() => {
async function fetchToken() {
const response = await fetch('/api/chat/token')
const data = await response.json()
setToken(data.token)
}
fetchToken()
}, [])
if (!token) return <div>Loading...</div>
return (
<WorkOneProvider token={token}>
<ChatWidget />
</WorkOneProvider>
)
}With WebSocket (Direct API)
import io from 'socket.io-client'
const socket = io('https://api.work1.ai', {
auth: {
token: 'your-jwt-token-here'
}
})
socket.on('connect', () => {
console.log('Connected to WorkOne')
socket.emit('chat:join', {
agentId: 'your-agent-id',
token: 'your-jwt-token-here'
})
})
socket.on('chat:message', {
agentId: 'your-agent-id',
message: 'Hello!',
conversationId: 'conv_123'
})
socket.on('chat:response', response => {
console.log('Agent response:', response.content)
})