cd studio
npm install
npx sanity init --bare
You will be prompted with an output that contains the project ID and dataset name.
Success! Below are your project details:
Project ID: <projectId>
Dataset: <dataset>
You can find your project on Sanity Manage — https://www.sanity.io/manage/project/rjtcllfw
Your projectI ID
is not sensitive information
cp -i .env.local.example .env.local
Next, populate the .env.local
file with the project ID and dataset name provided by the Sanity CLI in the previous step, or by visiting the Sanity Manage Console
If you want to start with some sample content, you can import the provided dataset (demoData.tar.gz) into your Sanity project. This step is optional but can be helpful for getting started quickly.
To import the dataset, run the following command in your terminal:
npx sanity dataset import demoData.tar.gz production
This assumes your dataset is named production
. If your dataset is named differently, replace production
with the name of your dataset.
At this point, when you run your Sanity Studio, you'll see a message indicating that Presentation needs to be configured.
We'll address this in the next section when we set up the Next.js app.
npm run dev
Tip
In this demo starter, we are using TypeScript. After making changes to your schema, you'll need to extract the Sanity Studio Schema so that you can use that JSON file to generate TypeScript types for your Next.js app.
npm run extract-types
This command is running sanity schema extract --enforce-required-fields
under the hood. Learn more about extracting types.
cd nextjs-app # from the root of the repo
npm install
cp -i .env.local.example .env.local
You can now populate NEXT_PUBLIC_SANITY_PROJECT_ID
and NEXT_PUBLIC_SANITY_DATASET
by running the following command. Select your project and dataset when prompted and select NO when asked "Would you like to add configuration files for a Sanity project":
npm create sanity@latest -- --env=.env.local
Before you can run the project you need to setup a read token (SANITY_API_READ_TOKEN
), it's used for authentication by Sanity's Presentation tool and pulling content while in draft mode.
- Go to manage.sanity.io and select your project in the "Project" dropdown.
- Click on the
🔌 API
tab. - Click on
+ Add API token
. - Name it "Next.js / Presentation READ Token" and set
Permissions
toViewer
and hitSave
. - Copy the token and add it to your
.env.local
file.
SANITY_API_READ_TOKEN="<paste your token here>"
npm run dev
Note
we are generating types automatically by running "predev": "npm run typegen"
in our package.json file. This is optional, but will build your sanity.types.ts
file automatically. Learn more about Sanity TypeGen.
npm run extract-types
This command is running sanity typegen generate
under the hood. Learn more about generating types.
To deploy your Sanity Studio, follow these steps:
-
You'll likely need a different .env file for production, so that you can set a different
SANITY_STUDIO_PREVIEW_URL
to match the domain you will deploy your Next.js app to. Copy the .env.local file to .env.production and set the correct environment variables.cp -i .env.local .env.production
-
In your terminal use the following command to deploy the Studio to Sanity's servers. Learn more about deploying to Sanity.
npx sanity deploy
-
When prompted, choose a unique hostname for your Studio. This will be the URL where your Studio is accessible.
-
Once the deployment is complete, you'll receive a URL for your deployed Sanity Studio. It will look something like:
https://your-project-name.sanity.studio
-
You can now access and use your Sanity Studio from this URL from any device with an internet connection.
Remember to redeploy your Studio whenever you make changes to its configuration or schema.
Note
Make sure you have the necessary permissions to deploy. If you're working in a team, check with your project owner or administrator.
Tip
You can also set up continuous deployment for your Sanity Studio using services like Netlify or Vercel. This allows your Studio to automatically redeploy whenever you push changes to your repository.
[!NOTE]
You can deploy your Next.js app wherever you'd like, but for the sake of this demo we will be using Vercel.
-
Push your code to a Git repository (GitHub, GitLab, or Bitbucket).
-
Visit Vercel's dashboard and click on "New Project".
-
Import your Git repository:
- Select your Git provider (GitHub, GitLab, or Bitbucket)
- Choose the repository containing your Next.js app
-
Configure your project:
- Set the Root Directory to the directory of your Next.js app
- Vercel will automatically detect that it's a Next.js app
- Adjust the build settings if needed (usually not necessary for Next.js apps)
-
Set up environment variables:
- Click on "Environment Variables"
- Add all the variables from your
.env
file. Don't forget to setNEXT_PUBLIC_SANITY_STUDIO_URL
the url of your deployed studio.
-
Click "Deploy" to start the deployment process.
-
Once deployed, Vercel will provide you with a URL for your live app.
-
(Optional) Set up a custom domain in the Vercel dashboard.
-
Now you can add your App's domain to the list of CORS origins in your Sanity Manage console, under the
🔌 API
tab.
For subsequent deployments, simply push changes to your Git repository. Vercel will automatically rebuild and redeploy your app.
Tip
You can also use the Vercel CLI for deployment. Install it globally with npm i -g vercel
, then run vercel
in your nextjs-app directory and follow the prompts.
[!NOTE]
You may need to disable or configure "Protection Bypass for Automation" in your Vercel settings to get Presentation to work in your Sanity Studio.