mem0ai/mem0 · error · Error
apiKey is required for Mem0Memory
Error message
apiKey is required for Mem0Memory
What it means
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.
Source
Thrown at mem0-ts/src/community/src/integrations/langchain/mem0.ts:174
sessionId: string;
humanPrefix = "Human";
aiPrefix = "AI";
mem0Client: InstanceType<typeof MemoryClient>;
memoryOptions: AddMemoryOptions | SearchMemoryOptions | GetAllMemoryOptions;
mem0Options: ClientOptions;
// 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.
// Defaults to false (return a single HumanMessage) in order to allow more flexibility with different models.
separateMessages?: boolean;
constructor(fields: Mem0MemoryInput) {
if (!fields.apiKey) {
throw new Error("apiKey is required for Mem0Memory");
}
if (!fields.sessionId) {
throw new Error("sessionId is required for Mem0Memory");
}
super({
returnMessages: fields?.returnMessages ?? false,
inputKey: fields?.inputKey,
outputKey: fields?.outputKey,
});
this.apiKey = fields.apiKey;
this.sessionId = fields.sessionId;
this.humanPrefix = fields.humanPrefix ?? this.humanPrefix;
this.aiPrefix = fields.aiPrefix ?? this.aiPrefix;
this.memoryOptions = fields.memoryOptions ?? {};
this.mem0Options = fields.mem0Options ?? {
apiKey: this.apiKey,View on GitHub (pinned to 001c235229)
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
Example fix
// before
const memory = new Mem0Memory({ sessionId: 'user-1' });
// after
const memory = new Mem0Memory({
apiKey: process.env.MEM0_API_KEY!,
sessionId: 'user-1',
}); Defensive patterns
Strategy: validation
Validate before calling
const apiKey = process.env.MEM0_API_KEY;
if (!apiKey) throw new Error('MEM0_API_KEY is not set');
const memory = new Mem0Memory({ apiKey, sessionId }); Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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).
Related errors
- sessionId is required for Mem0Memory
- Failed to initialize Mem0Client. Please check your configura
- Langchain embedder provider requires an initialized Langchai
- Provided Langchain 'instance' in the 'model' field does not
- Langchain provider requires an initialized Langchain instanc
AI-assisted analysis of mem0ai/mem0@001c235229 (2026-08-15).
Data as JSON: /api/errors/266eff1a8a4acb8b.
Report an issue: GitHub.