How to find API tokens and create a preview secret in Sanity.io.
I can help you find the details you need for your Sanity .env file! Here's where to locate each piece of information:
Finding Your Project ID and Dataset
You can find both your project ID and dataset name at manage.sanity.io:
- Go to manage.sanity.io and log in
- Select your project from the list
- Your project ID will be visible in the project settings/overview
Your dataset name is typically something like production, development, or whatever you named it when creating your project. You can see all available datasets in your project settings under the "Datasets" section.
Alternatively, if you already have a Studio set up locally, you can find these in your sanity.config.js or sanity.json file (for older Studio v2 projects).
Creating API Tokens
To create an API token:
- Go to manage.sanity.io
- Select your project
- Navigate to API → Tokens in the sidebar
- Click "Add API token"
- Give it a name and select the appropriate permissions (Editor, Viewer, etc.)
- Copy the token immediately - you won't be able to see it again!
Important: Never commit tokens to version control. Add your .env files to .gitignore.
API Version
The API version is a date string in the format YYYY-MM-DD. Common versions include:
2021-03-25(introduced GROQ spec compliance)2021-10-212025-02-19(latest, changed default perspective topublished)
You should always specify an API version to avoid unexpected behavior. The API versioning docs have details on what changed in each version.
Example .env File
Here's what your .env file should look like:
SANITY_STUDIO_API_PROJECT_ID=your-project-id SANITY_STUDIO_API_DATASET=production SANITY_AUTH_TOKEN=your-token-here
Note: Environment variables for Sanity Studio must be prefixed with SANITY_STUDIO_. If you're using Next.js, you'll need NEXT_PUBLIC_ prefix instead for client-side variables. More details in the environment variables documentation.
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.