mem0ai/mem0 · error · Error

Mem0 API key is required

Error message

Mem0 API key is required

What it means

MemoryClient._validateApiKey() throws this when this.apiKey is falsy — undefined, null, or empty — before the client makes any request. It is a fail-fast constructor-level guard so an unconfigured client never reaches the network.

Source

Thrown at mem0-ts/src/client/mem0.ts:111

// Shares one ping per (host, api key) across clients; FIFO-capped.
const IDENTITY_CACHE_MAX_DEFAULT = 50;
const identityByCredentials = new Map<string, Promise<ClientIdentity>>();

export default class MemoryClient {
  apiKey: string;
  host: string;
  private organizationId: string | number | null;
  private projectId: string | number | null;
  headers: Record<string, string>;
  client: any;
  telemetryId: string;
  private initialized: Promise<void>;
  private identityCacheMax: number;

  _validateApiKey(): any {
    if (!this.apiKey) {
      throw new Error("Mem0 API key is required");
    }
    if (typeof this.apiKey !== "string") {
      throw new Error("Mem0 API key must be a string");
    }
    if (this.apiKey.trim() === "") {
      throw new Error("Mem0 API key cannot be empty");
    }
  }

  constructor(options: ClientOptions) {
    this.apiKey = options.apiKey;
    this.host = options.host || "https://api.mem0.ai";
    this.organizationId = null;
    this.projectId = null;
    this.identityCacheMax =
      options.identityCacheMax ?? IDENTITY_CACHE_MAX_DEFAULT;

    this.headers = {

View on GitHub (pinned to 001c235229)

Solutions

  1. Ensure apiKey is set at construction: new MemoryClient({ apiKey: process.env.MEM0_API_KEY }).
  2. Fail startup early: if (!process.env.MEM0_API_KEY) throw new Error('MEM0_API_KEY missing') in your bootstrap.
  3. Check spelling and export of the env var and that dotenv/config runs before client creation.
  4. In serverless, add the key to the function's environment configuration.

Example fix

// before
const client = new MemoryClient(); // no options → 'Mem0 API key is required'

// after
const client = new MemoryClient({ apiKey: process.env.MEM0_API_KEY! });
Defensive patterns

Strategy: validation

Validate before calling

function requireApiKey(env: NodeJS.ProcessEnv = process.env): string {
  const key = env.MEM0_API_KEY;
  if (!key) throw new Error('MEM0_API_KEY is not set — configure it before creating MemoryClient');
  return key;
}

const client = new MemoryClient({ apiKey: requireApiKey() });

Type guard

const hasValidApiKeyShape = (v: unknown): v is string =>
  typeof v === 'string' && v.trim().length > 0 && v.startsWith('m0-'); // adjust prefix to your keys

Prevention

When it happens

Trigger: Constructing `new MemoryClient({ apiKey: process.env.MEM0_API_KEY })` when the env var is unset; passing options without an apiKey key at all; passing apiKey: undefined via a conditional spread.

Common situations: Env var named differently (MEM0_API_KEY vs API_KEY), missing .env load in the entrypoint, CI/CD secrets not injected, or deploy environment (edge/serverless) without the variable.

Related errors


AI-assisted analysis of mem0ai/mem0@001c235229 (2026-08-15). Data as JSON: /api/errors/2b9a70e68043caa8. Report an issue: GitHub.