rohitg00/agentmemory · error · Error
OPENROUTER_API_KEY is required
Error message
OPENROUTER_API_KEY is required
What it means
OpenRouterEmbeddingProvider needs an OpenRouter API key, taken from the constructor argument or the OPENROUTER_API_KEY environment variable. With neither present the constructor throws this message before any network activity.
Source
Thrown at src/providers/embedding/openrouter.ts:18
import type { EmbeddingProvider } from "../../types.js";
import { getEnvVar } from "../../config.js";
import { fetchWithTimeout } from "../_fetch.js";
import { resolveDimensions } from "./_dimensions.js";
const API_URL = "https://openrouter.ai/api/v1/embeddings";
const DEFAULT_MODEL = "openai/text-embedding-3-small";
export class OpenRouterEmbeddingProvider implements EmbeddingProvider {
readonly name = "openrouter";
readonly dimensions: number;
private apiKey: string;
private model: string;
constructor(apiKey?: string) {
this.apiKey = apiKey || getEnvVar("OPENROUTER_API_KEY") || "";
if (!this.apiKey) throw new Error("OPENROUTER_API_KEY is required");
this.model = getEnvVar("OPENROUTER_EMBEDDING_MODEL") || DEFAULT_MODEL;
this.dimensions = resolveDimensions(
this.model,
getEnvVar("OPENROUTER_EMBEDDING_DIMENSIONS"),
"OPENROUTER_EMBEDDING_DIMENSIONS",
);
}
async embed(text: string): Promise<Float32Array> {
const [result] = await this.embedBatch([text]);
return result;
}
async embedBatch(texts: string[]): Promise<Float32Array[]> {
const response = await fetchWithTimeout(API_URL, {
method: "POST",
headers: {
Authorization: `Bearer ${this.apiKey}`,View on GitHub (pinned to e04ba88819)
Solutions
- Set OPENROUTER_API_KEY in the environment or ~/.agentmemory/.env
- Pass the key explicitly: new OpenRouterEmbeddingProvider(key)
- Check the variable name casing and that your dotenv loading actually populates process.env before provider construction
- For the gemini-via-OpenRouter path, note it reads GEMINI_API_KEY/GOOGLE_API_KEY instead — set whichever the chosen provider type requires
Example fix
// before // (no OPENROUTER_API_KEY set) const provider = new OpenRouterEmbeddingProvider(); // after // export OPENROUTER_API_KEY=sk-or-v1-... const provider = new OpenRouterEmbeddingProvider();
Defensive patterns
Strategy: validation
Validate before calling
const key = process.env.OPENROUTER_API_KEY;
if (!key) throw new Error('Set OPENROUTER_API_KEY before using the openrouter embedding provider');
const provider = new OpenRouterEmbeddingProvider(key); Type guard
const isNonEmptyString = (v: unknown): v is string => typeof v === 'string' && v.trim().length > 0;
Try / catch
try {
provider = new OpenRouterEmbeddingProvider();
} catch (err) {
if (err instanceof Error && err.message === 'OPENROUTER_API_KEY is required') {
console.error('Set OPENROUTER_API_KEY in env or ~/.agentmemory/.env');
process.exit(1);
}
throw err;
} Prevention
- Pre-flight env check at startup listing every required provider key
- Migrate all provider keys together when switching provider types
- Keep one canonical .env at ~/.agentmemory/.env loaded before any provider construction
- Name secrets exactly as the library reads them (OPENROUTER_API_KEY)
When it happens
Trigger: Calling `new OpenRouterEmbeddingProvider()` (directly or via the provider factory when config.type is 'openrouter') with no argument and OPENROUTER_API_KEY unset in env / ~/.agentmemory/.env.
Common situations: Switching providers from openai to openrouter without migrating env vars; CI/production secrets not provisioned; .env not loaded because the file lives in the project dir instead of ~/.agentmemory/.env; key present under a differently-named variable.
Understand the failure class
Background: "API key is required" / "API key not found" / "No API key was set": the missing-api-key error family across 16 libraries — this error's family across 16 libraries.
Related errors
- COHERE_API_KEY is required
- GEMINI_API_KEY is required
- API key is required (via constructor, OPENAI_EMBEDDING_API_K
- VOYAGE_API_KEY is required
- GEMINI_API_KEY (or GOOGLE_API_KEY) is required for the gemin
AI-assisted analysis of rohitg00/agentmemory@e04ba88819 (2026-08-30).
Data as JSON: /api/errors/3963f8ed73153afb.
Report an issue: GitHub.