How to find API tokens and create a preview secret in Sanity.io.

3 replies
Last updated: Mar 15, 2024
Hello, i have sanity .env questions. - where can we find below details
AI Update

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:

  1. Go to manage.sanity.io and log in
  2. Select your project from the list
  3. 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:

  1. Go to manage.sanity.io
  2. Select your project
  3. Navigate to APITokens in the sidebar
  4. Click "Add API token"
  5. Give it a name and select the appropriate permissions (Editor, Viewer, etc.)
  6. 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-21
  • 2025-02-19 (latest, changed default perspective to published)

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.

On your project dashboard. If you go to sanity.io and log in you'll find your projects there and on the page for that project you'll find those tokens.
Hello
user G
Thanks for your response. It is really helpful
I created API token as "Website preview" from here for SANITY_API_TOKEN - now how can do for the SANITY_PREVIEW_SECRET
The
SANITY_PREVIEW_SECRET
can be anything - it's just there to hash things with (I think). You need to create (ideally) 2 tokens: 1 with read access and 1 with read and write access. You do that in the API panel in the
tokens
section at the bottom.

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.

Was this answer helpful?