Unlock seamless workflows and faster delivery with our latest releases – get the details

Migrating Initial Value Templates

Initial Value Templates allows you to set up templates for what initial values new documents are created with. These values are only applied when you create a new document in the Studio – the values are not retroactively applied to existing documents or when documents are created from the client libraries.

In the new Config API, initial value templates are declared on the schema.templates property which accepts either a static array of template objects or a callback function that returns the same. The callback function is invoked with the current value as its first argument, and a configuration context object as its second.

This is how you would set up Initial Value Templates in v2:

// sanity.json
{
  "root": true,
  "project": {
    "name": "My Project"
  },
  // ...
  "parts": [
    {
      "name": "part:@sanity/base/initial-value-templates",
      "path": "./initialValueTemplates.js"
    }
  ]
}

// ./initialValueTemplates.js
import T from '@sanity/base/initial-value-template-builder'

export default [
  T.template({
    id: 'category-child',
    title: 'Category: Child',
    schemaType: 'category',
    parameters: [{name: `parentId`, title: `Parent ID`, type: `string`}],
    value: ({parentId}) => ({
      parent: {_type: 'reference', _ref: parentId},
    }),
  }),
  T.template({
    id: 'article-with-author',
    title: 'Article: Author',
    schemaType: 'article',
    parameters: [{name: `authorId`, title: `Author ID`, type: `string`}],
    value: ({authorId}) => ({
      author: authorId,
    }),
  }),
  ...T.defaults(),
]

And here is how to achieve it in v3:

// sanity.config.ts
import {defineConfig} from 'sanity'

export default defineConfig({
  name: 'default',
  title: 'My Cool Project',
  projectId: 'my-project-id',
  dataset: 'production',
  schema: {
    templates: (prev) => [
		  // NOTE: the builder is gone
		  {
		    id: 'category-child',
		    title: 'Category: Child',
		    schemaType: 'category',
		    parameters: [{name: `parentId`, title: `Parent ID`, type: `string`}],
		    value: ({parentId}) => ({
		      parent: {_type: 'reference', _ref: parentId},
		    }),
		  },
		  {
		    id: 'article-with-author',
		    title: 'Article: Author',
		    schemaType: 'article',
		    parameters: [{name: `authorId`, title: `Author ID`, type: `string`}],
		    value: ({authorId}) => ({
		      author: authorId,
		    }),
		  },
		  ...prev, // (same as `T.defaults()`)
		]
  },
})

// sanity.config.ts
import {defineConfig} from 'sanity'
import {templates} from './schema/templates'

export default defineConfig({
  name: 'default',
  title: 'My Cool Project',
  projectId: 'my-project-id',
  dataset: 'production',
  schema: {
		// ALTERNATIVE: if you'd only like to augment your
		// templates to the end, then you don't need the function
    templates: [
		  // NOTE: the builder is gone
		  {
		    id: 'category-child',
		    title: 'Category: Child',
		    schemaType: 'category',
		    parameters: [{name: `parentId`, title: `Parent ID`, type: `string`}],
		    value: ({parentId}) => ({
		      parent: {_type: 'reference', _ref: parentId},
		    }),
		  },
		  {
		    id: 'article-with-author',
		    title: 'Article: Author',
		    schemaType: 'article',
		    parameters: [{name: `authorId`, title: `Author ID`, type: `string`}],
		    value: ({authorId}) => ({
		      author: authorId,
		    }),
		  },
		]
  },
})

Feedback or questions?

These guides are under development. If you have questions or suggestions for improvement, then we want to hear from you. Get in touch via our GitHub discussions page.

Was this article helpful?