Resources / Guides
Build your own CORS proxy (and why public ones are a bad idea)
Public CORS proxies leak your data and API keys and go down without warning. Here is how to build a small, safe CORS proxy with an origin and target allowlist.
A browser blocks your request to someone else's API because that API doesn't send CORS headers allowing your site. Servers don't have that restriction, so the standard fix is a CORS proxy: your page calls a server you control, the server calls the API and returns the answer with the right headers.
Why not a public CORS proxy?
Free proxies that wrap any URL are handy for a quick experiment, but don't ship them:
- They see everything. Every request and response — including tokens, API keys and user data — passes through someone else's server.
- They disappear. Rate limits, outages and shutdowns break your app without notice.
- They can change the response. You're trusting a stranger to return what the API sent.
A proxy of your own is a few dozen lines.
What a safe proxy does
- Allows only your origins. It answers with
Access-Control-Allow-Originset to your site, not*. - Allows only the APIs you need. It forwards to a fixed list of hosts. Otherwise you've built an open proxy anyone can use to hide their traffic behind your server.
- Handles preflight. It answers
OPTIONSrequests with the allowed methods and headers. - Adds secrets server-side. If the upstream API needs a key, the proxy adds it; the browser never sees it.
- Limits abuse. Rate-limit per client and cap response sizes.
Example (Node.js)
const ALLOWED_ORIGINS = ['https://myapp.com', 'http://localhost:5173'];
const ALLOWED_HOSTS = ['api.example.com', 'api.weather.example'];
export async function handler(req) {
const origin = req.headers.origin;
const cors = ALLOWED_ORIGINS.includes(origin)
? { 'Access-Control-Allow-Origin': origin, 'Access-Control-Allow-Methods': 'GET, POST', 'Access-Control-Allow-Headers': 'Content-Type', Vary: 'Origin' }
: {};
if (req.method === 'OPTIONS') return { status: 204, headers: cors };
if (!cors['Access-Control-Allow-Origin']) return { status: 403, body: { error: 'Origin not allowed' } };
let target;
try { target = new URL(req.query.url); } catch { return { status: 400, headers: cors, body: { error: 'Invalid url' } }; }
if (target.protocol !== 'https:' || !ALLOWED_HOSTS.includes(target.hostname)) {
return { status: 403, headers: cors, body: { error: 'Host not allowed' } };
}
const upstream = await fetch(target, {
method: req.method,
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${process.env.UPSTREAM_API_KEY}` },
body: req.method === 'POST' ? JSON.stringify(req.body) : undefined,
});
return { status: upstream.status, headers: { ...cors, 'Content-Type': upstream.headers.get('content-type') ?? 'application/json' }, body: await upstream.text() };
}
Even better than passing a full URL: expose named routes (/proxy/weather?city=…) that build the upstream URL themselves. Then the client can't reach anything you didn't plan for.
Common mistakes
- *`Access-Control-Allow-Origin: ` with credentials.** Browsers reject it; and if you echo back any origin instead, you've allowed every site.
- Forgetting `Vary: Origin` when the header depends on the origin, so a cache serves one site's headers to another.
- Forwarding the browser's cookies or auth headers to a third party.
- No timeout on the upstream call, so slow APIs tie up your proxy.
Why the browser enforces all this in the first place is covered in CORS errors explained.
On Wisp
Describe it — "a proxy endpoint that lets myapp.com call the weather API with my key" — and Wisp writes it with the origin and host allowlists, stores the key as a secret, and gives you an HTTPS URL to call from the browser, with every request in the log.
Try it: Template: OpenAI API key proxy
Build it with Wisp — describe an API, get it live.