Sanity imageBuilder urlFor returns undefined despite having assetId and type

24 replies
Last updated: Jan 28, 2026
Hi there, I have implemented sanity.io in my project and I am having trouble trying tpo upload images in it. Specifically, I am using the imageBuilder in order to upload them but it looks like it's not working as when I am using the urlFor function in my component, it says that the data pulled it's undefined. When I have figure it out, I have tried to check if I was pulling the data in the right way and evntually I was doing it correctly as i could get the assetId and the type.
AI Update

It sounds like you're running into a common issue with image references in Sanity! The problem is likely that urlFor() is receiving an undefined value even though you can see the _type and asset reference when logging your data.

Here are the most common causes and solutions:

1. Passing the wrong part of the image object

When you query an image field from Sanity, you get an object with properties like _type, asset, hotspot, and crop. You need to pass the entire image object to urlFor(), not just the asset reference.

// ❌ Wrong - passing just the asset reference
urlFor(post.mainImage.asset)

// ✅ Correct - pass the entire image object
urlFor(post.mainImage)

2. Your GROQ query might not be fetching the image data correctly

Make sure your query includes the image field properly. Here's a good pattern:

// Basic query
const query = `*[_type == "post"][0] {
  title,
  mainImage
}`

// Or with expanded asset reference (optional but helpful for debugging)
const query = `*[_type == "post"][0] {
  title,
  mainImage {
    asset->,
    hotspot,
    crop
  }
}`

3. Check your urlFor setup

Make sure your urlFor helper is configured correctly with your project ID and dataset as shown in the @sanity/image-url documentation:

import imageUrlBuilder from '@sanity/image-url'
import { client } from './client' // your sanity client

const builder = imageUrlBuilder(client)

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

4. Handle null/undefined values

Add a safety check in your component:

{post.mainImage && (
  <img src={urlFor(post.mainImage).width(800).url()} alt={post.title} />
)}

Debugging tip: Log the entire data structure you're receiving:

console.log('Full post data:', JSON.stringify(post, null, 2))
console.log('Image field:', post.mainImage)

Since you mentioned you can see the _type and assetId, you're definitely getting data back - you just need to make sure you're passing the complete image object (not drilling down to .asset) to the urlFor() function. The image type stores asset references along with metadata like hotspot and crop information, and the URL builder needs the full object to work properly.

Show original thread
24 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?