mem0ai/mem0 · error · APIError

API Key is invalid

Error message

API Key is invalid

What it means

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.

Source

Thrown at mem0-ts/src/client/mem0.ts:296

  async ping(): Promise<void> {
    try {
      const response = await this._fetchWithErrorHandling(
        `${this.host}/v1/ping/`,
        {
          method: "GET",
          headers: {
            Authorization: `Token ${this.apiKey}`,
          },
        },
      );

      if (!response || typeof response !== "object") {
        throw new APIError("Invalid response format from ping endpoint");
      }

      if (response.status !== "ok") {
        throw new APIError(response.message || "API Key is invalid");
      }

      const { orgId, projectId, userEmail } = response;

      if (orgId) this.organizationId = orgId;
      if (projectId) this.projectId = projectId;
      if (userEmail) this.telemetryId = userEmail;
    } catch (error: any) {
      // Pass through structured exceptions and APIError
      if (error instanceof MemoryError || error instanceof APIError) {
        throw error;
      } else {
        throw new APIError(
          `Failed to ping server: ${error.message || "Unknown error"}`,
        );
      }
    }
  }

View on GitHub (pinned to 001c235229)

Solutions

  1. Regenerate the API key in the Mem0 dashboard and update your env/secret store.
  2. Confirm the key matches the host: platform keys for api.mem0.ai, server-specific credentials for self-hosted.
  3. Re-copy the key carefully — no leading/trailing characters — and store it without shell-quoted quotes.
  4. Check the server's message field in the thrown APIError for the exact rejection reason.

Example fix

# before
MEM0_API_KEY=m0-truncated-wrongkey

# after
MEM0_API_KEY=m0-full-newly-regenerated-key
Defensive patterns

Strategy: try-catch

Validate before calling

async function verifyApiKey(host = 'https://api.mem0.ai', apiKey: string): Promise<void> {
  const res = await fetch(`${host}/v1/ping/`, { headers: { Authorization: `Token ${apiKey}` } });
  const body = await res.json();
  if (body?.status !== 'ok') throw new Error(`Mem0 rejected the key: ${body?.message ?? res.status}`);
}

await verifyApiKey(host, apiKey); // fail fast at startup

Type guard

const isPingOk = (v: unknown): v is { status: 'ok'; orgId?: string; projectId?: string } =>
  isJsonObject(v) && (v as { status?: string }).status === 'ok';

Try / catch

try {
  await client.users();
} catch (e) {
  if (e instanceof Error && /API Key is invalid/.test(e.message)) {
    // credential problem — regenerate key; retrying with the same key will not help
    throw new Error('Mem0 API key rejected: rotate the key and update configuration');
  }
  throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of mem0ai/mem0@001c235229 (2026-08-15). Data as JSON: /api/errors/caaac68b0869c443. Report an issue: GitHub.