Discussion on protecting project ID and dataset in Nuxt project with Sanity
Congrats on your first Nuxt + Sanity project! š
Great news: you don't actually need to protect your project_id and dataset with environment variables from a security perspective. Here's why:
Your project ID and dataset are meant to be public information. Even if someone knows them, they cannot write or modify your content without proper authentication. To mutate your content, they would need:
- A valid write token with the correct permissions
- Or authenticated access to your Sanity account
When you use Sanity Studio, authentication tokens are automatically generated when you log in. So even if someone had your project ID, dataset, and even your entire source code, they still couldn't do anything to your data without your Sanity login credentials.
That said, you can still use environment variables if you want to keep your configuration clean and flexible across different environments. In Nuxt, you'd configure it like this in your nuxt.config.ts:
export default defineNuxtConfig({
modules: ["@nuxtjs/sanity"],
sanity: {
projectId: process.env.NUXT_PUBLIC_SANITY_PROJECT_ID,
dataset: process.env.NUXT_PUBLIC_SANITY_DATASET,
},
});Then create a .env file:
NUXT_PUBLIC_SANITY_PROJECT_ID=your_project_id NUXT_PUBLIC_SANITY_DATASET=your_dataset
About CORS origins: You can keep localhost:3000 in your CORS settings - it makes local development much easier. If you want extra obscurity (though it's not a real security measure), you could run your Studio on a non-standard port like sanity start --port=12345 and add that to your CORS origins instead.
The bottom line: Your content is already protected by Sanity's authentication system, regardless of whether your project ID is visible or not. Anyone can read your public data, but only authenticated users can write to it.
Check out the Nuxt quickstart guide for more details on configuration, and the Studio environment variables documentation if you want to dive deeper into environment configuration.
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.