GROQ estimatedReadingTime query fails in Next.js API route but works in getStaticProps
This is a classic API versioning issue! The pt::text() function was introduced in a later API version, so it's not available if you're using an older apiVersion (or no version at all, which defaults to v1).
The reason it works in getStaticProps but not in your API route is likely because you have different Sanity client configurations between the two. One is probably using a newer apiVersion while the other isn't.
The fix: Make sure your Sanity client in your API route specifies a recent API version. The pt::text() function was introduced in the v2021-03-25 API version, so you need at least that version or newer.
Update your client configuration in your API route:
import {createClient} from '@sanity/client'
const client = createClient({
projectId: 'your-project-id',
dataset: 'your-dataset',
apiVersion: '2021-03-25', // or any later date like '2023-05-03'
useCdn: false, // typically false for API routes
})If you're using next-sanity, make sure both your client instances use the same API version:
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: '2021-03-25', // Add this!
useCdn: true,
})Why this happens: Sanity uses date-based API versioning to maintain backward compatibility. The pt::text() function is part of the pt namespace (Portable Text namespace) which was added in later API versions. Without specifying an apiVersion, clients default to v1, which doesn't include this function.
Check both your getStaticProps client and your API route client configurations, and make sure they both have the same (recent) apiVersion specified. That should resolve your "expected ')' following function arguments" error!
Show original thread2 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.