GROQ query error: Unknown query parameter "slug.current" in client code

11 replies
Last updated: Apr 28, 2021
Hello folks! πŸ‘‹ I've finally had the chance to try Sanity out. GROQ seems really powerful but i'm having a strange issue with it.
When using the simple query *[ _type == "post" && slug.current == "${slug}"] i get the following error:

error: {
description: 'Unknown query parameter "slug.current"',
names: [ 'slug.current' ],
type: 'httpUnknownQueryStringParameters'
}

I've double checked in Vision that the query does work over there and that this is the correct key. Using just [ _type == "post"] works fine.

Any guidance in the right direction is very much appreciated, thank you!
AI Update

The error you're seeing is actually about how you're executing the query, not the GROQ syntax itself! The httpUnknownQueryStringParameters error means you're accidentally passing slug.current as an HTTP query parameter instead of as a proper GROQ parameter.

The issue is in your query string - you're using JavaScript template literals (${slug}) which is trying to interpolate the value directly into the query string. When this gets sent to Sanity's API, it's being interpreted incorrectly.

The Problem

Your current query:

*[_type == "post" && slug.current == "${slug}"]

This is trying to do string interpolation, which doesn't work properly with GROQ and can cause the API to misinterpret parts of your query as HTTP parameters.

The Solution

Use GROQ parameters instead - they're safer, cleaner, and the recommended approach according to the GROQ parameters documentation:

const query = '*[_type == "post" && slug.current == $slug][0]'
const params = { slug: slug }

const result = await sanityClient.fetch(query, params)

Notice:

  • Use $slug in the query (with the $ prefix)
  • Pass the actual value as a separate params object as the second argument to fetch()
  • No template literals or string concatenation needed!

Why This Approach is Better

  1. Security: Parameters prevent GROQ injection attacks since they're JSON literals, not arbitrary GROQ expressions
  2. Proper escaping: The client automatically handles special characters like quotes
  3. Cleaner code: No messy string concatenation
  4. Type safety: Values maintain their proper data types

The Vision tool works because you're typing the query directly with the actual value, but when using the client programmatically, you need to use the parameter syntax. This is the same reason your simple *[_type == "post"] query works - it doesn't have any dynamic values that could be misinterpreted!

Show original thread
11 replies
I think you should rewrite it without quotes, like this:
*[ _type == "post" && slug.current == $slug]
Appreciate the answer. It didn't work for me. But I found a solution, I had to encode the query with encodeURIComponent. After this it worked!
Are you sending out a raw query? Or through the API client? I believe the API client would take care of that automatically.
It's a raw query through fetch. I think you might be right.
Ah, that explains it then! And also the difference in syntax (js. interpolation vs. GROQ params passed to the
client.fetch
method).
I'm gonna have to checkout the API client, didn't know it existed. Thanks a lot man!
Appreciate the answer. It didn't work for me. But I found a solution, I had to encode the query with encodeURIComponent. After this it worked!
Are you sending out a raw query? Or through the API client? I believe the API client would take care of that automatically.
It's a raw query through fetch. I think you might be right.
Ah, that explains it then! And also the difference in syntax (js. interpolation vs. GROQ params passed to the
client.fetch
method).

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?