Joint session with Vercel: How to build intelligent storefronts (May 15th)

Adding HTML to Sanity document via client.create()

24 repliesLast updated: Nov 29, 2025

Does Sanity have any documentations adding HTML to a document via the
client.create()
function?
AI Update

While Sanity doesn't have specific documentation about adding HTML directly to documents via client.create(), the recommended approach is to convert HTML to Portable Text format first using the @sanity/block-tools package.

Converting HTML to Portable Text

When you need to add HTML content to a Sanity document, you should:

npm install @sanity/block-tools jsdom
const {htmlToBlocks} = require('@sanity/block-tools')
const {JSDOM} = require('jsdom')

const html = '<p>Your HTML content here</p>'
const blocks = htmlToBlocks(html, blockContentType, {
  parseHtml: (html) => new JSDOM(html).window.document
})
const doc = {
  _type: 'yourDocumentType',
  title: 'Document Title',
  body: blocks  // Your converted Portable Text
}

client.create(doc).then((res) => {
  console.log(`Document created with ID: ${res._id}`)
})

Why Portable Text Instead of HTML?

Sanity uses Portable Text as its structured content format rather than raw HTML. This provides several benefits:

About client.create()

The client.create() method expects a plain JavaScript object representing the document. It must contain a _type attribute and may optionally contain an _id. For rich text content fields, that content should be in Portable Text format rather than raw HTML.

const doc = {
  _type: 'bike',
  name: 'Sanity Tandem Extraordinaire',
  seats: 2,
}

client.create(doc).then((res) => {
  console.log(`Bike was created, document ID is ${res._id}`)
})

Resources for HTML to Portable Text Conversion

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