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:

A proxy of your own is a few dozen lines.

What a safe proxy does

  1. Allows only your origins. It answers with Access-Control-Allow-Origin set to your site, not *.
  2. 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.
  3. Handles preflight. It answers OPTIONS requests with the allowed methods and headers.
  4. Adds secrets server-side. If the upstream API needs a key, the proxy adds it; the browser never sees it.
  5. 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

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.

Related