Environment Variables
Environment variables in Docsta are used to securely store sensitive values such as API keys, project IDs, and dataset names. These variables ensure your app connects to the right services without hardcoding values into your source code.
1. Creating a .env.local File
Create a .env.local file in the root of your Next.js project. This file is ignored by Git by default and should never be committed to version control.
touch .env.local
2. Common Variables for Sanity
If you're using Sanity as your content backend, you'll typically need the following variables:
# .env.local
NEXT_PUBLIC_SANITY_PROJECT_ID=your_project_id
NEXT_PUBLIC_SANITY_DATASET=production
NEXT_PUBLIC_SANITY_API_VERSION=2023-01-01NEXT_PUBLIC_SANITY_PROJECT_ID– Your Sanity project IDNEXT_PUBLIC_SANITY_DATASET– Dataset name (usuallyproduction)NEXT_PUBLIC_SANITY_API_VERSION– API version (use current ISO date or a fixed supported date)
These variables are prefixed with NEXT_PUBLIC_ to make them accessible in the browser (required for client-side content fetching).
3. Using Environment Variables in Code
You can access these variables like this:
const projectId = process.env.NEXT_PUBLIC_SANITY_PROJECT_ID
const dataset = process.env.NEXT_PUBLIC_SANITY_DATASET
const apiVersion = process.env.NEXT_PUBLIC_SANITY_API_VERSIONThey are typically used in your Sanity client configuration or fetching functions.
4. Deployment Environment Setup
When deploying (e.g., to Vercel, Netlify, or any cloud provider), you must manually add the same variables to the deployment dashboard.
Vercel:
- Go to your project > Settings > Environment Variables
- Add each variable and its value
- Redeploy your project
Netlify:
- Go to Site settings > Environment
- Add the variables under Build & deploy > Environment
5. Optional Variables
You can also define custom environment variables for:
- Feature toggles
- API base URLs
- External integrations
Example:
NEXT_PUBLIC_DOCS_VERSION=1.0.0
NEXT_PUBLIC_GITHUB_REPO=https://github.com/your-org/your-docsEnvironment variables keep your configuration flexible, secure, and ready for multiple environments (local, staging, production).
👉 Next Up: Learn how to fetch content using these variables with your Sanity client or content API.
What did you think of this content?
It was helpful
It was not helpful
I have a feedback