Agent Actions

Translate quick start

Learn to translate documents with the Translate Agent Actions action.

Experimental feature

Translate is a Sanity Agent Actions action that lets you programmatically run schema-aware AI translations on Sanity documents. You can run translations from anywhere you can execute code, such as Sanity Functions, custom components, webhook listeners, CI/CD pipelines, migration scripts, and more.

In this guide, you'll first use Translate to convert a document into a new language. You'll use @sanity/client to create the translation requests (you can also make requests using the HTTP API directly).

Prerequisites

  • @sanity/client v7.1.0 or later and an environment to run client requests.
  • API version vX is required for all requests to the Agent Actions API.
  • Optional: In Node.js v23.6 or later, you can run this guide's TypeScript examples without additional servers or build processes. Alternatively, you can use earlier versions with an experimental flag. Converting the examples to JavaScript is okay too.
  • sanity CLI v3.88.0 or later.
  • A Sanity project for testing. This guide's examples use details from the sample "Movies" studio schema that you can select when initializing a new project.
    • A read/write API token to authenticate requests.
    • A valid projectId and dataset name.

Obtain a schema ID

Translate requires an uploaded schema. If you've deployed recently, you can check for a list of uploaded schemas by running the schemas list command. If you don't see a schema or want to deploy the latest version, redeploy your studio to Sanity or deploy the schema:

Copy the schema ID, which you'll need for making Agent Actions requests.

For more information, see Deploy a schema.

Configure the client

Import and configure @sanity/client with the projectId, dataset, API token, and an apiVersion of vX:

If you're already using the client elsewhere in an application, you can reuse its base configuration. To adjust the token or the API version, use the withConfig method to create a new client based on your existing one. For example:

// ...
const translateClient = client.withConfig({
  token: process.env.SANITY_API_TOKEN,
})

Translate a document

It's common to want a complete, translated version of a document. To achieve this with Translate:

  • Provide a source documentId of the original document.
  • Define the fromLanguage. This is optional, but it avoids the AI interpreting the document as a language other than the one you expect.
  • Define the toLanguage that you want the document translated into.
  • Set the operation. Operations tell Agent Actions what to do. This example uses create. Learn more about operations.

Here's a minimal example that uses an existing English language document to create a new translation in Greek:

This creates a new draft document based on the source (documentId).

Create makes an unlinked draft

Customize the output with style guides

If you're familiar with the other Agent Actions, styleGuide is Translate's version of instruction. It lets you add additional context and guidance beyond setting a target language.

This example tells Translate to use a formal tone:

You can also pass information into the style guide with styleGuideParams:

This example uses a constant type parameter to assign the string "formal" to the tone key, then passes it into the style guide as $tone. This is one type of parameter. You can do everything from including full documents to making GROQ queries. Learn more about passing parameters into style guides.

Next steps

To learn more about what you can do with Translate, explore the other guides and resources available for Agent Actions.

Was this page helpful?