
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeThe modern approach is to use environment variables with Next.js, and you don't need to access sanity.json from your Next.js code at all. Here's how to set this up properly:
Create a .env.local file in your Next.js project root (not in the Studio directory):
NEXT_PUBLIC_SANITY_PROJECT_ID=your-project-id
NEXT_PUBLIC_SANITY_DATASET=productionThe NEXT_PUBLIC_ prefix is important - it tells Next.js to expose these variables to the browser.
Then update your client configuration:
// lib/sanity.js or client.js
import { createClient } from '@sanity/client'
// or if using next-sanity:
// import { createClient } from 'next-sanity'
export const client = createClient({
projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,
dataset: process.env.NEXT_PUBLIC_SANITY_DATASET,
apiVersion: '2024-01-01', // use current date
useCdn: true, // set to false if you need fresh data
})In your Netlify dashboard, add the same environment variables:
NEXT_PUBLIC_SANITY_PROJECT_ID with your project IDNEXT_PUBLIC_SANITY_DATASET with production (or your production dataset name)This way, your local development uses the values from .env.local, while your Netlify deployment uses the values from Netlify's environment settings. You can even set different values for different Netlify deploy contexts (production, preview, etc.).
If you're starting fresh or can update, I'd recommend using the next-sanity package, which is the official toolkit for Next.js integration. When you run sanity init in a Next.js project, it automatically sets up environment variables and creates a properly configured client for you.
The key insight is that sanity.json is a Studio v2 configuration file that's separate from your Next.js app - you don't need to access it from Next.js. Environment variables are the clean, standard way to configure your Sanity client for different environments.
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.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store