Create a minimal working Cohere example with Chat, Embed, and Rerank.
Use when starting a new Cohere integration, testing your setup,
or learning basic Cohere API v2 patterns.
Trigger with phrases like "cohere hello world", "cohere example",
"cohere quick start", "simple cohere code".
Create a minimal working Cohere example with Chat, Embed, and Rerank.
Use when starting a new Cohere integration, testing your setup,
or learning basic Cohere API v2 patterns.
Trigger with phrases like "cohere hello world", "cohere example",
"cohere quick start", "simple cohere code".
allowed-tools
Read, Write, Edit
version
1.5.0
license
MIT
author
Jeremy Longshore <jeremy@intentsolutions.io>
tags
["saas","ai","nlp","cohere"]
compatibility
Designed for Claude Code
Cohere Hello World
Overview
Three minimal working examples: Chat completion, text embedding, and search reranking. Each demonstrates a core Cohere API v2 endpoint.
Prerequisites
Completed cohere-install-auth setup
cohere-ai package installed
CO_API_KEY environment variable set
Instructions
Example 1: Chat Completion
import { CohereClientV2 } from'cohere-ai';
const cohere = newCohereClientV2();
asyncfunctionchat() {
const response = await cohere.chat({
model: 'command-a-03-2025',
messages: [
{ role: 'system', content: 'You are a helpful coding assistant.' },
{ role: 'user', content: 'Explain what a closure is in JavaScript in 2 sentences.' },
],
});
console.log(response.message?.content?.[0]?.text);
}
chat().catch(console.error);
asyncfunctionrerank() {
const response = await cohere.rerank({
model: 'rerank-v3.5',
query: 'What is machine learning?',
documents: [
'Machine learning is a subset of artificial intelligence.',
'The weather today is sunny and warm.',
'Deep learning uses neural networks with many layers.',
'I enjoy cooking Italian food on weekends.',
],
topN: 2,
});
for (const result of response.results) {
console.log(`[${result.relevanceScore.toFixed(3)}] ${result.index}`);
}
}
rerank().catch(console.error);