Claude API Cross-Origin CORS Configuration Guide
If you've tried calling the Claude API directly from a browser-based app, you've probably hit a CORS error in the console: Access-Control-Allow-Origin missing, or the request is blocked before it even reaches Anthropic's servers. This isn't a bug in your code — Anthropic's API is not designed to be called from client-side JavaScript, and it doesn't return the permissive CORS headers a browser needs to accept the response.
The short answer: you can't fully solve this with CORS configuration on your side, because you don't control Anthropic's server headers. The actual fix is to route Claude API requests through a backend (your own proxy or a hosted API layer) that sits between your frontend and the model, handles the cross-origin headers correctly, and — just as importantly — keeps your API key off the client. This guide covers why the error happens, how to build a correct proxy, and what to check if requests are still failing.
Why you get a CORS error calling Claude from the frontend
CORS (Cross-Origin Resource Sharing) is a browser security mechanism, not a server feature you can switch on remotely. When your frontend running on https://app.example.com sends a request to https://api.anthropic.com/v1/messages, the browser first checks whether the response includes headers like:
Access-Control-Allow-Origin: https://app.example.com
Access-Control-Allow-Headers: content-type, x-api-key, anthropic-version
Anthropic's API does not return these headers for arbitrary origins, so the browser blocks the response (or the preflight OPTIONS request fails outright). This is intentional — direct browser access to a raw LLM API with a long-lived secret key is a security risk, not just a CORS inconvenience.
Even if you found a workaround to bypass the preflight check, shipping your Anthropic API key inside frontend JavaScript means anyone can read it from the network tab and use it on your bill. CORS errors here are effectively doing you a favor.
The correct fix: a backend proxy
The standard pattern is: frontend → your backend → Claude API. Your backend holds the real API key, your frontend talks to your backend, and your backend sets the CORS headers your frontend actually needs.
A minimal Node/Express proxy looks like this:
import express from "express";
import cors from "cors";
const app = express();
app.use(express.json());
app.use(cors({
origin: ["https://app.example.com"],
methods: ["POST"],
allowedHeaders: ["Content-Type", "Authorization"],
}));
app.post("/api/chat", async (req, res) => {
const response = await fetch("https://api.anthropic.com/v1/messages", {
method: "POST",
headers: {
"content-type": "application/json",
"x-api-key": process.env.ANTHROPIC_API_KEY,
"anthropic-version": "2023-06-01",
},
body: JSON.stringify(req.body),
});
const data = await response.json();
res.status(response.status).json(data);
});
app.listen(3000);
Key points in this setup:
- The real API key only ever exists server-side, in an environment variable.
cors()is scoped to your actual frontend origin — never setorigin: "*"on a route that forwards authenticated requests, since that lets any website use your backend as an open proxy.- You validate and rate-limit incoming requests before forwarding them, so a leaked frontend endpoint can't be used to drain your Anthropic quota.
- If you need streaming responses, you'll also need to proxy chunked/SSE output correctly, which adds more code than this snippet shows.
Skipping the proxy you'd otherwise have to maintain
Building and operating this proxy yourself means handling CORS, key rotation, rate limiting, retries, and streaming forwarding — on top of whatever else your app does. SubToAPI does this layer for you: it turns your existing Claude access into an HTTPS API with its own application keys (sub_live_...), and the hosted endpoint is already configured to accept cross-origin requests from your registered app domains, so you don't write CORS middleware at all.
A frontend call through SubToAPI looks like:
const res = await fetch("https://api.subtoapi.app/v1/messages", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${process.env.SUBTOAPI_KEY}`,
},
body: JSON.stringify({
model: "claude-sonnet-4",
messages: [{ role: "user", content: "Summarize this PR description." }],
}),
});
In practice you still shouldn't put the sub_live_... key directly in frontend code either — the same key-exposure logic applies. Use SubToAPI from your own backend (or a thin serverless function) the same way you would Anthropic's API, and let SubToAPI handle streaming, tool use, and usage metadata on top. See the quickstart and the Messages API reference for request formats, and streaming if your frontend needs token-by-token output. Plans start with a free trial at signup, with pricing from Solo at €9/month listed on the pricing page.
Checking your CORS setup is actually correct
If requests still fail after adding a proxy, check these in order:
- Preflight response — open DevTools Network tab, find the
OPTIONSrequest, and confirm it returns200withAccess-Control-Allow-Originmatching your frontend's exact scheme + host + port. - Allowed headers — if you send custom headers like
AuthorizationorContent-Type: application/json, they must appear inAccess-Control-Allow-Headers, or the preflight fails even if the main request would have worked. - Credentials mode — if your frontend
fetchusescredentials: "include", the server cannot respond withAccess-Control-Allow-Origin: *; it must echo back a specific origin. - Wildcard origins in production —
origin: "*"passes CORS checks but removes any access control, meaning any site can call your authenticated proxy from a visitor's browser.
questions
Does Anthropic's Claude API support CORS for direct browser calls? No. Anthropic does not return permissive CORS headers for arbitrary origins, and calling the API directly from client-side JavaScript would also expose your API key, so direct browser access isn't recommended even where technically possible.
**Can I just add Access-Control-Allow-Origin: to fix this?* Only on a server you control that's proxying the request — you can't add that header to Anthropic's responses. Setting it to * on your own proxy is also risky if the proxy forwards an authenticated key, since it turns your backend into an open relay for anyone.
What's the fastest way to add a Claude-backed API with CORS already handled? Use a hosted gateway instead of writing your own proxy. SubToAPI provisions an HTTPS endpoint with application keys, streaming, and cross-origin support out of the box — see the docs to get started.