Skip to main content
Cedar’s voice system sends audio data to your backend for processing and expects either audio responses or structured JSON responses. This page covers how to implement the backend endpoint to handle voice interactions.

Endpoint Configuration

Automatic Configuration with Mastra

When using the Mastra provider, you can automatically configure the voice endpoint through the provider configuration:
This eliminates the need to manually call setVoiceEndpoint() and ensures consistency between your chat and voice endpoints.

Manual Configuration

For other providers or custom setups, configure the endpoint manually:

Request Format

The voice system sends a multipart/form-data POST request to your configured endpoint with the following fields:

Audio Data

  • Format: WebM container with Opus codec
  • Type: Binary blob from MediaRecorder API
  • Quality: Optimized for speech recognition

Voice Settings

Additional Context

The context includes Cedar’s additional context data (file contents, state information, etc.) that can be used to provide better responses.

Response Formats

Your backend can respond in several ways:

1. Direct Audio Response

Return audio data directly with the appropriate content type:

2. JSON Response with Audio URL

3. JSON Response with Base64 Audio

4. Structured Response with Actions

Implementation Examples

Node.js with Express

Python with FastAPI

Mastra Agent Integration

When using Cedar-OS with the Mastra provider and voiceRoute configuration, your Mastra backend should handle requests at the specified route. Here’s how to implement the voice handler:

Error Handling

Your backend should handle various error cases:

CORS Configuration

Ensure your backend allows CORS for the frontend domain:

Performance Considerations

Audio Processing

  • Use streaming transcription for real-time responses
  • Implement audio compression to reduce bandwidth
  • Cache TTS responses for common phrases

Response Optimization

  • Stream audio responses when possible
  • Use CDN for serving generated audio files
  • Implement request queuing for high-traffic scenarios

Security Best Practices

  1. Rate Limiting: Prevent abuse of voice endpoints
  2. Authentication: Verify user permissions
  3. Input Validation: Sanitize audio data and settings
  4. Content Filtering: Screen transcriptions for inappropriate content

Testing Your Integration

Test your voice endpoint with curl:
Or use the Cedar voice system directly for end-to-end testing: