GROQ query to get resolved image URL with width and height in latest API
Your query is almost working correctly! The syntax asset->url does work with the latest API versions - that's the standard way to get image URLs in GROQ. Based on your query and the official documentation on presenting images, here's what you need:
*[_type == 'teamMember' && !(_id in path('drafts.**'))] {
'headshot': {
'url': headshot.image.asset->url,
'alt': headshot.alt,
'width': headshot.image.asset->metadata.dimensions.width,
'height': headshot.image.asset->metadata.dimensions.height
}
}The key is accessing the dimensions through asset->metadata.dimensions - this metadata is automatically extracted when images are uploaded and includes width, height, and aspect ratio.
Important notes about your original query:
- The
asset->urlsyntax does work in current API versions - it's the standard approach shown in Sanity's documentation - The
metadata.dimensionsfields (width, height, aspectRatio) are always included automatically and can't be disabled - If you need the image format/extension, you can add:
'format': headshot.image.asset->extension
Here's a complete example with format included:
*[_type == 'teamMember' && !(_id in path('drafts.**'))] {
'headshot': {
'url': headshot.image.asset->url,
'alt': headshot.alt,
'width': headshot.image.asset->metadata.dimensions.width,
'height': headshot.image.asset->metadata.dimensions.height,
'aspectRatio': headshot.image.asset->metadata.dimensions.aspectRatio,
'format': headshot.image.asset->extension
}
}This approach from the community answer I found shows exactly this pattern working. The image asset documents contain all this metadata by default, so you can query it directly without any special configuration.
If your original V1 query stopped working, it might be due to a different issue (like a schema change or dataset configuration), but the asset->url syntax itself is definitely supported in current API versions!
Show original thread15 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.