Overview
createNavaiMobileBackendClient creates a client for communicating with your backend API. It handles client secret generation, function listing, and function execution.
Import
Usage
Type Signature
Parameters
Configuration options for the backend client
Return Value
createClientSecret
(input?: CreateRealtimeClientSecretInput) => Promise<CreateRealtimeClientSecretResult>
Create a client secret for OpenAI Realtime API connection.
List available backend functions.
Execute a backend function.Returns the function result (type depends on the function).Throws if function execution fails or returns an error.
API Endpoints
The backend client expects these endpoints:POST /navai/realtime/client-secret
Create ephemeral client secret for OpenAI Realtime API. Request:GET /navai/functions
List available backend functions. Response:POST /navai/functions/execute
Execute a backend function. Request:Examples
Basic Client
With Environment Variables
Custom Endpoints
Error Handling
Function Discovery
Custom Fetch (for testing)
Error Handling
The client throws errors in these cases:| Method | Error Condition | Error Message |
|---|---|---|
createClientSecret | HTTP error | Response text or HTTP {status} |
createClientSecret | Invalid response | "Invalid client-secret response." |
listFunctions | HTTP error | Returns empty functions with warning |
listFunctions | Parse error | Returns empty functions with warning |
executeFunction | HTTP error | Response text or HTTP {status} |
executeFunction | Invalid response | "Invalid backend function response." |
executeFunction | Function error | payload.details or payload.error |
Implementation Notes
URL Construction
The client constructs URLs by:- Removing trailing slashes from base URL
- Ensuring path starts with
/ - Joining:
{baseUrl}{path}
Response Parsing
createClientSecret: Expects{ value: string, expires_at?: number }listFunctions: Expects{ items: [], warnings: [] }, gracefully handles errorsexecuteFunction: Expects{ ok: true, result: any }or{ ok: false, error: string }
Fetch Fallback
IffetchImpl is not provided, uses global fetch. This allows for:
- Custom fetch implementations
- Testing with mock fetch
- Network layer customization
Best Practices
- URL Configuration: Use environment variables for API URL
- Error Handling: Always wrap API calls in try-catch
- Secret Expiration: Monitor
expires_atand refresh before expiry - Function Discovery: Cache function list to reduce API calls
- Timeout Handling: Configure reasonable timeouts for fetch
See Also
- Agent Runtime API - Using backend functions in agent
- Types Reference - TypeScript types
- useMobileVoiceAgent Hook - Integration hook