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

How to auto-publish documents created via Sanity client in Node.js?

10 repliesLast updated: Dec 2, 2025

Hi! I already asked this question without answer, and it seem to have been buried so I ask it again.
https://sanity-io-land.slack.com/archives/C01T1B5GVEG/p1624550975405600
I have created a Node (Javascript) script to input data into my dataset with the sanity client.

I create my client like this (with real values):

const sanityClient = require('@sanity/client')
const client = sanityClient({
  projectId: 'your-project-id',
  dataset: 'bikeshop',
  apiVersion: '2019-01-29', // use current UTC date - see "specifying API version"!
  token: 'sanity-auth-token', // or leave blank for unauthenticated usage
  useCdn: true, // `false` if you want to ensure fresh data
})
Then I create a matching javascript object and call


client.create(obj);
All works fine and the document is created but it is not published. (this is an issue because I need to create a few hundred documents so I dont want to manually publish them).
AI Update

I understand your frustration! The behavior you're experiencing is actually expected - when you use client.create() without specifying an _id, Sanity auto-generates one as a UUID, and this creates what Sanity considers a published document (no drafts. prefix in the ID).

Understanding Draft vs Published

The key thing to understand is that in Sanity, whether a document is "draft" or "published" is determined entirely by its _id:

According to the Perspectives documentation and Published Documents documentation, when you create a document without the drafts. prefix, it's already published.

Why You Might Think They're Not Published

The confusion likely comes from how you're viewing the documents. Here's what might be happening:

Verify Your Documents Are Actually Published

Run this check to see what's really in your dataset:

const sanityClient = require('@sanity/client')
const client = sanityClient({
  projectId: 'your-project-id',
  dataset: 'bikeshop',
  apiVersion: '2021-06-24', // Use a current date
  token: 'sanity-auth-token',
  useCdn: false // Important for accurate results
})

// Check for published documents (no drafts. prefix)
const published = await client.fetch('*[_type == "yourType" && !(_id in path("drafts.**"))]')
console.log('Published documents:', published.map(d => d._id))

// Check for draft documents (with drafts. prefix)
const drafts = await client.fetch('*[_type == "yourType" && _id in path("drafts.**")]')
console.log('Draft documents:', drafts.map(d => d._id))

Your Code Should Already Work

Your current approach should create published documents automatically:

const sanityClient = require('@sanity/client')
const client = sanityClient({
  projectId: 'your-project-id',
  dataset: 'bikeshop',
  apiVersion: '2021-06-24', // Update to a current date
  token: 'sanity-auth-token',
  useCdn: false // Use false for write operations
})

// This creates a PUBLISHED document (auto-generated ID without drafts. prefix)
await client.create({
  _type: 'yourDocumentType',
  title: 'My Document',
  // your other fields
})

According to the Document ID documentation, when you don't specify an _id, Sanity auto-generates a UUID, and these are created as published documents by default.

Important Configuration Changes

Make these adjustments to your client setup:

If Documents Really Aren't Showing

If you run the verification query above and find your documents ARE published (no drafts. prefix) but still aren't appearing where you expect:

The documents created by client.create() without an explicit _id are already published and ready to use - no manual publishing step needed!

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