mem0ai/mem0 · error · Error
Mem0 API key cannot be empty
Error message
Mem0 API key cannot be empty
What it means
The third branch of _validateApiKey() rejects strings that are empty after trimming — values like '', ' ', or '\t'. This catches half-configured keys (placeholder strings, whitespace from copy-paste) that would otherwise send a syntactically present but useless Authorization header.
Source
Thrown at mem0-ts/src/client/mem0.ts:117
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 = {
Authorization: `Token ${this.apiKey}`,
"Content-Type": "application/json",
};
this.client = axios.create({
baseURL: this.host,View on GitHub (pinned to 001c235229)
Solutions
- Set a real key: MEM0_API_KEY=m0-... in your .env / secret store.
- Trim on your side at startup: new MemoryClient({ apiKey: process.env.MEM0_API_KEY?.trim() }).
- Remove placeholder/space-only entries from .env templates that get copied into real environments.
Example fix
# before MEM0_API_KEY= # after MEM0_API_KEY=m0-your-real-key
Defensive patterns
Strategy: validation
Validate before calling
const cleanApiKey = (v: string | undefined): string => {
const t = (v ?? '').trim();
if (t.length === 0) throw new Error('MEM0_API_KEY is blank — set a real key in the environment');
return t;
};
new MemoryClient({ apiKey: cleanApiKey(process.env.MEM0_API_KEY) }); Type guard
const isNonBlankString = (v: unknown): v is string => typeof v === 'string' && v.trim().length > 0;
Prevention
- Trim env-provided keys before use.
- Never commit template .env files with real key slots; keep placeholders clearly invalid (e.g. 'replace-me').
- Add a startup assertion that the key is non-blank in every environment.
When it happens
Trigger: Constructing the client with apiKey: ' ' or apiKey: '' — typically from a .env entry with no value (MEM0_API_KEY=) or a placeholder like spaces; dotenv parses MEM0_API_KEY= as empty string, which passes the falsy check for ''? No — '' is falsy so it hits 'required'; ' ' (spaces) is truthy and reaches this branch.
Common situations: Template .env files committed with blank values; keys pasted with trailing whitespace/newlines; CI secrets defined but empty.
Related errors
- Mem0 API key is required
- Provide text or metadata to update
- Mem0 API key must be a string
- organizationId and projectId must be set to access instructi
- apiKey is required for Mem0Memory
AI-assisted analysis of mem0ai/mem0@001c235229 (2026-08-15).
Data as JSON: /api/errors/e4fd82d105ed3808.
Report an issue: GitHub.