mem0ai/mem0 · error · Error
Mem0 API key must be a string
Error message
Mem0 API key must be a string
What it means
The second branch of _validateApiKey() throws when apiKey is present but not a string — e.g. a number, object, or array. TypeScript types say string, but at runtime (JS callers, JSON configs, env coercion) a non-string can reach the client, and the header-building code would produce garbage auth, so the client rejects it up front.
Source
Thrown at mem0-ts/src/client/mem0.ts:114
const identityByCredentials = new Map<string, Promise<ClientIdentity>>();
export default class MemoryClient {
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",
};View on GitHub (pinned to 001c235229)
Solutions
- Pass the raw string: apiKey: String(process.env.MEM0_API_KEY).
- Quote the value in JSON/YAML config files so it parses as a string.
- If the key arrives as an object (e.g. {token}), extract the string field before constructing the client.
Example fix
// before
const client = new MemoryClient({ apiKey: config.mem0_key }); // config.mem0_key = 4815162342
// after
const client = new MemoryClient({ apiKey: String(config.mem0_key) }); Defensive patterns
Strategy: type-guard
Validate before calling
const asApiKeyString = (v: unknown): string => {
if (typeof v === 'string') return v;
throw new Error(`apiKey must be a string, got ${typeof v}`);
};
new MemoryClient({ apiKey: asApiKeyString(config.mem0_key) }); Type guard
const isStringApiKey = (v: unknown): v is string => typeof v === 'string';
Prevention
- Quote API key values in JSON/YAML so they never parse as numbers.
- If the key may arrive as an object, extract the string field explicitly before construction.
- Validate external config with a schema (zod .string()) before passing to the client.
When it happens
Trigger: Passing apiKey: 12345, apiKey: { token: '...' }, or a Buffer; reading the key from a JSON config as a number; a proxy layer forwarding the key as a non-string type.
Common situations: Numeric-looking keys stored unquoted in JSON/YAML; JWE/JWT objects passed where the raw string is expected; JS callers bypassing the TS types.
Related errors
- Provide text or metadata to update
- Mem0 API key is required
- Mem0 API key cannot be empty
- apiKey is required for Mem0Memory
- Either 'api_key' must be provided or TURBOPUFFER_API_KEY env
AI-assisted analysis of mem0ai/mem0@001c235229 (2026-08-15).
Data as JSON: /api/errors/1eb6ca72fd45c6d9.
Report an issue: GitHub.