I want to call a web API to get data about schools within a given radius of a location.
In the documentation for the API, it gives an example saying that I can call it with curl --location --request GET 'https://gs-api.greatschools.org/schools?city=alameda&state=ca&limit=50' --header 'x-api-key: myapikey’
, where the --header 'x-api-key: myapikey' is the key code I have for the API. It works in the terminal, and
I want to call it in a React application on my local host using
fetch(https://gs-api.greatschools.org/schools?city=alameda&state=ca&limit=50', {
{
method: 'GET',
headers: {'x-api-key': 'XXXXX'} //XXXX is my api key
}
}
but I get a
localhost/:1 Access to fetch at 'https://gs-api.greatschools.org/schools?city=alameda&state=ca&limit=50' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: 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.
How can I pass the --header 'x-api-key:myapikey' from the fetch request so that it returns the expected response? The documentation for the API I am using can be found at https://docs.google.com/document/d/1pSe1AeZXGL01m5uG3wwRr9k4pI2Qw52xzPi2NmNrhyI/edit#heading=h.mo9am8ecq574