JavaScript (Node.js) Quick Start Guide for /v1/embeddings API
Last updated May 13, 2025
Table of Contents
The Cohere Embed Multilingual (cohere-embed-multilingual
) model generates vector embeddings (lists of numbers) for provided text inputs. These embeddings can be used in various applications, such as search, classification, and clustering. This guide describes how to access the /v1/embeddings
API using JavaScript.
Prerequisites
Before making requests, provision access to the model of your choice.
If it’s not already installed, install the Heroku CLI. Then install the Heroku AI plugin:
heroku plugins:install @heroku/plugin-ai
Attach the embedding model to an app of yours:
# If you don't have an app yet, you can create one with: heroku create example-app # specify the name you want for your app (or skip this step to use an existing app you have!) # Create and attach the embedding model to your app. heroku ai:models:create -a example-app cohere-multilingual --as EMBEDDING
Install the necessary
axios
package:npm install axios
JavaScript Example Code
const axios = require('axios');
// Assert that environment variables are set
const EMBEDDING_URL = process.env.EMBEDDING_URL;
const EMBEDDING_KEY = process.env.EMBEDDING_KEY;
const EMBEDDING_MODEL_ID = process.env.EMBEDDING_MODEL_ID;
if (!EMBEDDING_URL || !EMBEDDING_KEY || !EMBEDDING_MODEL_ID) {
console.error("Missing required environment variables.");
console.log("Set them up using the following commands:");
console.log("export EMBEDDING_URL=$(heroku config:get -a $APP_NAME EMBEDDING_URL)");
console.log("export EMBEDDING_KEY=$(heroku config:get -a $APP_NAME EMBEDDING_KEY)");
console.log("export EMBEDDING_MODEL_ID=$(heroku config:get -a $APP_NAME EMBEDDING_MODEL_ID)");
process.exit(1);
}
async function parseEmbeddingOutput(response) {
if (response.status === 200) {
console.log("Embeddings:", response.data.data);
} else {
console.log(`Request failed: ${response.status}, ${response.statusText}`);
}
}
async function generateEmbeddings(payload) {
try {
const response = await axios.post(`${EMBEDDING_URL}/v1/embeddings`, payload, {
headers: {
'Authorization': `Bearer ${EMBEDDING_KEY}`,
'Content-Type': 'application/json'
}
});
await parseEmbeddingOutput(response);
} catch (error) {
console.error("Error generating embeddings:", error.message);
}
}
// Example payload
const payload = {
model: EMBEDDING_MODEL_ID,
input: ["Hello, I am a blob of text.", "How's the weather in Portland?"],
input_type: "search_document",
truncate: "END",
encoding_format: "float"
};
// Generate embeddings
generateEmbeddings(payload);