{"record":{"id":"caaac68b0869c443","repo":"mem0ai/mem0","slug":"api-key-is-invalid","errorCode":null,"errorMessage":"API Key is invalid","messagePattern":"API Key is invalid","errorType":"http","errorClass":"APIError","httpStatus":null,"severity":"error","filePath":"mem0-ts/src/client/mem0.ts","lineNumber":296,"sourceCode":"\n  async ping(): Promise<void> {\n    try {\n      const response = await this._fetchWithErrorHandling(\n        `${this.host}/v1/ping/`,\n        {\n          method: \"GET\",\n          headers: {\n            Authorization: `Token ${this.apiKey}`,\n          },\n        },\n      );\n\n      if (!response || typeof response !== \"object\") {\n        throw new APIError(\"Invalid response format from ping endpoint\");\n      }\n\n      if (response.status !== \"ok\") {\n        throw new APIError(response.message || \"API Key is invalid\");\n      }\n\n      const { orgId, projectId, userEmail } = response;\n\n      if (orgId) this.organizationId = orgId;\n      if (projectId) this.projectId = projectId;\n      if (userEmail) this.telemetryId = userEmail;\n    } catch (error: any) {\n      // Pass through structured exceptions and APIError\n      if (error instanceof MemoryError || error instanceof APIError) {\n        throw error;\n      } else {\n        throw new APIError(\n          `Failed to ping server: ${error.message || \"Unknown error\"}`,\n        );\n      }\n    }\n  }","sourceCodeStart":278,"sourceCodeEnd":314,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0-ts/src/client/mem0.ts#L278-L314","documentation":"The ping endpoint answered with a JSON object whose `status` field is not 'ok', and the client throws APIError with the server's `message` field or the fallback 'API Key is invalid'. The server itself rejected the credentials — this is the authoritative 'your key is wrong/expired' signal from Mem0's hosted API.","triggerScenarios":"new MemoryClient({ apiKey }) where apiKey is revoked, expired, copied with a missing prefix/suffix, or belongs to a different environment (staging key vs prod host). Ping runs during initialization, so the error typically surfaces on the first awaited client call.","commonSituations":"Rotated keys after a leak; key from a deleted org; copy-paste truncation at line wraps; swapping host (self-hosted vs cloud) without swapping the key.","solutions":["Regenerate the API key in the Mem0 dashboard and update your env/secret store.","Confirm the key matches the host: platform keys for api.mem0.ai, server-specific credentials for self-hosted.","Re-copy the key carefully — no leading/trailing characters — and store it without shell-quoted quotes.","Check the server's message field in the thrown APIError for the exact rejection reason."],"exampleFix":"# before\nMEM0_API_KEY=m0-truncated-wrongkey\n\n# after\nMEM0_API_KEY=m0-full-newly-regenerated-key","handlingStrategy":"try-catch","validationCode":"async function verifyApiKey(host = 'https://api.mem0.ai', apiKey: string): Promise<void> {\n  const res = await fetch(`${host}/v1/ping/`, { headers: { Authorization: `Token ${apiKey}` } });\n  const body = await res.json();\n  if (body?.status !== 'ok') throw new Error(`Mem0 rejected the key: ${body?.message ?? res.status}`);\n}\n\nawait verifyApiKey(host, apiKey); // fail fast at startup","typeGuard":"const isPingOk = (v: unknown): v is { status: 'ok'; orgId?: string; projectId?: string } =>\n  isJsonObject(v) && (v as { status?: string }).status === 'ok';","tryCatchPattern":"try {\n  await client.users();\n} catch (e) {\n  if (e instanceof Error && /API Key is invalid/.test(e.message)) {\n    // credential problem — regenerate key; retrying with the same key will not help\n    throw new Error('Mem0 API key rejected: rotate the key and update configuration');\n  }\n  throw e;\n}","preventionTips":["Run a ping check at application startup so bad keys fail deploys, not user requests.","Store keys in a secret manager and rotate on schedule.","Match keys to environments — never mix platform and self-hosted credentials."],"tags":["authentication","api-key","ping"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}