
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeI can help you with that! While Sanity's documentation focuses heavily on GROQ (which is their native query language), you can absolutely use GraphQL with Next.js. Here's what you need to know:
First, you need to deploy a GraphQL API for your Sanity project. Run this command in your Sanity project directory:
sanity graphql deployThis creates a GraphQL endpoint for your dataset.
Once deployed, your GraphQL endpoint will be:
https://<projectId>.api.sanity.io/v1/graphql/<dataset>/default
Replace <projectId> with your actual project ID and <dataset> with your dataset name (usually "production").
Here's how to query your Sanity GraphQL API using fetch in Next.js:
async function fetchSanityData() {
const query = `
query {
allPost {
title
slug {
current
}
}
}
`;
const response = await fetch(
`https://${process.env.NEXT_PUBLIC_SANITY_PROJECT_ID}.api.sanity.io/v1/graphql/${process.env.NEXT_PUBLIC_SANITY_DATASET}/default`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
// Only needed if your dataset is private:
// 'Authorization': `Bearer ${process.env.SANITY_API_TOKEN}`,
},
body: JSON.stringify({ query }),
}
);
const { data } = await response.json();
return data;
}For a better GraphQL experience in Next.js, consider using Apollo Client with Sanity:
import { ApolloClient, InMemoryCache, gql } from '@apollo/client';
const client = new ApolloClient({
uri: `https://${process.env.NEXT_PUBLIC_SANITY_PROJECT_ID}.api.sanity.io/v1/graphql/${process.env.NEXT_PUBLIC_SANITY_DATASET}/default`,
cache: new InMemoryCache(),
});
// Then in your component or API route:
const { data } = await client.query({
query: gql`
query {
allPost {
title
}
}
`,
});Hope this gets you unstuck! Let me know if you hit any other issues.
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.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store