APIs and SDKs

Content Agent API

Build custom integrations with the Sanity Content Agent API.

Build chat interfaces, automate content workflows, and create custom tools that read and write Sanity content through natural language. The content-agent npm package is a Vercel AI SDK provider that handles streaming, authentication, and thread management.

The package supports two interaction modes: threads for stateful, multi-turn conversations (.agent()) and one-shot prompts for stateless single-turn tasks (.prompt()). Both work with the standard Vercel AI SDK functions like generateText and streamText.

For the full API reference, see the content-agent reference docs.

Prerequisites

Before you start, you need:

  • A Sanity project with a deployed schema
  • A Sanity authentication token (from sanity.io/manage)
  • Your organization ID (visible in your project settings)
  • Node.js 18+

Content Agent calls consume AI credits

Quick start

First, install the packages:

Generating text

This example sends a single prompt to the Content Agent and prints the response. It uses generateText from the Vercel AI SDK.

Streaming

Use streamText to display results as they arrive.

Installation and setup

Install the packages

The content-agent package is available on npm.

The content-agent package is a Vercel AI SDK provider. The ai package is a peer dependency required for generateText, streamText, and other Vercel AI SDK functions.

Create the provider

For all provider options, see the createContentAgent reference.

Authentication

All API requests require a Sanity authentication token. You can create tokens in your project settings at sanity.io/manage.

Keep tokens secure

Applications

Each application key uniquely identifies a deployed Sanity Studio workspace. Since multiple studios can share the same project ID and dataset, the application key targets the right one.

Use .applications() to list available studios for the authenticated user, then pass the key to .agent() or .prompt():

Configuration

The config object controls agent behavior. Pass it as part of the options to .agent() or .prompt(). For the full type definition, see the Config reference.

Here are three common patterns:

For full details on each option, see the subsections below.

Capabilities

Capabilities control what the agent can do. Configure read and write independently. Each accepts true (standard preset), false (no access), or an object with a preset name. For the full type definition, see the Capabilities reference.

PresetRead featuresWrite features
falseNo accessNo access
{ preset: 'minimal' }Document queries, web searchSimple mutations
true or { preset: 'standard' }Document queries, sets (bulk analysis), web searchSimple and bulk mutations

Drafts only

Use capabilities.features to toggle individual features on or off, overriding the preset defaults:

Filters

Use GROQ boolean expressions to control which documents the agent can see and modify. For the full type definition, see the Filter reference.

Perspectives

Perspectives control which document versions the agent reads from and writes to. Values are Sanity perspective IDs: "drafts", "published", "raw", or a release ID.

When you set read, the agent's query tools are restricted to the listed perspectives. When you set write, new documents are created in the specified perspective (for example, "drafts" creates drafts.* IDs).

User message context

The userMessageContext field passes contextual information that the agent appends to each user message. Each key becomes an XML tag with the value as content.

Custom instructions

The instruction field adds custom instructions to the agent's system prompt.

Custom tools

You can extend the agent with your own tools using the Vercel AI SDK tool pattern. Pass custom tools when calling generateText or streamText. The package forwards tool schemas to the agent and runs execution locally on your server.

Custom tools run alongside the agent's built-in tools. The agent decides when to call them based on the message and the tool descriptions you provide.

Examples

Read-only document explorer

Restrict the agent to querying documents without making changes.

Chat with user context

Pass contextual information about the current environment or workflow to the agent.

Error handling

The package throws API errors as exceptions. Wrap your calls in try/catch blocks. For the full list of error types and status codes, see the ErrorResponse reference.

Limitations

  • The agent can only write to draft and versioned documents.
  • The prompt endpoint has a 10,000 character limit for the message field.
  • The API version is currently vX (preview). Endpoints and behavior may change.
  • The API manages thread history server-side. You cannot retrieve or modify past messages through the API.

Was this page helpful?