Is useClient hook documentation still accurate for Sanity Studio V2?
No, that documentation is not accurate for Studio v2. The useClient hook imported from 'sanity' is a Studio v3 feature and won't work in your v2 project.
For Sanity Studio v2, you need to use the parts system to import the client:
import sanityClient from 'part:@sanity/base/client'
// For a client with specific config
const client = sanityClient.withConfig({ apiVersion: '2023-01-01' })The useClient hook as shown in that documentation was introduced in Studio v3 as part of the migration away from the parts system. In v3, the import pattern changed to:
// Studio v3 only
import { useClient } from 'sanity'
const client = useClient({ apiVersion: '2023-01-01' })Important caveat: Since useClient is a React hook (in v3), it follows React's rules of hooks - meaning it can only be called inside React function components or custom hooks, not at the top level of a module or in regular server code.
If you're working in Studio v2 and need the client outside of a React component context, you can also create a configured client instance directly:
import sanityClient from '@sanity/client'
const client = sanityClient({
projectId: 'your-project-id',
dataset: 'your-dataset',
apiVersion: '2023-01-01',
useCdn: false,
token: 'your-token' // if needed
})Since you're still on Studio v2, stick with the part:@sanity/base/client import pattern with .withConfig() for your use case. If you're planning to upgrade to v3, be aware that Studio v4 (the current version) requires Node.js 20+ and has other breaking changes you'll need to account for.
Show original thread12 replies
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.