๐—•๐˜‚๐—ถ๐—น๐—ฑ๐—ถ๐—ป๐—ด ๐—ฎ ๐—ฆ๐—ฒ๐—ฟ๐˜ƒ๐—ฒ๐—ฟ๐—น๐—ฒ๐˜€๐˜€ ๐—ฃ๐—ฟ๐—ผ๐˜…๐˜† ๐—ณ๐—ผ๐—ฟ ๐—”๐—œ ๐—”๐—ฃ๐—œ๐˜€

I wanted to add an AI chatbot to my side project.

The goal seemed simple. Take user messages. Send them to an LLM API. Stream the response back to the frontend. Keep my API key safe.

I ran into problems immediately.

First, I tried calling the API directly from the browser. This failed because of CORS errors. It also exposed my API key to anyone visiting the site.

Next, I built an Express server. It worked, but I did not want to manage a VPS. I did not want to worry about uptime or crashes for a small project.

The best solution was a serverless function.

I used Vercel Edge Functions to act as a lightweight proxy. This approach keeps the API key secure in environment variables. It has no idle costs and requires no server management.

How the setup works:

This setup works well for MVPs and small projects. However, you should watch out for these four issues:

If you build a large production app, consider these upgrades:

Use a serverless proxy if you need to prototype quickly or run a small app. Avoid it if you need sub-100ms response times or handle millions of daily requests.

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