Network error querying Sanity GROQ API in Electron React app

11 replies
Last updated: Apr 15, 2020
Hi all, am just looking at integrating Sanity with React in a really basic way for proof of concept testing for a desktop app using Electron.
I was just wondering what the most straightforward GROQ query usage would be to do this?

I was using essentially the query from the React Native git repo which I have working on another POC experiment. But when the query is called I get the following:


Unhandled Rejection (Error): Network error while attempting to reach <https://fi3u3pfj.apicdn.sanity.io/v1/data/query/production?query=%0A%20%20*%5B_type%20%3D%3D%20'course'%5D%20%7B%0A%20%20_id%2C%20%0A%20%20title%2C%20%0A%20%20caption%2C%0A%20%20subtitle%2C%0A%20%20body%2C%0A%20%20%22logo%22%3A%20logo.asset-%3Eurl%2C%0A%20%20%22image%22%3A%20mainImage.asset-%3Eurl%2C%0A%7D>
But the query url is working fine:


https://fi3u3pfj.apicdn.sanity.io/v1/data/query/production?query=%0A%20%20*%5B_type%20%3D%3D%20%27course%27%5D%20%7B%0A%20%20_id%2C%20%0A%20%20title%2C%20%0A%20%20caption%2C%0A%20%20subtitle%2C%0A%20%20body%2C%0A%20%20%22logo%22%3A%20logo.asset-%3Eurl%2C%0A%20%20%22image%22%3A%20mainImage.asset-%3Eurl%2C%0A%7D
Any clues what I'm missing here?
AI Update

This is a classic CORS (Cross-Origin Resource Sharing) issue! The good news is your query is working fine - the problem is that Sanity's API is blocking the request because your Electron app's origin isn't whitelisted.

When you open that URL directly in your browser, it works because you're making a simple GET request. But when your React app running in Electron tries to fetch it, the browser engine sees it as a cross-origin request and checks CORS headers.

The Solution: Add Your Origin to CORS Settings

You need to add your Electron app's origin to your Sanity project's CORS origins:

  1. Go to manage.sanity.io
  2. Select your project
  3. Navigate to Settings > API > CORS Origins
  4. Click Add CORS origin

For Electron apps, you'll likely need to add one of these origins depending on how your app loads content:

  • http://localhost:3000 (or whatever port your dev server uses during development)
  • file:// (if your Electron renderer loads from the file system)
  • http://localhost (without a port)

Important: Make sure to check the "Allow credentials" checkbox if you're using any authentication tokens with your Sanity client. This is required when the Access-Control-Allow-Credentials header needs to be 'true', as explained in this CORS troubleshooting guide.

For Electron-Specific Challenges:

Electron can be tricky with CORS because the renderer process might use the file:// protocol. If adding http://localhost doesn't work, you have a few options:

  1. Use a local dev server - Run your React app on http://localhost:3000 and add that to CORS origins (recommended for development)
  2. Disable web security in Electron (development only!) - Add webPreferences: { webSecurity: false } to your BrowserWindow config
  3. Proxy through the main process - Make API calls from Electron's main process (Node.js environment) where CORS doesn't apply, then send data to the renderer

The CORS configuration documentation has more details about managing these settings, and you can also use the Sanity CLI with sanity cors add if you prefer command-line configuration.

Once you've added the correct origin with credentials enabled, your React app should be able to fetch data without the network error!

Show original thread
11 replies
I believe you have to add
null
to CORS origins?
In the fetch you mean?
On manage.sanity.io =&gt; Settings =&gt; API
As in like this? Sorry if this is a silly question!
Yup! There is no such thing as a silly question:)
Does the studio need redeployment after that or is it automatic.
And thanks
user Y
I’ve tested that idea a little in the past :)
No redeployment needed. This only affects the backend
Will double check later and report back
user Y
- after all that it was a localhost issue. Added
<http://localhost:3000/>
and we're in business.
But thanks for your help, that set me on the right track and I appreciate it!
Aha. I thought electron apps wanted
null
but awesome that you figured it out!
Thanks again

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?