GROQ estimatedReadingTime query fails in Next.js API route but works in getStaticProps

2 replies
Last updated: Jul 4, 2022
Hello, i’m facing a pretty strange issue, and i have no idea how i can solf it.Its in a NextJS Website.

I use “estimatedReadingTime”: round(length(pt::text(body)) / 5 / 180 ),
in a query, to get as the field says the estimated reading time.

When i use this query in getStaticProps, everything works just fine and i get the estimated Reading time.
But when i use the exact same query in a API route (NextJS /api/..some endpoint) i get an error.

When i remove only this line for estimatedReadingTime, the query works, so the query and fetching works fine in the API but the calculation von the readingtime give me an error.

The error is: „queryParseError”, expected ‘)’ following function arguments

Does anyone know what could be wrong here?

Thanks
AI Update

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 thread
2 replies
Are you using the same API-version both places?
Oh man! Thousend thanks, i didn’t set any api version, and after setting the api version for the APi endpoint, it works. Thanks

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?