Learn about SvaraAI’s Express.js backend, API routes, and AI service integration
The SvaraAI backend is a TypeScript-based Express.js server that orchestrates AI services and manages conversation data. It serves as the bridge between the frontend and external AI APIs.
The main server configuration is defined in server.ts:
import express from 'express';import dotenv from 'dotenv';import cors from 'cors';import path from 'path';import entriesRouter from './routes/entries';import geminiRouter from './routes/gemini';import humeRouter from './routes/hume';dotenv.config({ path: path.resolve(__dirname, '.env') });const app = express();const PORT = process.env.PORT || 5000;// Middlewareapp.use(cors());app.use(express.json());// Routesapp.use('/api', entriesRouter);app.use('/api/gemini', geminiRouter);app.use('/api/hume', humeRouter);app.listen(PORT, () => { console.log(`Server running on PORT ${PORT}`);}).on('error', (err: Error) => { console.error('Server failed to start:', err); process.exit(1);});
The backend runs on port 5000 by default. You can customize this by setting the PORT environment variable.
{ "transcript": "User: I had a great day\nAssistant: That's wonderful!\n...", "emoData": { "joy": 0.85, "contentment": 0.72, "excitement": 0.68, // ... other emotions }}
Gemini configuration
The Gemini API is configured for consistent, concise analysis:
Model: gemini-2.0-flash (fast, efficient)
Temperature: 0.3 (focused, less creative)
Top-K: 20 (limits token sampling)
Top-P: 0.8 (nucleus sampling)
Max tokens: 100 (short, concise insights)
Prompt engineering
The prompt is defined in the GEMINI_PROMPT environment variable and uses placeholders:
Analyze this conversation transcript and emotional data:Transcript:{{transcript}}Top Emotions:{{emoData}}Provide a brief emotional analysis in 2-3 sentences.
The backend replaces {{transcript}} and {{emoData}} before sending to Gemini.
Response format
{ "response": "The conversation shows predominantly positive emotions with high joy and contentment. The user appears to be in a good mood and engaged positively throughout.", "emotions": { "joy": 0.85, "contentment": 0.72, // ... all emotions }}
The Gemini API key must be set in the .env file as GEMINI_API_KEY. Without it, all requests to this endpoint will return a 500 error.
This endpoint is designed for batch processing of pre-recorded audio files. The frontend currently uses Hume’s real-time Voice SDK instead, but this endpoint can be useful for analyzing uploaded recordings.
try { // ... route logic} catch (error) { console.error('[Route Error]', error); res.status(500).json({ error: 'Unable to process your request at this time' });}
Error messages sent to the client are generic to avoid leaking sensitive implementation details. Detailed errors are logged to the console for debugging.
PORT=5000GEMINI_API_KEY=your_gemini_api_key_hereGEMINI_PROMPT="Analyze this conversation...{{transcript}}...{{emoData}}"VITE_HUME_API_KEY=your_hume_api_keyHUME_SECRET_KEY=your_hume_secret_key