Skip to content

Where to find Sanity environment variable details in .env file?

3repliesLast updated: Nov 14, 2026

Hello, i have sanity .env questions. - where can we find below details
AI Update

You get the project ID, dataset and API token from your project in Manage. The preview secret (SANITY_PREVIEW_SECRET) doesn't come from Sanity: it's any long random string you make up yourself.

Project ID and dataset

API token (SANITY_API_TOKEN / SANITY_API_READ_TOKEN)

Keep tokens in environment variables and never commit them (Authentication and tokens).

Preview secret (SANITY_PREVIEW_SECRET)

Generate a random string and use the same value in two places:

(Draft mode lesson)

If you're using the Presentation tool, you may not need this variable at all. With @sanity/preview-url-secret, the Studio creates a new secret each time it opens a preview, and that secret expires after one hour. Your enable endpoint checks it with validatePreviewUrl(client, request.url), which only needs a server-side read token (Implementing draft mode).

Example: Next.js .env.local

NEXT_PUBLIC_SANITY_PROJECT_ID="your-project-id"
NEXT_PUBLIC_SANITY_DATASET="production"
SANITY_API_READ_TOKEN="your-viewer-token"
SANITY_PREVIEW_SECRET="a-long-random-string"

Don't add a NEXT_PUBLIC_ prefix to the token or the secret. Read them only in server code and keep them out of the client bundle. Restart the dev server if a new variable isn't picked up (token handling lesson).

Example: Studio .env

SANITY_STUDIO_PROJECT_ID="your-project-id"
SANITY_STUDIO_DATASET="production"

The Studio only reads variables that start with SANITY_STUDIO_, and you have to reference them as full static strings, such as process.env.SANITY_STUDIO_DATASET. Every SANITY_STUDIO_ value gets built into the Studio's JavaScript, so never put tokens there. If the Studio needs a secret, use the @sanity/studio-secrets plugin instead (Studio environment variables).

API version

apiVersion isn't something you look up. It's a UTC date in YYYY-MM-DD format that you choose and hard-code. Any date up to today works, and you get the API as it behaved on that date:

const client = createClient({
  projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,
  dataset: process.env.NEXT_PUBLIC_SANITY_DATASET,
  apiVersion: '2026-07-28', // fixed date string, not computed
  useCdn: true,
})

Don't compute the date at runtime: the API's behavior could then change without you deploying anything. If you leave out apiVersion, the client warns that this is deprecated and falls back to v1 (API versioning).

Show original thread
3 replies

Was this answer helpful?

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.

Related contributions