External fetch API (Forbidden)

I’m trying to fetch from an external api but I get (forbidden).

Locally it works ok.
I added the vercel.json file with headers, in the admin I enabled OPTIONS Allowlist and entered the path /api.

What could be missing?

Using astro framework

  const res = await fetch('EXTERNAL_API',
    {
      headers: {

        "Content-Type": "application/json",
        "Access-Control-Allow-Credentials": "true" ,
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Methods": "GET,OPTIONS,PATCH,DELETE,POST,PUT" ,
        "Access-Control-Allow-Headers": "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version" 
      },
    });

    const result= await res.json();

added vercel.json
OPTIONS Allowlist -> /api

Hi, @frodrigues! Welcome to the Vercel Community :smile:

CORS errors are always the most frustrating. Sorry to hear this! Let’s try and get to a solution :pray: In case anyone else stumbles upon this post, we also have a helpful guide on this topic:

For your specific case, could you try and:

  1. Remove CORS headers from your fetch request. They should be set on the server side, not in the client-side fetch.
  2. Update your vercel.json to include CORS headers for all routes:
{
  "headers": [
    {
      "source": "/(.*)",
      "headers": [
        { "key": "Access-Control-Allow-Origin", "value": "*" },
        { "key": "Access-Control-Allow-Methods", "value": "GET,OPTIONS,PATCH,DELETE,POST,PUT" },
        { "key": "Access-Control-Allow-Headers", "value": "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version" }
      ]
    }
  ]
}
  1. Redeploy your app after making these changes.
  2. If the issue persists, check if the external API allows requests from your Vercel deployment’s domain.

If you’re still encountering problems, please provide more details about the external API and the exact error message you’re receiving :pray:

Let us know how you get on!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.