How to Fix Claude API CORS Error (Cross-Origin Resource)
Updated 9/6/2026
If you try to call the Anthropic API directly from a frontend application (such as React, Vue, Angular, or a static HTML page), your browser will block the request and throw a Cross-Origin Resource Sharing (CORS) error. The browser console will usually report that the header Access-Control-Allow-Origin is missing from the requested resource.
This behavior is intentional. This guide explains why direct browser requests are blocked and how to implement a secure proxy to resolve the error.
Why Does Claude Block Client-Side Requests? Anthropic's API does not include CORS headers that allow web browsers to access its endpoints directly. This security design protects your organization.
If you call api.anthropic.com directly from front-end Javascript, your private x-api-key header is exposed to the public. Anyone viewing the page's source code, network requests, or developer tools can extract your key, use your budget, and access your organization's account. To prevent this, Anthropic forces all API requests to originate from secure, server-side environments.
---
How to Resolve the Claude API CORS Error
To bypass CORS safely, you must route your API requests through a server-side backend or serverless function.
1. Stop Calling the API from the Frontend Remove any direct calls to `https://api.anthropic.com/v1/messages` from your React, Vue, Svelte, or vanilla JS code. Also, do not initialize the `@anthropic-ai/sdk` directly in client-side files.
2. Set Up a Secure Backend Proxy You must build a small server or serverless endpoint that acts as a middleman. Your frontend calls *your* server, and your server calls the Anthropic API.
Here is a conceptual example of a secure Node.js Express endpoint:
`javascript const express = require('express'); const cors = require('cors'); const { Anthropic } = require('@anthropic-ai/sdk');
const app = express(); app.use(cors({ origin: 'https://your-frontend-domain.com' })); // Restrict CORS to your app app.use(express.json());
const anthropic = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY, // Kept safe on the server });
app.post('/api/chat', async (req, res) => { try { const { messages } = req.body; const response = await anthropic.messages.create({ model: 'claude-3-5-sonnet-latest', max_tokens: 1024, messages: messages, }); res.json(response); } catch (error) { res.status(500).json({ error: error.message }); } });
app.listen(3000, () => console.log('Secure proxy running on port 3000')); `
3. Store Your API Key Safely Do not hardcode your API key anywhere in your backend codebase. 1. Create a `.env` file in your backend directory. 2. Add your key: `ANTHROPIC_API_KEY=your_actual_key_here`. 3. Ensure your `.gitignore` file includes `.env` so you do not push your credentials to Github.
4. Connect Your Frontend to Your Proxy Update your frontend network request code to target your newly created backend endpoint instead of Anthropic's servers:
`javascript // Frontend code async function sendMessageToClaude(userMessages) { const response = await fetch('https://your-backend-server.com/api/chat', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ messages: userMessages }), }); const data = await response.json(); return data; } `
---
When to Escalate If you have built a backend proxy server, verified that your server successfully executes the API calls, but you still experience CORS or network connectivity blocks between your frontend and your backend, the issue lies in your hosting provider's CORS configuration.
Check the origin configurations on your hosting platform (Vercel, AWS, Heroku, or Netlify) to ensure your frontend domain is explicitly whitelisted to communicate with your backend.
Quick fixes
- Claude is down or not loading
- Claude Pro billing or payment problem
- Can't sign in to Claude