{"record":{"id":"2b9a70e68043caa8","repo":"mem0ai/mem0","slug":"mem0-api-key-is-required","errorCode":null,"errorMessage":"Mem0 API key is required","messagePattern":"Mem0 API key is required","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"mem0-ts/src/client/mem0.ts","lineNumber":111,"sourceCode":"\n// Shares one ping per (host, api key) across clients; FIFO-capped.\nconst IDENTITY_CACHE_MAX_DEFAULT = 50;\nconst identityByCredentials = new Map<string, Promise<ClientIdentity>>();\n\nexport default class MemoryClient {\n  apiKey: string;\n  host: string;\n  private organizationId: string | number | null;\n  private projectId: string | number | null;\n  headers: Record<string, string>;\n  client: any;\n  telemetryId: string;\n  private initialized: Promise<void>;\n  private identityCacheMax: number;\n\n  _validateApiKey(): any {\n    if (!this.apiKey) {\n      throw new Error(\"Mem0 API key is required\");\n    }\n    if (typeof this.apiKey !== \"string\") {\n      throw new Error(\"Mem0 API key must be a string\");\n    }\n    if (this.apiKey.trim() === \"\") {\n      throw new Error(\"Mem0 API key cannot be empty\");\n    }\n  }\n\n  constructor(options: ClientOptions) {\n    this.apiKey = options.apiKey;\n    this.host = options.host || \"https://api.mem0.ai\";\n    this.organizationId = null;\n    this.projectId = null;\n    this.identityCacheMax =\n      options.identityCacheMax ?? IDENTITY_CACHE_MAX_DEFAULT;\n\n    this.headers = {","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0-ts/src/client/mem0.ts#L93-L129","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure apiKey is set at construction: new MemoryClient({ apiKey: process.env.MEM0_API_KEY }).","Fail startup early: if (!process.env.MEM0_API_KEY) throw new Error('MEM0_API_KEY missing') in your bootstrap.","Check spelling and export of the env var and that dotenv/config runs before client creation.","In serverless, add the key to the function's environment configuration."],"exampleFix":"// before\nconst client = new MemoryClient(); // no options → 'Mem0 API key is required'\n\n// after\nconst client = new MemoryClient({ apiKey: process.env.MEM0_API_KEY! });","handlingStrategy":"validation","validationCode":"function requireApiKey(env: NodeJS.ProcessEnv = process.env): string {\n  const key = env.MEM0_API_KEY;\n  if (!key) throw new Error('MEM0_API_KEY is not set — configure it before creating MemoryClient');\n  return key;\n}\n\nconst client = new MemoryClient({ apiKey: requireApiKey() });","typeGuard":"const hasValidApiKeyShape = (v: unknown): v is string =>\n  typeof v === 'string' && v.trim().length > 0 && v.startsWith('m0-'); // adjust prefix to your keys","tryCatchPattern":null,"preventionTips":["Assert the env var at process start, not at first request.","Add the key to CI/serverless environment configuration explicitly.","Fail deployment smoke checks if a client cannot be constructed."],"tags":["configuration","api-key","startup","validation"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}