{"record":{"id":"e4fd82d105ed3808","repo":"mem0ai/mem0","slug":"mem0-api-key-cannot-be-empty","errorCode":null,"errorMessage":"Mem0 API key cannot be empty","messagePattern":"Mem0 API key cannot be empty","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"mem0-ts/src/client/mem0.ts","lineNumber":117,"sourceCode":"  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 = {\n      Authorization: `Token ${this.apiKey}`,\n      \"Content-Type\": \"application/json\",\n    };\n\n    this.client = axios.create({\n      baseURL: this.host,","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0-ts/src/client/mem0.ts#L99-L135","documentation":"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.","triggerScenarios":"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.","commonSituations":"Template .env files committed with blank values; keys pasted with trailing whitespace/newlines; CI secrets defined but empty.","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."],"exampleFix":"# before\nMEM0_API_KEY=\n\n# after\nMEM0_API_KEY=m0-your-real-key","handlingStrategy":"validation","validationCode":"const cleanApiKey = (v: string | undefined): string => {\n  const t = (v ?? '').trim();\n  if (t.length === 0) throw new Error('MEM0_API_KEY is blank — set a real key in the environment');\n  return t;\n};\n\nnew MemoryClient({ apiKey: cleanApiKey(process.env.MEM0_API_KEY) });","typeGuard":"const isNonBlankString = (v: unknown): v is string =>\n  typeof v === 'string' && v.trim().length > 0;","tryCatchPattern":null,"preventionTips":["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."],"tags":["configuration","api-key","validation"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}