How to Integrate Cohere with Your Headless CMS
Connect Cohere to your headless CMS so published articles, product docs, and support content become searchable, rankable, and usable in AI workflows within minutes.
What is Cohere?
Cohere is an enterprise AI platform that provides large language models and APIs for chat, text generation, embeddings, reranking, and classification. Teams use Cohere to build semantic search, retrieval-augmented generation, support automation, and content analysis workflows. Its Embed and Rerank models are widely used when teams need language-aware search across private business content.
Why integrate Cohere with a headless CMS?
If your product docs, support articles, policy pages, or knowledge base entries change every day, Cohere is only as useful as the content you give it. A disconnected setup usually means someone exports CSV files, runs a one-off embedding script, and hopes the search index still matches what customers see on the site. That breaks fast when 200 product pages are edited before a launch or when support content changes after a pricing update.
Architecture overview
A typical Sanity and Cohere integration starts when an editor publishes or updates content in Sanity Studio. A webhook fires on the publish event, or a Sanity Function runs directly from the content mutation. The handler receives the document ID, uses @sanity/client to fetch the full document from the Content Lake with GROQ, and projects only the fields Cohere needs, such as title, slug, excerpt, body text, locale, and referenced product names. The handler then calls Cohere's Embed API with a model such as embed-english-v3.0 and inputType set to search_document. The returned embedding can be written to a vector index such as pgvector, Pinecone, or Elasticsearch, along with the Sanity document ID and metadata. At query time, your frontend sends the user's search text to a server route, calls Cohere Embed with inputType set to search_query, retrieves nearest documents from the vector index, and can optionally call Cohere Rerank with rerank-english-v3.0 before showing results to the end user. Sanity stays the source for the published content, Cohere handles language understanding, and the search index handles nearest-neighbor lookup.
Common use cases
Semantic documentation search
Use Cohere Embed and Rerank to return the right help article when a user searches for "reset team billing" instead of the exact article title.
Retrieval for support agents
Feed approved Sanity content into a Cohere-backed retrieval flow so support agents answer from current policies, product docs, and troubleshooting guides.
Content classification
Call Cohere Classify to tag articles by intent, product area, risk level, or customer segment after editors publish new content.
Multilingual knowledge discovery
Use Cohere multilingual embeddings to connect related content across locales, even when users search in a different language than the source article.
Step-by-step integration
- 1
Create a Cohere API key
Create a Cohere account, open the API keys section, and copy a server-side key. Add it to your environment as COHERE_API_KEY. Install the SDK with npm install cohere-ai.
- 2
Model searchable content in Sanity Studio
Define fields that map cleanly to Cohere inputs, such as title, slug, excerpt, body, locale, category, and referenced products. Keep the AI-facing text explicit instead of relying on rendered page HTML.
- 3
Write a GROQ query for the Cohere payload
Fetch only the fields needed for embeddings or classification. For example, include title, excerpt, Portable Text plain text, product names, locale, and canonical URL. Skip internal notes, drafts, and fields that shouldn't be sent to a model.
- 4
Create the sync trigger
Use a Sanity webhook for publish, update, and delete events, or use a Sanity Function if you want server-side processing without running your own listener. Filter events by document type so only articles, docs, or product pages trigger Cohere work.
- 5
Call Cohere and write the result to your index
Send the selected text to Cohere Embed with inputType set to search_document. Store the returned vector with the Sanity document ID, slug, locale, and updatedAt value in your vector index. On deletes, remove the matching ID.
- 6
Test search and reranking in the frontend
Build a server route that embeds the user's query with inputType set to search_query, fetches nearest matches from your vector index, and calls Cohere Rerank for the top 20 to 50 candidates before rendering results.
Code example
import {createClient} from '@sanity/client';
import {CohereClient} from 'cohere-ai';
const sanity = createClient({
projectId: process.env.SANITY_PROJECT_ID!,
dataset: process.env.SANITY_DATASET!,
apiVersion: '2025-01-01',
token: process.env.SANITY_API_TOKEN!,
useCdn: false,
});
const cohere = new CohereClient({token: process.env.COHERE_API_KEY!});
export async function POST(req: Request) {
const {ids} = await req.json();
const id = ids?.created?.[0] || ids?.updated?.[0];
if (!id) return Response.json({ok: true});
const doc = await sanity.fetch(`
*[_id == $id][0]{
_id,
title,
excerpt,
"slug": slug.current,
"body": pt::text(body),
"products": products[]->title
}
`, {id});
if (!doc) return Response.json({ok: true});
const text = [doc.title, doc.excerpt, doc.body, doc.products?.join(', ')]
.filter(Boolean)
.join('
');
const embed = await cohere.embed({
model: 'embed-english-v3.0',
texts: [text],
inputType: 'search_document',
embeddingTypes: ['float'],
});
await fetch(process.env.VECTOR_INDEX_URL!, {
method: 'POST',
headers: {'content-type': 'application/json'},
body: JSON.stringify({
id: doc._id,
slug: doc.slug,
title: doc.title,
vector: embed.embeddings.float?.[0],
}),
});
return Response.json({ok: true, indexed: doc._id});
}How Sanity + Cohere works
Build your Cohere integration on Sanity
Sanity gives you the structured content foundation, real-time event system, and flexible APIs to connect published content with Cohere for search, reranking, classification, and AI workflows.
Start building free →CMS approaches to Cohere
| Capability | Traditional CMS | Sanity |
|---|---|---|
| Content shape for Cohere | Content often lives as page HTML or mixed layout fields, so teams clean and parse text before sending it to Cohere. | The Content Lake stores typed JSON, and GROQ can create a Cohere-ready payload with text, metadata, and joined references. |
| Sync after publish | Teams often rely on scheduled crawlers or manual exports, which can leave search results behind published changes. | Webhooks or Functions can trigger on publish, update, or delete events, then call Cohere without polling. |
| Field-level control | Sending only safe, public, AI-ready fields can be difficult when content and presentation are tightly mixed. | GROQ selects exactly the fields Cohere needs, including flattened Portable Text, locale, categories, and referenced product data. |
| Editor workflow | Editors publish pages, then technical teams may run separate scripts to update AI search or classification. | Sanity Studio can show fields, validation, Tasks, and custom index status components in the same editorial workspace. |
| AI agent readiness | Agents often need crawlers or copied knowledge bases to access current content. | Agent Context gives production AI agents read-only, scoped access to structured content, while Cohere can handle embeddings, reranking, and language tasks. |
Keep building
Explore related integrations to complete your content stack.
Sanity + OpenAI
Generate summaries, drafts, and structured transformations from content in the Content Lake.
Sanity + Anthropic (Claude)
Build editorial review, long-context analysis, and knowledge workflows using approved Sanity content.
Sanity + Writer
Apply brand rules, terminology, and content governance to AI-assisted editorial workflows.