How AI is powering better personalization in e-commerce [with Vercel]. Sign up now

How to attach uploaded image asset to document field using patch?

6 repliesLast updated: Nov 30, 2025

I’m having some trouble adding an image to a document using the the
client
. I am successfully uploading the image asset, but Im not sure how to attach that to the field I want in the document. My current attempt is using the
.patch
functionality and seems like it should be working, but its encountering an error `Content has invalid type:
sanity.imageAsset
I’ll post code and full error in thread.
AI Update

I see what's happening! The error Content has invalid type: sanity.imageAsset indicates you're trying to set the image field directly to the asset reference, but Sanity's image field expects a specific structure.

When you upload an image asset successfully, you need to structure the image field as an object that contains the asset reference, not just pass the asset reference directly. Here's the correct structure:

// After uploading your image asset
const uploadedAsset = await client.assets.upload('image', fileBuffer, {
  filename: 'my-image.jpg'
})

// Correct way to patch the image field
await client
  .patch(documentId)
  .set({
    myImageField: {
      _type: 'image',
      asset: {
        _type: 'reference',
        _ref: uploadedAsset._id
      }
    }
  })
  .commit()

Key points:

Common mistake (what's likely causing your error):

// ❌ WRONG - This causes "invalid type: sanity.imageAsset"
.set({
  myImageField: {
    _ref: uploadedAsset._id,
    _type: 'reference'  // or 'sanity.imageAsset'
  }
})

The image type documentation explains that when an asset is uploaded, "an asset metadata document is created, and a reference to that document is added to the asset field within the image field." This nested structure is what Sanity expects.

If you're also setting crop/hotspot data or custom fields (like captions), you'd include those at the same level as asset:

.set({
  myImageField: {
    _type: 'image',
    asset: {
      _type: 'reference',
      _ref: uploadedAsset._id
    },
    caption: 'My image caption',  // custom field if defined in schema
    hotspot: { /* hotspot data */ }  // if needed
  }
})
Show original thread
6 replies

Was this answer helpful?

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.

Related contributions