Back

Getting CORS Error during Appwrite Function from localhost?

  • 0
  • Functions
  • Web
shashankdaima
16 Oct, 2023, 21:47

Hey Appwriters :), I am new to WebDev, majorily worked in Mobile Industry. So, I have zero knowledge of CORS Errors. I made public cloud function in appwrite using python, wanted to use that in React Project, but for some reasons I am constantly facing CORS error, whereas i can use the function in curl, browser and postman. This is my source code of appwrite function,

TypeScript
from appwrite.client import Client
import os

from .notion_api import get_top_news


NOTION_API_KEY = os.environ["NOTION_API_KEY"]

def main(context):
    
    return context.res.json(
        {
            "notion_top_news": get_top_news(NOTION_API_KEY),
        }
    )

Whereas I am calling it like this,

TypeScript
export async function fetchTopNews(): Promise<{ notion_top_news: { document_id: string; headline: string; image_url: string; summary: string } }> {
    const url = 'https://<function-id>.appwrite.global/';

    try {
        const response = await fetch(url);

        if (!response.ok) {
            throw new Error(`Request failed with status: ${response.status}`);
        }

        const data = await response.json();

        if (!data.notion_top_news || typeof data.notion_top_news !== 'object') {
            throw new Error('Invalid JSON format in response');
        }

        return data as {
            notion_top_news: {
                document_id: string;
                headline: string;
                image_url: string;
                summary: string;
            };
        };
    } catch (error) {
        throw new Error(`Failed to fetch data: ${error}`);
    }
}
TL;DR
The user is experiencing a CORS error when trying to call an Appwrite function from localhost in a React project. They have already added a client in the Appwrite dashboard and set localhost as the hostname. They are unsure how to add a custom header in the response. Solution: To resolve the CORS error, the user needs to return the `Access-Control-Allow-Origin` header in the response from the Appwrite function. They can add the header using the Python sample code provided in the support thread. Additionally, they should make sure that the function is hosted in the cloud and is being called correctly using the correct Appwrite domain.
shashankdaima
16 Oct, 2023, 21:48

So, When i run react on locahost:3000, it is throwing error.

D5
16 Oct, 2023, 22:00

You will need to add a platform in the appwrite dashboard and add localhost as hostname

shashankdaima
16 Oct, 2023, 22:01

hey @D5 , i have already added a client...

shashankdaima
16 Oct, 2023, 22:02
shashankdaima
16 Oct, 2023, 22:04

is there any tutorial or any other sample that explain how to use cloud function only from React/etc framework?

shashankdaima
16 Oct, 2023, 22:11

hey i just check console, localhost/:1 Access to fetch at 'https://<function-id>.appwrite.global/' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

shashankdaima
16 Oct, 2023, 22:12

so how to add header in response?

D5
16 Oct, 2023, 22:14

If the function is not hosted in cloud, why you're calling it like that? I mean, by using appwrite.global domain

shashankdaima
16 Oct, 2023, 22:16

function is hosted to cloud.(I removed the actual function host name, because it is proxy for notion api, i don't want to it ping it unwantedly)

shashankdaima
16 Oct, 2023, 22:16

it surely not giving header, can set custom response headers?

shashankdaima
16 Oct, 2023, 22:17

Access-Control-Allow-Origin

Drake
17 Oct, 2023, 00:35

do you need to use the function domain? Why not use the Appwrite SDK?

Drake
17 Oct, 2023, 00:35

i think the problem is the user doesn't have access to make that fetch api call

Drake
17 Oct, 2023, 00:49

Getting CORS Error during Appwrite Function from localhost?

shashankdaima
17 Oct, 2023, 13:51

Hey @Steven , I am using function as proxy b/w notion API and caching markdown in appwrite storage bucket. thinking that it will be much better that I just remove appwrite specific code from website. So, that if I need to host my own flask server in future, then I don't have to remove appwrite SDK. Currently my whole project only depends on functions only. and In functions I am accessing storage and db.

Drake
17 Oct, 2023, 14:47

i see okay. so what are the permissions set on this function?

shashankdaima
17 Oct, 2023, 18:28

for now it is any, and api allow localhost only. for now. later on deployment i will also deployement url+localhost only.

Drake
17 Oct, 2023, 18:39

and api allow localhost only

What do you mean?

Drake
17 Oct, 2023, 18:40

you may need to return Access-Control-Allow-Origin header to allow CORS then

shashankdaima
17 Oct, 2023, 19:41

yes, btw it isn't localhost but wildcard(*).

shashankdaima
17 Oct, 2023, 19:42

btw thanks Steven.

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more