๐๐๐ถ๐น๐ฑ๐ถ๐ป๐ด ๐ ๐ฆ๐ฒ๐ฟ๐๐ฒ๐ฟ๐น๐ฒ๐๐ ๐ฃ๐ฟ๐ผ๐ ๐ ๐ณ๐ผ๐ฟ ๐๐ ๐๐ฃ๐๐
I wanted to add a chatbot to my side project last month.
The goal was simple. Take user messages. Send them to an AI API. Stream the response back. Keep the API key safe.
I tried calling the API directly from the browser. This failed for two reasons.
- The API does not support CORS.
- Exposing your API key in the browser is a security risk.
I tried building an Express server next. It worked, but I did not want to manage a VPS. I did not want to worry about uptime or restarts for a small project.
I chose a serverless function instead. I used Vercel Edge Functions.
Why this works:
- No server to manage.
- No costs when no one uses it.
- You keep your API keys in environment variables.
The setup is simple. The function receives the message, calls the AI, and streams the result back to your frontend.
However, serverless is not perfect. I found four main issues:
- Cold starts. There is a small delay when the function starts.
- Timeouts. Most providers limit how long a function can run.
- Rate limiting. Users can spam your endpoint and increase your bill. Use Redis or Upstash to stop this.
- Error handling. You need to format errors so your users understand them.
My advice for the future:
- Use a queue for high traffic.
- Cache common prompts to save money.
- Use a dedicated worker if you need massive scale.
This approach is great for MVPs and small projects. Do not use it if you need sub-100ms response times or if you have millions of requests every day.
For most developers, a serverless proxy is enough to get started safely.
How do you handle AI API calls in your production apps?
Source: https://dev.to/__c1b9e06dc90a7e0a676b/building-a-serverless-proxy-for-ai-apis-lessons-learned-34lj