Troubleshooting returning image URLs in Next.js using Sanity.io

25 replies
Last updated: Jul 21, 2020
Can someone point me to the docs where you return a url for an image?I keep seeing the url param returned in sample objects - but not seeing it on my end - what am I missing?

For my images: I have an
asset
property - which is where I would assume the
url
would be. However I am only getting a
_ref
and a
type
.
AI Update

Ah, I see what you're experiencing! The url property you're seeing in sample code is not automatically returned when you query an image field - you need to explicitly request it by dereferencing the asset reference.

The Issue

When you query an image field in Sanity, you get an asset object with just _ref and _type because that's how Sanity stores image references. The actual image data (including the url) lives in a separate asset document that you need to dereference.

The Solution

To get the image URL in your GROQ query, you need to use the dereference operator (->) to follow the asset reference:

*[_type == "yourDocumentType"]{
  title,
  "imageUrl": image.asset->url
}

For example, if you have a movie document with a poster image:

*[_type == "movie"]{
  title,
  "posterUrl": poster.asset->url
}

For Arrays of Images

If you have an array of images, use the array traversal syntax with []:

*[_type == "movie"]{
  title,
  "imageUrls": images[].asset->url
}

This returns a simple array of URL strings.

Using @sanity/image-url

Alternatively, you can use the @sanity/image-url helper library in your frontend code. This is actually the recommended approach because it gives you access to Sanity's image transformation pipeline:

import imageUrlBuilder from '@sanity/image-url'

const builder = imageUrlBuilder(client)

function urlFor(source) {
  return builder.image(source)
}

// Then use it with your image asset reference:
const imageUrl = urlFor(movie.poster).width(800).url()

With this approach, you just query the image field normally (with the _ref), and construct the URLs with transformations in your frontend.

The key takeaway: the url property requires dereferencing the asset with -> in your GROQ query, or you can construct URLs in your frontend using the @sanity/image-url helper library. The docs you're looking for are the How Queries Work guide (specifically the section on dereferencing) and the Image URLs documentation for the @sanity/image-url package.

where are you trying to return the URL?
in sanity? gatsby? something else?
user N
Thanks for asking for more context.
Returning the URL in a nextjs frontend using sanity groq query.
mmm, ok, i've not used nextjs but let me see if i can't still help you out
Sweet thanks. I saw there are a few examples of image packages used for gatsby and such - but thats a little overkill for what I am looking for.
Specifically referencing this:

https://www.sanity.io/docs/image-type#example-of-a-image-asset-object-with-metadata-location-lqip-palette-and-dimensions-df3f768ce073
Returns a
url
but no reference on how to do so or where it comes from.
Ooh i think im seeing where - you drop the
metadata
into your
options
call and specify what you want to return. I’ll try this.
Hmm - not that. Url seems to live outside of metadata somewhere!
you've seen this?
you might need to use the
@sanity/image-url
plugin
not 100% sure about that
Yea that was step 2. 🙂
I’ll dig in a bit more after dinner - thanks for the help so far!
image.asset->url
- asset does not return url as a data point: only getting
_ref
and
type
back 🤷‍♂️
*[_type == 'person']{
  name,
  "imageUrl": image.asset->url
}
seems relevant
haha, ok, you saw that
Yup 🙂 im just racking my brains on why its missing - it seems like a standard return based on the docs
i think it would be something like
your-field-name.asset->url
In the interm i could build up the string using interpolation and the
_ref
item 👍
have you tried
mainImage.asset->url
?
is
mainImage
a field or an object?
Yea I tried that - errors out.
user N
I think it was a syntax issue from the docs.
Ended up running
mainImage.asset->{url}
and got what I needed.
Also had to re read the groq docs concerning references 🙂 - huge fan of all the sweet operators you can throw in there to return data.
oh nice, glad its working ... did you drop a note in the feedback channel so the sanity team can update the docs?
I did not - will do that now.

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?