Is there any way to set a default value in a document field?
Yes! Sanity provides two main ways to set default values in document fields:
1. Field-Level Initial Values (Simple Defaults)
For straightforward default values, you can use the initialValue property directly in your schema field definition:
defineField({
name: 'approved',
type: 'boolean',
initialValue: true
})This works great for static defaults that should apply to all new documents of that type.
2. Initial Value Templates (Dynamic Defaults)
For more complex scenarios, Sanity has a powerful Initial Value Templates feature that lets you:
- Set dynamic values based on context
- Create multiple creation options for the same document type
- Access current user information
- Fetch data from APIs or existing documents
- Pass parameters from the creation flow
Here's an example that sets the author field to the current user:
// sanity.config.ts
export default defineConfig({
document: {
newDocumentOptions: (prev, context) => {
return [
...prev,
{
id: 'article-with-author',
title: 'New Article',
schemaType: 'article',
value: async (params, context) => {
const {currentUser} = context
return {
author: {
_type: 'reference',
_ref: currentUser.id
},
publishedAt: new Date().toISOString()
}
}
}
]
}
}
})Initial Value Templates are especially useful for parent-child relationships, where you want to automatically set a reference to a parent document when creating a child from a specific context.
The main difference: use initialValue for simple static defaults, and Initial Value Templates when you need dynamic values, user context, or multiple creation options for the same document type.
Show original thread5 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.