Overview
Client secret APIs handle the creation of secure, ephemeral tokens for authenticating with the OpenAI Realtime API. These tokens have configurable TTL and support session configuration.createRealtimeClientSecret
Function Signature
packages/voice-backend/src/index.ts:160
Description
Creates an ephemeral client secret by calling OpenAI’s Realtime API. Validates options, resolves API key based on configuration policy, builds session instructions with language/accent/tone preferences, and returns the client secret with expiration.Parameters
Backend configuration options
Per-request overrides for session configuration
Return Value
API Key Resolution
API key is resolved with strict priority:- Backend key wins: If
opts.openaiApiKeyis set, it is always used - Request key fallback: If backend key is missing,
req.apiKeyis used only ifopts.allowApiKeyFromRequestistrue - Error: If no key is available, throws an error
Validation Rules
Session Instructions
Final instructions are built by combining:- Base instructions from
req.instructionsoropts.defaultInstructions - Optional language directive:
"Always reply in {language}." - Optional accent directive:
"Use a {voiceAccent} accent while speaking." - Optional tone directive:
"Use a {voiceTone} tone while speaking."
Usage Example
createExpressClientSecretHandler
Function Signature
packages/voice-backend/src/index.ts:207
Description
Creates an Express middleware handler for the client secret endpoint. Validates options on creation, then handles incoming requests by callingcreateRealtimeClientSecret with the request body.
Parameters
Backend configuration options (same as
createRealtimeClientSecret)Return Value
Express middleware handler that:
- Reads
CreateClientSecretRequestfromreq.body - Calls
createRealtimeClientSecret(opts, req.body) - Responds with
{ value, expires_at } - Passes errors to
next(error)
Usage Example
getNavaiVoiceBackendOptionsFromEnv
Function Signature
packages/voice-backend/src/index.ts:114
Description
Loads backend options from environment variables with smart defaults for API key policy.Parameters
Environment variables object
Return Value
Backend options populated from environment variables
Environment Variables
| Variable | Option | Default |
|---|---|---|
OPENAI_API_KEY | openaiApiKey | - |
OPENAI_REALTIME_MODEL | defaultModel | - |
OPENAI_REALTIME_VOICE | defaultVoice | - |
OPENAI_REALTIME_INSTRUCTIONS | defaultInstructions | - |
OPENAI_REALTIME_LANGUAGE | defaultLanguage | - |
OPENAI_REALTIME_VOICE_ACCENT | defaultVoiceAccent | - |
OPENAI_REALTIME_VOICE_TONE | defaultVoiceTone | - |
OPENAI_REALTIME_CLIENT_SECRET_TTL | clientSecretTtlSeconds | 600 |
NAVAI_ALLOW_FRONTEND_API_KEY | allowApiKeyFromRequest | See below |
API Key Policy Logic
Usage Example
Common Errors
Missing API Key
openaiApiKey nor allowApiKeyFromRequest is configured.
Solution: Set OPENAI_API_KEY or enable allowApiKeyFromRequest.
Request API Key Disabled
apiKey in request body but allowApiKeyFromRequest is false.
Solution: Set NAVAI_ALLOW_FRONTEND_API_KEY=true or configure backend API key.
Invalid TTL
clientSecretTtlSeconds between 10 and 7200 seconds.
OpenAI API Failure
Related APIs
- registerNavaiExpressRoutes - Register all routes including client secret endpoint
- NavaiVoiceBackendOptions type - Full type definition
- CreateClientSecretRequest type - Request body type