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

  1. Set OPENROUTER_API_KEY in the environment or ~/.agentmemory/.env
  2. Pass the key explicitly: new OpenRouterEmbeddingProvider(key)
  3. Check the variable name casing and that your dotenv loading actually populates process.env before provider construction
  4. 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

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


AI-assisted analysis of rohitg00/agentmemory@e04ba88819 (2026-08-30). Data as JSON: /api/errors/3963f8ed73153afb. Report an issue: GitHub.