Sanity Pioneers: Get early access to betas, extra AI credits, and a direct line to the engineering team. Apply now

Why doesn't client.fetch() work in Next.js 13 app directory?

3 repliesLast updated: Dec 1, 2025

I am trying to get Sanity running in the new app/page.js directory of Nextjs13. Why is client.fetch() not working here (where fetching jsonplaceholder does)? Please help.
AI Update

This is a common issue when using Sanity with Next.js 13's app directory! The problem is likely related to how you're configuring your Sanity client. Let me walk through the most common causes and solutions.

Common Causes

1. CORS Configuration (Most Likely for Client Components)

If you're using client.fetch() in a client component (marked with 'use client'), you need to add your localhost origin to your Sanity project's CORS settings. The reason jsonplaceholder works is because their API allows all origins, but Sanity requires explicit domain whitelisting for browser-based requests.

To fix this:

This is covered in the CORS Origins documentation.

2. Dataset Privacy Settings

Your dataset might be set to private, which requires an API token for read access. Check your dataset settings at manage.sanity.io. If it's private, you'll need to:

import { createClient } from 'next-sanity'

export const client = createClient({
  projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,
  dataset: process.env.NEXT_PUBLIC_SANITY_DATASET,
  apiVersion: '2024-01-01',
  useCdn: true,
  token: process.env.SANITY_API_TOKEN, // Only needed if dataset is private
})

Note: If your dataset is public (which is common for production websites), you don't need a token for read operations.

3. Incorrect Client Configuration

Make sure your client is properly configured. For Next.js 13+ app directory with Server Components (the default), you should use the next-sanity package:

npm i next-sanity

Then create a sanity/client.ts file:

import { createClient } from 'next-sanity'

export const client = createClient({
  projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID!,
  dataset: process.env.NEXT_PUBLIC_SANITY_DATASET!,
  apiVersion: '2024-01-01', // Use current date
  useCdn: true, // Set to false if you want fresh data
})

4. Using sanityFetch (Recommended)

For better caching and performance in Next.js 13+ app directory, consider using the sanityFetch helper from next-sanity instead of client.fetch(). It provides automatic cache configuration and works seamlessly with React Server Components:

import { sanityFetch } from './sanity/client'

const posts = await sanityFetch({
  query: `*[_type == "post"]`,
  tags: ['post'], // For cache revalidation
})

Debugging Steps

The key difference from jsonplaceholder is that Sanity has security measures in place - CORS restrictions for browser requests and optional authentication for private datasets. Once you configure these properly, client.fetch() will work just as reliably!

Show original thread
3 replies

Was this answer helpful?

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.

Related contributions