Sanity "Configuration must contain projectId" Error

9 replies
Last updated: Oct 19, 2023
AI Update

This error means your Sanity Studio configuration is missing the projectId value, which is required to connect to your Sanity project. Here's how to fix it:

Quick Fix

  1. Find your Project ID: Log into sanity.io/manage and locate your project ID in the dashboard

  2. Add it to your configuration: Open your Sanity config file (usually sanity.config.ts or sanity.config.js) and make sure it includes your projectId:

import {defineConfig} from 'sanity'

export default defineConfig({
  projectId: 'your-project-id-here', // ← Add this
  dataset: 'production',
  // ... rest of your config
})

If You're Using Environment Variables

For Next.js or other frameworks, you might be using environment variables. Make sure:

  1. Your .env.local file has the correct variables:
NEXT_PUBLIC_SANITY_PROJECT_ID=your-project-id-here
NEXT_PUBLIC_SANITY_DATASET=production

Note: In Next.js, the NEXT_PUBLIC_ prefix is required for client-side access.

  1. Your config file references them correctly:
import {defineConfig} from 'sanity'

export default defineConfig({
  projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,
  dataset: process.env.NEXT_PUBLIC_SANITY_DATASET,
  // ... rest of your config
})
  1. Restart your dev server after adding environment variables - they're only loaded on startup

Common Gotchas

  • Using a template? Make sure you've replaced the placeholder project ID with your actual one. Search your entire codebase for projectId to find all instances.
  • Project ID is public: Don't worry about exposing it - project IDs aren't sensitive information and will be visible in your production code anyway.

Once you add the correct projectId to your configuration, the error should disappear and your Studio will load properly!

Show original thread
9 replies
This looks like it’s coming from where you’ve configured your client on your front end, rather than from within the Studio code. Can you please post the code snippet where you’ve instantiated your client? Something like:

import { createClient } from '@sanity/client';

export const client = createClient({
  projectId: 'abcd1234',
  dataset: 'production',
  apiVersion: '2023-06-10',
  useCdn: true,
});
Hi User, thank you for offering your help. I'm currently new to Sanity and I'm working on building an E-Commerce app with Sanity and Next.js. I followed the steps to install the Next.js npm package first and then created the project from the web of sanity. Then, I installed the npm package for Sanity that was provided after creating the project.
Since I'm new to Sanity, I'm a bit confused about the code snippet you mentioned. I'm not sure where exactly I need to instantiate the client as you described. Could you please provide more guidance or clarification on how to integrate Sanity into my application and create the client?

Thank you for your assistance, I really appreciate it!
Welcome to the community!
In your Next.js code, you will use the Sanity Client to pull in your content from the Content Lake (essentially the Sanity data store). That client will need to be configured with your projectId, dataset, etc., so that it knows where to source from.

Is there a particular guide or tutorial you’re following? I can take a look and find where the client is configured.
https://youtu.be/4mOkFXyxfsU is a 1 year old video which I am using to learn about Sanity. But the difference is that I am using all the latest versions, not the old ones.
Thank you User for helping, I will check out Content Lake.
Looks like they start that setup at around the 40:36 mark . You’ll now import
{ createClient } from '@sanity/client';
and will use that function in place of
sanityClient
, but the config looks to be the same. Make sure you’re specifying `projectId`—case matters.
You are awesome User, while following the video I will do as you say and import createClient. 🤩 👍
Hey guys! I know I'm late but I'm having similar issue.
Same error but with a NextJS app. My import createClient is a bit different, it does not come from "@sanity/client" but instead "next-sanity"

Here is my ./root/sanity/lib/client.js file (which was generated by
npm create @sanity/latest
cmd):

import { createClient } from 'next-sanity';
import { apiVersion, dataset, projectId, useCdn } from '../env';

export const client = createClient({
  apiVersion,
  dataset,
  projectId,
  useCdn,
});
Let’s use greetings like “Hey Everyone,” “Hi, Y’all," or “Hello, Folks” to make sure everyone in the community is included. Please read the Sanity Community Code of Conduct to stay updated on expected communication & behavior in our spaces: https://www.sanity.io/docs/community-code-of-conduct
The
../env
file in question is:
export const apiVersion =
  process.env.NEXT_PUBLIC_SANITY_API_VERSION || '2023-10-14';

export const dataset = process.env.NEXT_PUBLIC_SANITY_DATASET;

export const projectId = process.env.NEXT_PUBLIC_SANITY_PROJECT_ID;

export const useCdn = false;
And
process.env
is getting those variables from
.env.local
file generated by NextJS as well.

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?