Fix guides · Cursor, v0, Lovable, Bolt, Claude Code ·

Fix "Failed to fetch" error

Error: failed to fetch

Your fetch request was blocked by the browser because the response did not include the right CORS headers, or the request never reached the server (network failure). The browser hides the real cause behind a generic "Failed to fetch" message. The fix depends on the root cause: CORS misconfiguration on your API, a missing CORS header on an error response, a Supabase Edge Function without CORS handling, or a network issue.

Free · 3 a day · no signup

Common causes – and how to tell which one you have

1. Your own API doesn't send CORS headers

How to tell: In DevTools → Network, click the failed request: the response headers have no Access-Control-Allow-Origin, or it lists a different origin (for example only your production URL, not http://localhost:3000, or the reverse).

Fix: Add CORS handling on the API server, such as the cors middleware in Express, that allows your exact origins, both http://localhost:3000 and your deployed URL. Redeploy the API.

2. Preflight (OPTIONS) request isn't handled

How to tell: The Network tab shows an OPTIONS request to the same URL that fails or returns 404/405.

Fix: Make the server answer OPTIONS requests with a 2xx status and include Access-Control-Allow-Origin, Access-Control-Allow-Methods and Access-Control-Allow-Headers (listing headers like Content-Type and Authorization).

3. Third-party API that isn't meant to be called from browsers

How to tell: You don't control the API, its docs say to call it from a backend, or it needs a secret API key that is currently in your frontend code.

Fix: Create a server-side route or serverless function that calls the API using the key from an environment variable, and have the frontend call your route instead. If a secret key was ever in client code, rotate it.

4. Supabase Edge Function missing CORS handling

How to tell: The URL contains .supabase.co/functions/v1/. The function either has no OPTIONS branch or returns some responses (often errors) without the CORS headers.

Fix: Following Supabase's CORS pattern, return the CORS headers when req.method is 'OPTIONS' and spread them into every response, including error responses. Then redeploy the function.

5. Network failure — request never reached the server

How to tell: In DevTools → Network, the request shows no response at all (not even an error status). The server may be down, or a VPN, firewall, or ad blocker is blocking the request.

Fix: Check your internet connection and any VPN or firewall settings. Confirm the server is running by visiting the URL directly in the browser.

How to fix it

  1. Open your browser DevTools → Network tab and reload. Click the failing request and note the status code and response headers.
  2. Check that the URL is real and correct, not a placeholder like api.example.com.
  3. If the request shows a 404 or 500 but HAS CORS headers, the server is responding correctly — the error is in your code handling the response, not in the fetch itself. If it shows a 404 or 500 WITHOUT CORS headers, add CORS headers to error responses too.
  4. If the Network tab shows no response or a CORS error, check whether you own the API. If yes, add CORS headers (Access-Control-Allow-Origin, Allow Methods, Allow Headers). If third-party, create a server-side proxy route.
  5. For Supabase Edge Functions: ensure the function returns CORS headers on all responses including errors, and handles OPTIONS preflight requests.
  6. Restart the dev server and confirm the error is gone. For production, check the deployed site in an incognito window to rule out browser extensions.

Using Lovable, Bolt or v0?

In Lovable or Bolt, tell the tool: "My fetch to [URL] is blocked by CORS. Don't call third-party APIs with secret keys from the browser. Move the call to a backend function and keep the key in secrets. If the call goes to a Supabase Edge Function, ask it to handle the OPTIONS preflight and add CORS headers to every response, including errors. Then check the function's logs in the Supabase dashboard for crashes. After you publish, make sure the API allows your live domain, not just the preview or localhost URL."

Using Cursor, Claude Code or Windsurf?

Ask the agent to first find out whether the failing URL is your own API, a third-party API, or a placeholder, and whether an OPTIONS preflight is involved. Have it make the change on the server side, either CORS config or a proxy route, not in the fetch call. To verify, have it run curl -i -X OPTIONS with an Origin: http://localhost:3000 header and an Access-Control-Request-Method header against the endpoint and show you the Access-Control-Allow-* headers in the response. Then reload the page and confirm in the Network tab.

The “fix” to avoid

Agents often add mode: "no-cors" to fetch. That hides the error but gives you an "opaque" response your code can't read, so the data is still missing. Other common bad fixes are routing through a public CORS proxy, which leaks your requests and keys to a stranger, or setting Access-Control-Allow-Origin: * on an API that uses cookies, which browsers reject when credentials are included. Adding Access-Control-Allow-Origin to the request headers in your frontend does nothing, because the header has to come from the server's response.

Copy-paste fix prompt for your AI coding tool

Paste this into Cursor, Claude Code, Lovable or Bolt:

My fetch request fails with: "Failed to fetch". Check in this order: 1) Is the URL correct and does the endpoint return 2xx? 2) Is this my API or third-party? 3) If my API, add CORS headers allowing my exact origins (localhost and production URL), answer OPTIONS preflight, allow Content-Type and Authorization headers, and include CORS headers on error responses too. 4) If third-party, create a server route that calls the API with the key from an environment variable, and have the frontend call that route. 5) For Supabase Edge Functions: ensure CORS headers are present on every response and OPTIONS preflight is handled. Verify in DevTools Network that the response includes Access-Control-Allow-Origin and the data loads. Do not use mode: "no-cors" or public CORS proxies.
Your error looks a bit different? Paste your exact error and get a diagnosis tailored to your code — free.

Questions people ask

Why does the request work in Postman or curl but not in my browser?

CORS is enforced only by browsers, to protect users. Postman and curl ignore it, so a working request there just proves the server responds. It doesn't prove the server allows your site origin.

Can I fix a CORS error from my frontend code?

No. The Access-Control-Allow-Origin header must come from the server's response. From the frontend side, your only real option is to call your own backend route, which then calls the API server-to-server where CORS doesn't apply.

Why did it work on localhost but break after deploying (or the other way round)?

The API's allowed-origins list probably includes only one of them. Origins must match exactly, including http vs https and the port, so add every origin your app runs on.

Related errors

Get fixes like this every week