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

How to import block content with images using @sanity/block-tools?

5 repliesLast updated: Nov 29, 2025

Hey there everyone,
I'm curious if anyone can offer some guidance on importing block content that contains images. I have already used the
@sanity/block-tools
to import the content, but would like all images to also be imported into Sanity as well.
I have already been using the
_sanityAsset
helper elsewhere, but I'm not certain how to format the rules so that Sanity gets the structure correct. Some pointers would be very appreciated!
AI Update

Great question! When importing block content with images using @sanity/block-tools, you'll need to set up your deserialize rules to handle image elements and use the _sanityAsset helper to tell Sanity to fetch and import those images during the import process.

Here's the key pattern that works well:

'rules': [
  {
    deserialize(el, next, block) {
      if (el.tagName?.toLowerCase() === 'img') {
        return block({
          _type: 'image',
          _sanityAsset: `image@${el.src}`,
          nodes: next(el.childNodes),
        })
      }
    }
  }
]

The magic happens with _sanityAsset: 'image@${el.src}' - this tells Sanity's import process to fetch the image from the URL in el.src and create a proper Sanity asset during import.

Complete workflow

Your block content schema should look something like this:

{
  type: 'array',
  name: 'content',
  title: 'Content',
  of: [
    {
      type: 'block',
    },
    {
      type: 'image',
      fields: [
        {
          type: 'string',
          name: 'alt',
          title: 'Alt',
          description: 'The alt text of the image',
        },
      ],
    },
  ],
}

Then when querying the content, you can access the imported images like this:

content[] {
  ...,
  _type == "image" => {
    "src": asset->url,
    alt,
  },
}

Important notes

The _sanityAsset helper works with the format image@<url> for images and file@<url> for other file types, making it straightforward to import various asset types into your Sanity dataset.

Show original thread
5 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