How to set initialValue for a field in Sanity schema?
Based on the Sanity documentation, you're experiencing a common issue with initialValue in Sanity Studio. Here's what's likely happening and how to fix it:
The Most Common Issue: Existing Documents
Field-level initialValue only works when creating NEW documents. If you've already created the document (even as a draft), the initial value won't populate. This is the most frequent reason developers think initialValue isn't working.
Solution: Delete any existing drafts/documents and create a completely new document to see the initial value.
Your Code Looks Correct
The example you're using is valid for Sanity Studio v3:
export default {
name: 'project',
type: 'document',
title: 'Project',
fields: [
{
name: 'title',
title: 'Title',
type: 'string',
initialValue: 'This string'
}
]
}Better Approach: Initial Value Templates
For Sanity Studio v3, the recommended approach is using Initial Value Templates instead of field-level initialValue. These are more powerful and reliable:
// sanity.config.ts
export default defineConfig({
// ... other config
document: {
newDocumentOptions: (prev, context) => {
return [
...prev,
{
id: 'project-with-title',
title: 'Project',
schemaType: 'project',
value: {
title: 'This string'
}
}
]
}
}
})Additional Troubleshooting
- Use
defineFieldhelper for better type safety:
import {defineField, defineType} from 'sanity'
export default defineType({
name: 'project',
type: 'document',
fields: [
defineField({
name: 'title',
type: 'string',
initialValue: 'This string'
})
]
})- Clear cache: Clear browser cache/local storage and restart your dev server
- Verify Studio version: Make sure you're on Studio v3 (check
package.json) - Check console: Look for any errors in the browser developer console
The key takeaway: initialValue at the field level does work in Sanity v3, but only for brand new documents. For more control and reliability, Initial Value Templates are the modern recommended approach.
Show original thread2 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.