When you need to hide your API key from client-side code or route requests through your backend, use the baseUrl option to point to your proxy server.

Why Use a Proxy?

We strongly recommend using a proxy server for production deployments. Key benefits:
  • API Key Protection: Keep your Sarvam API key secure on your server instead of exposing it in client-side code where it can be easily extracted
  • Access Control: Enforce your own authentication (e.g., only logged-in users can start conversations)
  • Rate Limiting: Implement custom rate limits per user to prevent abuse
  • Usage Tracking: Log and monitor API usage per user for billing or analytics
  • Request Validation: Validate and sanitize requests before forwarding to Sarvam

Use JWT-based authentication between your client and proxy—the same auth mechanism you use for the rest of your application. This ensures:
  • Only authenticated users can access the conversational AI
  • You can tie conversations to specific user accounts
  • No additional auth system to maintain

Server Setup (Python / FastAPI)


Client Setup

How you pass authentication credentials to your proxy depends on your platform and deployment scenario.

Web (Same Domain)

When your proxy server and frontend are on the same domain, cookies are sent automatically with requests:

Web (Cross-Domain) and React Native

For cross-domain requests, React Native, or any scenario where cookies aren’t automatically included, use customHeaders to pass authentication tokens:
The SDK includes these headers in all HTTP requests to the API (e.g., when fetching the signed WebSocket URL).

Flutter

Summary


Security Best Practices

Always Authenticate Users

Rate Limiting

Request Validation

Environment Variables

Store your API key securely: