Use a Proxy Server
For security reasons, you should never include your AI service API keys directly in client-side code. Instead, you should set up proxy services that securely forward requests to AI providers while keeping your API keys secure on the server side.
Each AI provider configuration requires a proxyUrl
parameter, which should point to your server-side endpoint that handles authentication and forwards requests to the AI service:
text2image: FalAiImage.RecraftV3({proxyUrl: 'https://your-server.com/api/falai'});
Proxy Implementation Requirements#
Your proxy should implement specific requirements for each AI service:
1. Anthropic Proxy#
- Target URL:
https://api.anthropic.com/
- Authentication Header: Add
X-Api-Key
header with your Anthropic API key - Request Handling: Forward request body as-is to Anthropic API
- Response Handling: Remove
content-encoding
headers to handle compressed responses correctly
2. fal.ai Proxy#
- Dynamic URL: Use a special header called
x-fal-target-url
to determine the actual endpoint - Authentication Header: Add
Authorization: Key YOUR_FAL_KEY
header - Request Forwarding: Preserve the complete request body and query parameters
- For more information on the requirements, refer to fal.ai's documentation.
3. ElevenLabs Proxy#
- Target URL:
https://api.elevenlabs.io/
- Authentication Header: Add
xi-api-key
header with your ElevenLabs API key - Response Handling: Remove
content-encoding
headers to handle compressed responses correctly
General Proxy Design#
A well-designed proxy service should:
- Route requests to the appropriate AI service based on the endpoint path
- Add authentication headers containing your API keys
- Forward the request body to maintain payload integrity
- Handle response streaming for services that support it (like Anthropic)
- Implement proper CORS headers to allow browser requests
- Add appropriate error handling and logging
- Consider rate limiting to protect your API keys from overuse
Security Considerations#
When implementing your proxy:
- Store API keys securely as environment variables
- Implement request validation to prevent abuse
- Consider adding user authentication to your proxy endpoints
- Monitor usage to detect unusual patterns
- Implement proper error handling without leaking sensitive information
This approach ensures your API keys remain secure while still allowing your application to utilize AI services. For a complete example of a proxy implementation, you can find various proxy templates online that can be adapted for your specific needs.