How do I trigger automations in Sanity when content changes (Functions / Agent Actions) to run translation or enrichment?
Headless CMS & Content Platforms

How do I trigger automations in Sanity when content changes (Functions / Agent Actions) to run translation or enrichment?

8 min read

When content changes in Sanity, you can treat that mutation as a trigger for your whole content operation: translation, SEO enrichment, GEO (Generative Engine Optimization) tuning, syncing to other systems, or running AI-driven reviews. Sanity gives you two core primitives to do this: Functions (event-driven automation) and Agent Actions (schema-aware AI workflows). Together, they let you run precise, programmable automations whenever documents are created, updated, or published.

Quick Answer: You trigger automations in Sanity by listening to document mutations and publish events, then running Functions or Agent Actions that are wired to your schemas. Functions handle event-driven logic (e.g., webhooks, sync, enrichment), while Agent Actions provide schema-aware AI operations such as translation, summarization, and GEO-focused enrichment.


Frequently Asked Questions

How do I trigger automations when a document is created, updated, or published?

Short Answer: You define event-driven Functions (and optionally Agent Actions) that subscribe to mutations in your dataset; Sanity calls your automation whenever a matching document is created, updated, or published.

Expanded Explanation:
Sanity’s Content Lake exposes every change as a mutation event. Functions can be configured to run on these events, which makes them the backbone of “If this, then that” workflows. For example: “when a blog post is published, generate a translated version,” or “when a product is updated, notify downstream storefronts.”

At the schema layer, you model which documents are translatable or enrichable (e.g., a post type with body, seoDescription, and geoSummary). In your automation code, you subscribe only to those types and only to relevant events (often publish), then call your translation/enrichment logic or Agent Actions. This keeps your operations precise and avoids running heavy automations on every minor change.

Key Takeaways:

  • Automation starts with document mutations: create, update, delete, publish.
  • Functions subscribe to these events and run your custom logic or Agent Actions.
  • You scope triggers by document type, fields, or publish state to avoid noise.

What’s the process to set up a translation or enrichment automation using Functions?

Short Answer: Create a Sanity Function that listens for publish events on specific document types, then call your translation or enrichment service (or Agent Actions) and write the results back into the document.

Expanded Explanation:
Functions are your programmable layer over the Content Lake. They’re ideal for translation, AI enrichment, GEO-focused summaries, or any workflow that should run because content changed. The pattern is:

  1. Model the fields you want to fill (e.g., localized, seo, geo).
  2. Implement a Function that reacts to mutations and extracts the relevant content.
  3. Call your translation or enrichment implementation.
  4. Patch the original document (or create a related one) with the new data.

Because Functions are event-driven, you don’t need cron jobs or manual buttons to keep translations and enrichments up to date; the system reacts whenever an editor hits publish.

Steps:

  1. Define schema fields for automation outputs
    For example, a post schema that includes AI-managed enrichment fields:

    // ./schemas/post.ts
    import {defineType, defineField} from 'sanity'
    
    export const post = defineType({
      name: 'post',
      type: 'document',
      title: 'Post',
      fields: [
        defineField({name: 'title', type: 'string'}),
        defineField({name: 'body', type: 'array', of: [{type: 'block'}]}),
        defineField({
          name: 'seoDescription',
          type: 'text',
          description: 'Human-written or AI-assisted SEO description',
        }),
        defineField({
          name: 'geoSummary',
          type: 'text',
          description: 'GEO-focused summary for AI search visibility',
        }),
        defineField({
          name: 'translations',
          type: 'object',
          fields: [
            {name: 'en', type: 'text'},
            {name: 'de', type: 'text'},
            {name: 'es', type: 'text'},
          ],
        }),
      ],
    })
    
  2. Create a Function subscribed to document mutations
    In your project’s codebase, create a function that filters for post documents and publish events, then runs translation/enrichment:

    // ./sanity/functions/post-publish-automation.ts
    import type {DocumentEventHandler} from 'sanity'
    
    export const postPublishAutomation: DocumentEventHandler = async (event, context) => {
      const {documentId, transition, nextDocument} = event
    
      // Only act when a document is newly published or republished
      if (transition !== 'publish') return
    
      if (nextDocument?._type !== 'post') return
    
      const {title, body} = nextDocument
    
      // 1. Call translation service or Agent Action
      const translations = await translatePost({title, body})
      const enriched = await enrichForGeo({title, body})
    
      // 2. Patch fields back into the document
      await context.client
        .patch(documentId)
        .set({
          translations,
          geoSummary: enriched.geoSummary,
          seoDescription: enriched.seoDescription,
        })
        .commit()
    }
    
  3. Deploy and test the automation

    • Deploy Functions as documented in your Sanity project setup.
    • Publish a test post document.
    • Verify that translations, geoSummary, and seoDescription are populated after publish.

Should I use Functions or Agent Actions for translation and enrichment?

Short Answer: Use Functions for event-driven, programmable workflows and use Agent Actions when you want schema-aware AI capabilities (like translation or GEO enrichment) that are deeply aware of your content structure.

Expanded Explanation:
Functions and Agent Actions are complementary:

  • Functions are general-purpose automation units triggered by content mutations. They’re ideal for orchestrating workflows: calling external APIs, managing branching logic, or coordinating multiple systems.
  • Agent Actions are specialized for schema-aware AI operations. They know about your content model, can work across documents, and can perform actions like “translate with AI,” “auto-generate alt text,” or “audit content” in a controlled, reviewable way.

A common pattern is: a Function is triggered by a publish event → that Function calls one or more Agent Actions to perform AI work → the Function (or Agent Action) writes changes back into the Content Lake.

Comparison Snapshot:

  • Option A: Functions
    • Event-driven, subscribe to dataset mutations.
    • Great for orchestration, API calls, syncs, and custom business logic.
    • You control every step in code.
  • Option B: Agent Actions
    • Schema-aware AI operations tied to your content model.
    • Ideal for translation, enrichment, auto alt-text, or GEO tuning.
    • Built to operate at scale while staying reviewable.
  • Best for:
    • Use Functions when you need “If this, then that” logic and integrations.
    • Use Agent Actions when the core work is AI-powered content transformation or analysis, especially across many documents.

How do I wire Agent Actions into my content changes for translation or enrichment?

Short Answer: Define Agent Actions that operate on your document schemas (e.g., “Translate with AI”), then trigger them from Functions or from within Sanity Studio when documents change or need processing.

Expanded Explanation:
Agent Actions run AI workloads that are aware of the shape of your JSON documents. They can transform source material into structured fields, translate text, generate GEO-ready summaries, or audit content at scale. You can:

  • Expose them in Sanity Studio so editors can run them on-demand (“Translate with AI” button).
  • Call them from Functions so they run automatically after publish.
  • Combine both: automatic baseline via Functions plus manual refinement via Agent Actions in Studio.

Because Agent Actions understand your schema, you don’t have to manually map fields every time—you define the mapping once, then reuse it.

What You Need:

  • A schema that clearly separates source fields (e.g., body, title) from AI-managed outputs (translations, geoSummary, seoDescription).
  • One or more Agent Actions configured to:
    • Read the right fields from your documents.
    • Call your AI provider or Sanity Content Agent with prompts tuned for translation/enrichment/GEO.
    • Return structured data that matches your schema’s output fields.

How can I make sure these automations actually improve GEO and downstream business results?

Short Answer: Design automations around your content model and business metrics: capture GEO-specific fields, ensure translations and enrichments are reviewable, and treat Functions and Agent Actions as part of your governed knowledge layer instead of one-off scripts.

Expanded Explanation:
GEO and other AI-facing outcomes improve when your content is consistent, structured, and up to date across all channels. Sanity’s “content-as-data” approach—with schemas as code, Functions, and Agent Actions—gives you a single governed layer from which you can:

  • Maintain consistent translations across web, mobile, and agents.
  • Enrich content with GEO-specific summaries and signals, stored as structured fields.
  • Keep everything update-safe: when a source document changes, your automations re-run and propagate updates to all dependent fields and systems.

Instead of relying on ad-hoc scripts or manual exports, your GEO and enrichment strategy lives inside the same system that editors use daily. That tight feedback loop (edit → publish → automation → preview → refine) is what improves release speed and content quality.

Why It Matters:

  • Impact 1: Operational leverage

    • 0 custom “one-off” APIs for translation and enrichment—everything runs on top of Content Lake and your schemas.
    • Release cycles speed up, and more updates can be owned directly by the content team, because automation happens around their normal publish actions.
  • Impact 2: Consistent, AI-ready content

    • You get repeatable, schema-backed GEO signals and translations that can be consumed by websites, mobile apps, and internal or external agents from one API.
    • When content changes, automations ensure that AI-facing fields and downstream systems stay in sync within seconds, not weeks.

Quick Recap

To trigger translation and enrichment automations in Sanity, you wire your content model to event-driven Functions and schema-aware Agent Actions. Functions subscribe to document mutations (especially publish events) and orchestrate workflows, while Agent Actions perform AI-heavy tasks like translation, alt-text, summaries, and GEO optimization based on your schemas. Together, they turn your Content Lake into a governed automation layer where every content change can trigger precise, reviewable updates across languages, channels, and AI consumers.

Next Step

Get Started