Introduction
The@navai/voice-backend package provides server-side functionality for Navai voice applications. It handles two critical responsibilities:
- Client Secret Management - Mint secure ephemeral
client_secrettokens for OpenAI Realtime API - Function Runtime - Discover, validate, expose, and execute backend tools from your codebase
Installation
express is required as a peer dependency.Architecture
The runtime architecture consists of three layers:1. Entry Layer (src/index.ts)
Exposes the public API including:
- Client secret helpers
- Express route registration
- Type exports
2. Discovery Layer (src/runtime.ts)
Handles function discovery:
- Resolves
NAVAI_FUNCTIONS_FOLDERSconfiguration - Scans files and applies path matching rules
- Builds module loaders for matched files
3. Execution Layer (src/functions.ts)
Manages function execution:
- Imports matched modules
- Transforms exports into normalized tool definitions
- Executes tools safely with payload validation
Request Flow
End-to-end request flow:- Frontend/mobile calls
POST /navai/realtime/client-secret - Backend validates options and API key policy
- Backend calls OpenAI
POST https://api.openai.com/v1/realtime/client_secrets - Frontend/mobile calls
GET /navai/functionsto discover allowed tools - Agent calls
POST /navai/functions/executewithfunction_nameandpayload - Backend executes only tool names loaded in the registry
Core APIs
Express Integration
registerNavaiExpressRoutes
Register all Navai routes with your Express app
Client Secret Management
Client Secret APIs
Create and manage OpenAI Realtime client secrets
Function Runtime
Function APIs
Load and execute dynamic backend functions
Type Definitions
TypeScript Types
Complete type reference for all APIs
Quick Start
Environment Variables
Key environment variables:OPENAI_API_KEY- Your OpenAI API keyOPENAI_REALTIME_MODEL- Default model (e.g.,gpt-realtime)OPENAI_REALTIME_VOICE- Default voice (e.g.,marin)OPENAI_REALTIME_INSTRUCTIONS- Default system instructionsOPENAI_REALTIME_LANGUAGE- Default response languageOPENAI_REALTIME_VOICE_ACCENT- Default voice accentOPENAI_REALTIME_VOICE_TONE- Default voice toneOPENAI_REALTIME_CLIENT_SECRET_TTL- TTL in seconds (10-7200)NAVAI_ALLOW_FRONTEND_API_KEY- Allow API keys from requestsNAVAI_FUNCTIONS_FOLDERS- Function discovery pathsNAVAI_FUNCTIONS_BASE_DIR- Base directory for scanning
Production Recommendations
Related Packages
- @navai/voice-frontend - Frontend voice integration
- @navai/voice-mobile - Mobile voice integration