2. Frontend: Connect using the SDK
Now, your frontend requests this token and uses the official Vapi Web SDK to start the actual audio stream.
Make sure you have installed the SDK: npm install @Vapi-ai/web
TypeScript
import Vapi from '@Vapi-ai/web';
const startInterview = async () => {
// 1. Ask Backend for Permission
const response = await fetch('/api/start-interview', {
method: 'POST',
body: JSON.stringify({ userId: '123', username: 'Jane' }),
headers: { 'Content-Type': 'application/json' }
});
if (!response.ok) {
alert("Please subscribe to access mock interviews.");
return;
}
const { token, assistantId, userData } = await response.json();
// 2. Initialize Vapi with the secure Token
const vapi = new Vapi(token);
// 3. Start the Call with Variable Injection
// We pass 'assistantOverrides' to inject the user data into the workflow
vapi.start(assistantId, {
variableValues: {
username: userData.username,
userid: userData.userid
}
});
// Optional: Listen for events
vapi.on('call-start', () => console.log('Interview started'));
vapi.on('call-end', () => console.log('Interview ended'));
};