Skip to content
Sanity
    • Platform

      Sanity Studio

      Flexible editing environment

      APIs

      Connect to anything

      Content Lake

      Real-time database

      Try product demo

      Features

      Real-time collaboration

      Fearlessly work with content

      Precise content querying

      Treat content as data with GROQ

      Localization

      Coherent messaging across territories

    • Use cases

      E-commerce

      Richer shopping experiences

      Marketing sites

      Control your story

      Products & services

      Innovate and automate

      Mobile apps

      Content backend for every OS

      View all

      Integrations

      Shopify
      Mux
      Vercel
      Netlify
      Algolia
      Cloudinary
      BigCommerce
      Commerce Layer
      Smartling
      Transifex
      View all
    • Learn

      Documentation
      Studio API Reference
      API reference
      Guides
      GROQ cheat sheet
      Sanity UI
      Get started

      Build and share

      Templates
      Tools and plugins
      Schemas and snippets
      Project showcase
      Share your work
      Browse Exchange

      Frameworks

      React
      Vue
      Next.js
      Nuxt.js
      Svelte
      Remix
      Gatsby
      Astro
      Angular
      Eleventy
      View all
    • Discover

      Blog
      Resource library
      Agency partners
      Become a partner
      Technical support
      Talk to sales

      Case studies

      Puma

      Source of truth for global markets

      Aether

      Unique digital shopping experience

      Morning Brew

      Omnichannel media distribution

      InVision

      Delivering exceptional customer experiences

      View all

      Popular guides

      Headless CMS
      Structured content
      Content modeling
      Headless SEO
      Static websites
      View all
    • Enterprise
    • Pricing
    • Log in
    • Contact sales
    • Get started
Contact salesGet started
Published October 24th 2019

Boost editor workflows with our new Initial Values

Give your editors a head start with Initial Values for Sanity Studio. Prefill fields with minimal configuration, create multiple templates for quick document creation, fetch content from external APIs and put it in new documents.

Even Eidsten Westvang

Sanity.io co-founder and product person

Espen Hovlandsdal

Principal Software Engineer at Sanity

Victoria Bergquist

Victoria is a frontend engineer at Sanity, working mostly on Sanity Studio.

With the newest version of Sanity Studio, you can now set initial values for all your document types. It’s a great way to save your editors time and effort spent on repetitive tasks. Initial Values lets you do things like:

  • Start a new blog post with the current time as the publish date.
  • Make sure that a certain category is set for all new projects in a portfolio.
  • Record your geolocation when you create a new report for your scientific intranet.
  • Create a selection of product types to choose from, with different currencies and shipment methods predefined for your e-commerce platform.
  • Pretty much set any initial value for any field to anything you would want it to be.

To try it out Initial Values, upgrade your Sanity Studio by running this command in the studio folder:

> sanity upgrade

Now you can explore the documentation and the new guide to learn how to get started.

Video: How to set the initial value for a new project document.

This has been a feature we have been looking forward to shipping for quite a while. We hope it will give you the flexibility you need and perhaps some new ideas to make your editor’s day even better. If you come up with something unexpected or extra super useful, we would love to hear about it, either in our community slack, or on Twitter.

How to Initial Values: short version

You can get started with initial values by adding a simple configuration key in your schemas. It can take a simple object using the field names as keys with the initial values. You can also return a function if you want to calculate the initial values with JavaScript. To set the initial value for a post document with a categories array, you can do like this:

export default {
  name: 'post',
  type: 'document',
  title: 'Post',
  initialValue: {
    categories: ['All']
  },
  fields: [
    // ... all the fields
  ]
}

We thought it might be useful to let you return a promise too. For those times you want to get your initial values from other documents, or an external API. Initial values from promises open some pretty powerful workflows. For example, if you want to automatically put the persons in your dataset in a list of members in a new project, you can use the Studio’s client and GROQ to fetch them, and project the data structure you need for those array items:

import client from 'part:@sanity/base/client'

export default {
  name: 'project',
  type: 'document',
  title: 'Project',
  initialValue: async () => ({
    members: await client.fetch(`//groq
      *[_type == "person"]{
        "_type": "projectMember",
        "person": {
          "_ref": _id,
          "_type": "reference"
        }
      }
    `)
  }),
  fields: [ 
    //... all the fields
  ]
}

Here's a video of how that works in practice, also showing how you can extend the GROQ filter if you want more specific results:

Turn it up to 11 with Initial Value Templates

With the Initial Value Templates Builder, you can make sets of configurations that your editors can choose from. Let's say your company uses Sanity for a list of employees with different roles. A basic way to use templates is to iterate over these roles, and generate different templates for the different roles:

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

const roles = [
  {name: 'developer', title: 'Developer'},
  {name: 'designer', title: 'Designer'},
  {name: 'admin', title: 'Administrator'},
  {name: 'manager', title: 'Manager'}
]

export default [
  ...T.defaults(),
  ...roles.map(role => 
    T.template({
      id: `personRole-${role.name}`,
      title: role.title,
      schemaType: 'person',
      value: {
        role: role.name
      }
    })
  )
]

This configuration will then add these choices to the “New document” menu:

We have just scratched the surface of what Initial Values can do in this blog post, explore the documentation to learn about more advanced use cases.

Page content

  • How to Initial Values: short version
  • Turn it up to 11 with Initial Value Templates

Product

Sanity StudioAPIsContent LakeSecurity & Compliance
  • Sanity vs Contentful
  • Sanity vs Strapi
  • Sanity vs Wordpress
  • Sanity vs Adobe Experience Manager
  • Sanity vs Hygraph
  • Sanity vs Sitecore
  • Sanity vs Storyblok
  • Sanity vs Contentstack
  • Sanity vs Prismic
  • Sanity vs Drupal
  • Sanity vs ButterCMS

Resources

DocumentationBlogResource libraryCase Studies
  • React Blog
  • Gatsby Blog
  • Next.js Landing Pages
  • Making a PWA
  • Single Page Application
  • Svelte & Typescript App
  • Vue & Tailwind Blog
  • Developer Portfolio Templates
  • Form validation with Yup
  • Live Preview with Next.js and Sanity.io
  • Next.js blog
  • Next.js personal website
  • Clean Next.js + Sanity app
  • Clean Remix + Sanity app
  • Clean SvelteKit + Sanity app
  • All Templates
  • Agency partners
  • Technology partners
  • Headless CMS 101
  • Static Sites 101
  • Headless Commerce 101
  • CMS for enterprise
  • Headless SEO
  • Localization
  • Content as a Service
  • What is a DXP?
  • Typescript 101
  • Ecommerce SEO
  • What is a Composable DXP?
  • What is an API?
  • GraphQL vs REST
  • React CMS
  • Next.JS CMS
  • CMS for Shopify
  • API-first CMS
  • Content platform
  • Multilingual CMS
  • Static Site CMS
  • Gatsby CMS
  • Node CMS
  • E-commerce CMS
  • Vue CMS
  • Angular CMS
  • GraphQL CMS
  • Newspaper CMS
  • Magazine CMS
  • CMS for apps
  • Remix CMS
  • Nuxt CMS
  • SvelteKit CMS
  • Agile CMS
  • Eleventy CMS

Company

Contact SalesEnterpriseCareersTerms of ServicePrivacy PolicyAccessibility Statement

Stay connected

  • GitHub
  • Slack
  • Twitter
  • YouTube
  • Stack Overflow
  • Blog RSS
  • Newsletter
©Sanity 2023