Sanity Library Reference Docs
    Preparing search index...

    Function defineDocumentFunction

    • Defines a function that is triggered by document events in Sanity datasets.

      defineDocumentFunction({
      name: 'my-document-function',
      event: {
      on: ['create', 'update'],
      filter: "_type == 'post'",
      projection: "{_id, title, slug}",
      },
      })

      Parameters

      • functionConfig: BlueprintDocumentFunctionConfig

        The configuration for the document function

        Configuration for defining a document function.

        • OptionaldisplayName?: string

          Human-readable display name for the function

        • Optionalenv?: Record<string, string>

          Environment variables provided to the function

        • Optionalevent?: BlueprintDocumentFunctionResourceEvent

          Event configuration specifying when and how the function is triggered

          {on: ['publish']}

        • Optionalmemory?: number

          Memory allocation in gigabytes

        • name: string
        • Optionalproject?: string

          The project ID of the project that contains your function.

          The project attribute must be defined if your blueprint is scoped to an organization.

        • OptionalrobotToken?: string

          Token provided during function invocation

        • Optionalruntime?: "node" | "nodejs22.x" | "nodejs24.x"

          The runtime environment for the function (currently only Node.js is supported)

          'nodejs24.x'
          
        • Optionalsrc?: string

          Path to the function source code

          functions/${name}

        • Optionaltimeout?: number

          Execution timeout in seconds

      Returns BlueprintDocumentFunctionResource

      The validated document function resource