Error connecting Sanity CMS to a site deployed on Vercel, seeking help with CORS configuration and API endpoint.
I can see the issue right away! Your environment variables are all commented out with # symbols at the beginning of each line. In .env files, the # character denotes a comment, which means those lines are being ignored and your variables aren't actually being set.
Remove the # symbols from the beginning of each line in your .env.local file:
SANITY_PROJECT_ID=your_project_id SANITY_DATASET=your_dataset_name SANITY_API_TOKEN=your_sanity_api_token SANITY_API_VERSION=2021-03-25
Also, make sure you replace the placeholder values (your_project_id, your_dataset_name, etc.) with your actual Sanity project credentials. You can find these in your Sanity project dashboard.
A couple of additional things to check:
Restart your development server after updating the
.env.localfile. Environment variables are only loaded when the server starts, so you need to stop and restart it for changes to take effect.Vercel deployment: When deploying to Vercel, you need to add these environment variables in your Vercel project settings (Project Settings → Environment Variables), not just in your local
.env.localfile. The.env.localfile only works for local development and typically isn't committed to your repository.API version: Consider updating your
SANITY_API_VERSIONto a more recent date like2024-01-01orv2024-12-16to get the latest API features and improvements.
The ERR_CONNECTION_REFUSED error you're seeing is likely because your API route is trying to connect to Sanity but doesn't have the required credentials (since they were commented out), causing the connection to fail.
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.