← Blog

Claude API CORS Issue: Frontend Fix Guide

2026-11-09 · 5 min read · SubToAPI Team

If you've tried calling https://api.anthropic.com/v1/messages directly from a browser-based app, you've almost certainly hit this error in your console:

Access to fetch at 'https://api.anthropic.com/v1/messages' from origin 'http://localhost:3000'
has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present
on the requested resource.

This isn't a bug in your code. The Anthropic API is not designed to be called from client-side JavaScript, and it doesn't send the CORS headers browsers require for cross-origin requests. The fix isn't to find a magic header or workaround — it's to stop calling the API directly from the frontend and route the request through a backend layer instead. Below are the three practical ways to do that, from "quick local fix" to "production-ready."

Why the CORS error happens in the first place

CORS (Cross-Origin Resource Sharing) is a browser security mechanism, not a server limitation. When your frontend JavaScript makes a request to a different origin (a different domain, port, or protocol), the browser first checks whether the target server explicitly allows that origin via response headers like Access-Control-Allow-Origin.

Anthropic's API doesn't return these headers because:

So the "fix" isn't a CORS workaround at all — it's architectural: never let the browser hold or send your Claude API key.

Fix 1: Add a minimal backend proxy

The simplest production fix is a small server route that receives the request from your frontend, attaches the API key server-side, and forwards it to Claude.

// server.js (Node/Express)
import express from "express";
const app = express();
app.use(express.json());

app.post("/api/chat", async (req, res) => {
  const response = await fetch("https://api.anthropic.com/v1/messages", {
    method: "POST",
    headers: {
      "x-api-key": process.env.ANTHROPIC_API_KEY,
      "anthropic-version": "2023-06-01",
      "content-type": "application/json",
    },
    body: JSON.stringify(req.body),
  });
  const data = await response.json();
  res.json(data);
});

app.listen(3001);

Your frontend then calls /api/chat on your own origin — no cross-origin request, no CORS issue, and the key never reaches the browser.

const res = await fetch("/api/chat", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    model: "claude-sonnet-4-5",
    max_tokens: 1024,
    messages: [{ role: "user", content: "Hello" }],
  }),
});

This works well for a single app, but you now own key rotation, rate limiting, logging, retries, and streaming handling yourself.

Fix 2: Use a serverless function instead of a full backend

If you don't want to run a persistent server, the same logic works as a Vercel/Netlify/Cloudflare function. It's the same pattern — the function holds the key, the browser only ever talks to your own domain — just without managing infrastructure.

This is a good fit for static sites and JAMstack apps, but you still need to handle streaming responses, error retries, and usage tracking yourself as your app grows.

Fix 3: Use a hosted API gateway built for this

If you don't want to maintain a proxy, handle streaming chunking, or build usage dashboards from scratch, a hosted gateway solves the CORS problem the same way (server-side key, your frontend never touches Claude directly) while also giving you app-specific keys, metering, and team access out of the box.

SubToAPI is built exactly for this: it sits between your frontend/backend and Claude, issuing scoped sub_live_... keys per application so you're never putting a raw Anthropic key into any client code. Requests go to https://api.subtoapi.app/v1/messages from your server (or your own thin proxy), and you get streaming, tool use, and usage metadata without building the plumbing yourself.

curl https://api.subtoapi.app/v1/messages \
  -H "Authorization: Bearer $SUBTOAPI_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "claude-sonnet-4-5",
    "max_tokens": 1024,
    "messages": [{"role": "user", "content": "Hello"}]
  }'

Note that even with SubToAPI, the call should still originate from a server or serverless function, not directly from browser JS — the sub_live_... key is scoped per app but still shouldn't be embedded in client-side code. The difference is you get key rotation, per-team seats, and usage dashboards without writing that infrastructure yourself. See the quickstart and messages docs for the full request/response shape, or streaming docs if your frontend needs token-by-token output.

A note on streaming from the frontend

Even after fixing CORS, streaming introduces another layer: your frontend needs to consume a server-sent stream from your own backend endpoint, not directly from Claude. The pattern is the same — browser talks to your origin, your origin talks to Claude (or SubToAPI) and relays chunks back over its own connection to the browser, typically via SSE or a readable stream.

Quick checklist

questions

Can I just add CORS headers myself to fix this? No — CORS headers are set by the server receiving the request (Anthropic's API), not the client. You can't add them from your frontend code. The only real fix is routing the request through a server you control.

Is there any way to call Claude directly from browser JavaScript? Not safely. Even if a workaround existed, it would expose your API key to anyone viewing page source or network requests. Always proxy through a backend.

Does using a gateway like SubToAPI remove the need for a backend? No — you still call it from a server or serverless function. What it removes is the need to build key rotation, usage tracking, and streaming infrastructure yourself; see the docs for setup details.

Turn your Claude access into an HTTPS API

SubToAPI gives you application API keys, streaming, tool use and usage insights on top of your existing Claude access — set up in minutes.

Start free  Read the quickstart →