{"record":{"id":"266eff1a8a4acb8b","repo":"mem0ai/mem0","slug":"apikey-is-required-for-mem0memory","errorCode":null,"errorMessage":"apiKey is required for Mem0Memory","messagePattern":"apiKey is required for Mem0Memory","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"mem0-ts/src/community/src/integrations/langchain/mem0.ts","lineNumber":174,"sourceCode":"  sessionId: string;\n\n  humanPrefix = \"Human\";\n\n  aiPrefix = \"AI\";\n\n  mem0Client: InstanceType<typeof MemoryClient>;\n\n  memoryOptions: AddMemoryOptions | SearchMemoryOptions | GetAllMemoryOptions;\n\n  mem0Options: ClientOptions;\n\n  // Whether to return separate messages for chat history with a SystemMessage containing (facts and summary) or return a single HumanMessage with the entire memory context.\n  // Defaults to false (return a single HumanMessage) in order to allow more flexibility with different models.\n  separateMessages?: boolean;\n\n  constructor(fields: Mem0MemoryInput) {\n    if (!fields.apiKey) {\n      throw new Error(\"apiKey is required for Mem0Memory\");\n    }\n    if (!fields.sessionId) {\n      throw new Error(\"sessionId is required for Mem0Memory\");\n    }\n\n    super({\n      returnMessages: fields?.returnMessages ?? false,\n      inputKey: fields?.inputKey,\n      outputKey: fields?.outputKey,\n    });\n\n    this.apiKey = fields.apiKey;\n    this.sessionId = fields.sessionId;\n    this.humanPrefix = fields.humanPrefix ?? this.humanPrefix;\n    this.aiPrefix = fields.aiPrefix ?? this.aiPrefix;\n    this.memoryOptions = fields.memoryOptions ?? {};\n    this.mem0Options = fields.mem0Options ?? {\n      apiKey: this.apiKey,","sourceCodeStart":156,"sourceCodeEnd":192,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0-ts/src/community/src/integrations/langchain/mem0.ts#L156-L192","documentation":"Thrown by the Mem0Memory LangChain integration constructor when fields.apiKey is falsy. The integration wraps the hosted MemoryClient and needs a Mem0 platform API key to authenticate every memory read/write. This fails fast at construction rather than on the first LLM call.","triggerScenarios":"new Mem0Memory({ sessionId: 's1', ... }) with apiKey missing, empty string, or undefined — typically when reading from process.env.MEM0_API_KEY that is not set in the current shell/deployment.","commonSituations":"Env var name mismatch (MEM0_API_KEY vs MEMORIA_API_KEY etc.); .env file not loaded in the entrypoint; deploying to Vercel/Lambda without adding the secret; passing apiKey inside mem0Options instead of the top-level field (top-level is required and takes precedence).","solutions":["Set apiKey at the top level of the constructor fields: new Mem0Memory({ apiKey: process.env.MEM0_API_KEY!, sessionId: 's1' })","Verify the env var is actually loaded: console.log(Boolean(process.env.MEM0_API_KEY)) or a startup assertion","Add MEM0_API_KEY to your platform's secret manager (Vercel/Lambda/Docker env) and restart"],"exampleFix":"// before\nconst memory = new Mem0Memory({ sessionId: 'user-1' });\n\n// after\nconst memory = new Mem0Memory({\n  apiKey: process.env.MEM0_API_KEY!,\n  sessionId: 'user-1',\n});","handlingStrategy":"validation","validationCode":"const apiKey = process.env.MEM0_API_KEY;\nif (!apiKey) throw new Error('MEM0_API_KEY is not set');\nconst memory = new Mem0Memory({ apiKey, sessionId });","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Fail fast at boot with an explicit env assertion instead of letting the constructor throw mid-request","Add MEM0_API_KEY to every deployment environment (Vercel/Lambda/Docker) in the same change that adds Mem0Memory","apiKey must be top-level in the constructor fields, not nested in mem0Options"],"tags":["langchain","integration","validation","api-key","typescript"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}