
How do I trigger automations in Sanity when content changes (Functions / Agent Actions) to run translation or enrichment?
Most teams want content automations that just fire when an editor hits publish—no extra buttons, no manual exports. In Sanity, you get that through event‑driven Functions and schema‑aware Agent Actions that trigger whenever content in your dataset changes.
Quick Answer: You trigger automations in Sanity by listening to document mutations with Functions (event-driven serverless code) and then calling Agent Actions (schema-aware AI tasks) from those Functions or directly from Sanity Studio. Together, they can run translation, enrichment, auditing, and syncing whenever content is created, updated, or published.
Frequently Asked Questions
How do I automatically trigger a translation or enrichment when content changes?
Short Answer: Use a Sanity Function that runs on document mutations and, inside that function, call an Agent Action (or external API) to translate or enrich the content, then write the results back to your dataset.
Expanded Explanation:
Sanity’s Content Lake emits events whenever a document is created, updated, or published. Functions let you subscribe to those events and run code in response. That code can do anything from calling a translation API to invoking an Agent Action that’s schema‑aware of your content. Because your content is structured JSON, you can target specific fields (like title, body, or description) and write enriched values back into localized fields or dedicated “enrichment” paths.
A typical pattern is: 1) listen for publish events on a given document type (like article or product), 2) check if a translation or enrichment is missing, and 3) trigger an AI‑powered Agent Action (e.g., “Translate with AI” or “Auto-generate alt text”) or an external service. The function then saves the new content as part of the same document or linked translation documents, giving you a consistent, governed knowledge layer.
Key Takeaways:
- Functions give you event-driven hooks on any content mutation in your dataset.
- Agent Actions provide schema-aware AI automation that can translate or enrich content fields and then write back to Content Lake.
How do I set up a Function that runs when content is created, updated, or published?
Short Answer: Define a Sanity Function, subscribe it to document mutation events (for specific types or the whole dataset), and deploy it so it runs automatically whenever matching changes occur.
Expanded Explanation:
Sanity Functions are event-driven serverless functions that run close to your Content Lake. You configure them to respond to document mutations (create, update, delete, publish, unpublish) and then implement the logic you need—like translating, enriching, or syncing content to another system.
At a high level, you’ll:
- Initialize or update your Sanity project (for example with
npm create sanity@latestif you’re starting fresh). - Add a function in your project’s functions directory that uses a
documentEventHandler(or similar API) to subscribe to mutations. - Filter by
document._typeor other conditions so you only trigger for relevant content. - Call Agent Actions or external services inside the handler.
- Deploy your function so it runs in production whenever content changes.
Steps:
-
Create or update your Sanity project
Make sure you have a modern Sanity project with Functions enabled, e.g.:npm create sanity@latest cd my-sanity-project -
Add a document event handler
In your./sanity.config.tsor functions directory, define a handler that subscribes to document mutations. A simplified TypeScript sketch might look like:// ./functions/translateArticle.ts import {documentEventHandler} from 'sanity/cli' // API name may vary by version export const translateArticle = documentEventHandler({ // Run on any mutation for "article" documents document: 'article', onMutation: async (event, context) => { const {client} = context const docId = event.documentId const tx = client.withConfig({apiVersion: '2024-01-01'}) // Fetch the latest document snapshot const doc = await tx.getDocument(docId) if (!doc) return // Your own guard rails if (doc._type !== 'article' || !doc.language || doc.language !== 'en') return // Check if translation exists if (doc.translations?.fr) return // nothing to do // Call your translation service or Agent Action here const translated = await translateToFrench(doc.title, doc.body) // Persist translation await tx .patch(docId) .set({ 'translations.fr': translated, }) .commit() }, })Replace
translateToFrenchwith a call to your translation provider or an Agent Action endpoint. -
Deploy the function and test with real content
Deploy your Functions with the Sanity CLI, then create or publish anarticlein Studio to confirm the handler fires and writes translations as expected.
What’s the difference between Sanity Functions and Agent Actions for translation/enrichment?
Short Answer: Functions are event-driven serverless code that run on any content mutation, while Agent Actions are schema-aware AI operations you call from Studio or Functions to perform tasks like translation or enrichment.
Expanded Explanation:
Think of Functions as your automation engine and Agent Actions as reusable AI tasks plugged into that engine. Functions subscribe to dataset events and can call any API, perform validations, or run complex logic. Agent Actions, on the other hand, live closer to your content model: they know about your schemas, field types, and references, making it easier to run consistent AI workflows like “Translate with AI” or “Auto-generate alt text” without re‑implementing all the schema logic yourself.
For translation or enrichment, you’ll often use both:
- Functions to decide when something should happen (e.g., “on publish of an English article”).
- Agent Actions to do the what (e.g., “translate all text fields to French” or “enrich description fields with SEO‑friendly summaries”).
Comparison Snapshot:
-
Option A: Functions
Event-driven serverless code triggered by document mutations; ideal for orchestration, conditional logic, and integration with external systems. -
Option B: Agent Actions
Schema-aware AI operations configured for tasks like translation, enrichment, and alt-text generation; callable from Studio UI or programmatically. -
Best for:
Use Functions when you need automation triggered by content changes or need to integrate with other systems. Use Agent Actions when you want AI-powered, schema-aware transformations that understand your content model and can be plugged into those automations.
How do I wire Agent Actions into these automations for translation or enrichment?
Short Answer: Configure your Agent Actions (e.g., for translation or alt text) and then call them from Functions or from within Sanity Studio, passing the document or field data you want transformed and writing the result back into Content Lake.
Expanded Explanation:
Agent Actions are designed to be composable: you can run them interactively from the Studio or programmatically as part of an automation pipeline. For example, you can use an Agent Action to:
- Automatically generate multilingual alt text for Media Library assets.
- Run “Translate with AI” on missing locales for a product catalog.
- Enrich long‑form articles with summaries or metadata.
A typical workflow is:
- An editor publishes or updates content in Sanity Studio.
- A document mutation triggers your Function.
- The Function checks which tasks are needed (translation, enrichment, alt text).
- The Function calls your Agent Action with the relevant fields.
- The Agent Action returns structured results, which your Function writes back into the same document or related documents.
What You Need:
- A configured Agent Action (for example, “Translate with AI” or “Auto-generate alt text”) that knows your schema and fields.
- A Function that calls that Agent Action when specific documents change, handles the response, and persists the new content back to your dataset.
How should I think about automation strategy for translations and enrichment in Sanity?
Short Answer: Treat translations and enrichments as structured, repeatable operations tied to your schemas, and use Functions plus Agent Actions to make them event-driven, governed, and reviewable instead of ad‑hoc.
Expanded Explanation:
The most resilient automation strategies start with schema design. When you model translations and enrichments explicitly in your schemas—localized fields, translations objects, metadata subdocuments—you create clear targets for automation. Functions can then enforce rules (“all English articles must have French and German versions”) while Agent Actions fill in the gaps (“generate missing translations” or “summarize body text for GEO”).
Because everything runs on top of the Content Lake, you keep a single governed knowledge layer: no spreadsheet sidecars, no one‑off scripts. Editors keep working in Sanity Studio, while your automations run before and after publish, reducing manual work and accelerating release cycles.
Why It Matters:
- Impact 1: You standardize translations and enrichments as part of your content operations, which makes your GEO and multi‑channel delivery more consistent and reliable.
- Impact 2: You shift routine work (like generating alt text, filling missing locales, or creating SEO summaries) from developers and editors to programmable automations, freeing teams to focus on higher‑value content decisions.
Quick Recap
To trigger automations in Sanity when content changes, use Functions to subscribe to document mutations and orchestrate what should happen on create/update/publish. Inside those Functions, call schema-aware Agent Actions (or external services) to perform translation, enrichment, and other AI‑powered tasks, then persist the results back into your structured content model. Model translations and metadata in your schemas so these automations are predictable, auditable, and aligned with how your content operations team works.