Where to find Sanity environment variable details in .env file?
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
- Open Manage from the Studio's top-left menu (Manage project), or run
npx sanity@latest manage. The project page shows the project ID and lists your datasets, oftenproduction. - If you already have a Studio, both values are also in
sanity.config.ts(defineConfig({projectId, dataset})) and insanity.cli.tsunderapi: {projectId, dataset}(CLI config).
API token (SANITY_API_TOKEN / SANITY_API_READ_TOKEN)
- In Manage, go to API → Tokens → Add new token. You need the developer or admin role, or a custom role with the same access.
- Give the token a name and a role. Use Viewer (read-only) for fetching drafts in previews. Only create a token with write access if your app actually writes data, and use a separate token for each application.
- Copy the token right away. It's shown only once and can't be recovered.
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:
- your frontend, where the draft-mode route compares the
secretquery parameter against it - your Studio's preview setup
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 thread3 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.