How to get image URL from content array in Sanity query?

16 replies
Last updated: Nov 29, 2025
Howdy. This might be a dumb question but what's the best way to get the url of an image that is returned inside a content array? Right now all I can see from the return is this:

2: Object { _key: "00af18309329", _type: "image", asset: {…} }

_key: "00af18309329"

_type: "image"

asset: Object { _ref: "image-efd90ba5e6942927a7bb04b72e8b7bb74494dd63-4896x3264-jpg", _type: "reference" }

_ref: "image-efd90ba5e6942927a7bb04b72e8b7bb74494dd63-4896x3264-jpg"

_type: "reference"

<prototype>: Object { … }

<prototype>: Object { … }
How would I add the Image Url to this return?

Here are my queries:



const entryFields = groq`
  _id,
  title,
  date,
  excerpt,
  content,
  topics,
  "slug": slug.current,
`

export const indexQuery = groq`
*[_type == "entry"] | order(date desc, _updatedAt desc) {
  ${entryFields}
}`
Thanks!
AI Update

You need to "dereference" the asset reference in your GROQ query to get the full image URL. Right now you're just getting the reference object, not the actual asset data.

Here's how to fix your query:

*[_type == "entry"] | order(date desc, _updatedAt desc) {
  _id,
  title,
  date,
  excerpt,
  content[] {
    ...,
    _type == "image" => {
      ...,
      asset->
    }
  },
  topics,
  "slug": slug.current,
}

The key part is asset-> - the -> operator follows the reference and expands it to include the full asset document, which contains the url field.

Alternatively, you can be more explicit and just grab the URL directly:

*[_type == "entry"] | order(date desc, _updatedAt desc) {
  _id,
  title,
  date,
  excerpt,
  content[] {
    ...,
    _type == "image" => {
      ...,
      "url": asset->url
    }
  },
  topics,
  "slug": slug.current,
}

This will add a url field to your image objects in the content array that contains the full CDN URL like https://cdn.sanity.io/images/....

If you want to use Sanity's image transformation features (recommended for optimized delivery), you can use the @sanity/image-url package on the frontend. But getting the URL directly like this works too if you just need the base image URL.

Show original thread
16 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.

Was this answer helpful?