How to get dataset name in Sanity v3?

14 replies
Last updated: Feb 2, 2023
Guys, I have a question, in version 2 I obtained my dataset in this way, now how can I obtain it?
import client from "part:@sanity/base/client";

const { dataset } = client.config();
AI Update

In Sanity Studio v3, the Parts System has been completely replaced, so you can no longer use import client from "part:@sanity/base/client". Instead, you have a few modern approaches to get your dataset configuration:

Using the useClient hook

The most common approach in v3 is to use the useClient hook within React components:

import { useClient } from 'sanity'

function MyComponent() {
  const client = useClient({ apiVersion: '2023-05-24' })
  const dataset = client.config().dataset
  
  // Use dataset...
}

Important: Since useClient is a React hook, it must be called inside a React component or custom hook - you can't use it at the module level.

Alternative: Direct config import

If you need to access the dataset outside of a React component (like in a utility file), you can import your configuration directly:

import { dataset } from './sanity.config'

// Use dataset...

Your sanity.config.ts (or .js) file should export your configuration:

import { defineConfig } from 'sanity'

export default defineConfig({
  projectId: 'your-project-id',
  dataset: 'production',
  // ... other config
})

// You can also export individual values
export const { dataset, projectId } = defineConfig({
  projectId: 'your-project-id',
  dataset: 'production',
})

For App SDK applications

If you're building with the App SDK, import from @sanity/sdk-react instead:

import { useClient } from '@sanity/sdk-react'

const client = useClient({ apiVersion: '2023-05-24' })
const dataset = client.config().dataset

The v3 migration removed the old parts-based import system in favor of standard JavaScript imports and React hooks, which provides better TypeScript support and a more modern development experience.

Show original thread
14 replies
I could be wrong, but my understanding is that you will set this up in a `sanity.config.js`file at the root of your project.
Strongly recommend that you setup a sample project that uses V3 of the Studio. There are a few to choose from, it takes 2 minutes, and will be incredibly useful as you inspect how different things are done.
Yes, I think I have it perfect, but in another file I need to have the value of the dataset that I have selected, for example staging or production, in order to show a preview with the url corresponding to the dataset.
You can access the client in a few ways. The cheat sheet goes over them!
user M
Is it possible to use this, in a file other than a component?
Good question! This should work for you.
👀
I get this error 😞
Can you share your code?
import { getCliClient } from "sanity/cli";

const previewSecret = "SANITY_PREVIEW_SECRET";

// Replace `remoteUrl` with your deployed <https://www.sanity.io/nextjs-cms|Next.js> site
const remoteUrl = `<https://site.netlify.app>`;
const localUrl = `<http://localhost:3000>`;
const stagingUrl = `<https://staging--site.netlify.app/>`;

const getBaseURL = () => {
  const dataset = "staging";
  const hostname = window.location.hostname;

  if (hostname === "localhost") {
    return localUrl;
  }

  if (dataset === "production") {
    return remoteUrl;
  }

  return stagingUrl;
};

export default function resolveProductionUrl(doc) {
  const baseUrl = getBaseURL();
  const previewUrl = new URL(baseUrl);

  const client = getCliClient({ apiVersion: "2023-1-03" });

  console.log(client);

  previewUrl.pathname = `/api/preview`;
  previewUrl.searchParams.append(`secret`, previewSecret);
  previewUrl.searchParams.append(`type`, doc?._type);
  previewUrl.searchParams.append(`slug`, doc?.slug?.current ?? `/`);

  return previewUrl.toString();
}
Oh in a case like this, the client is provided to you inside of the context that’s passed as the second argument. This is how you’d want to get your client in this case.
👀
It works perfect, thanks for saving my life 😃🙌
Haha, you’re welcome 🙂

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?