mastra-ai/mastra · error · Error

No credentials

Error message

No credentials

What it means

Inside authenticatedFetch's single-flight token refresh, the CLI dynamically imports credentials helpers and calls loadCredentials. If no stored credentials exist at all, it throws 'No credentials', meaning the request cannot be refreshed because there is nothing to refresh from. Callers of platformFetch surface this as an unauthenticated-request failure.

Source

Thrown at packages/cli/src/commands/auth/client.ts:137

    if (authHeader?.startsWith('Bearer ')) {
      _currentToken = authHeader.slice(7);
    }
  }

  const response = await fetch(input, init);

  if (response.status !== 401 || !_currentToken) {
    return response;
  }

  // Avoid multiple concurrent refreshes
  if (!_refreshInFlight) {
    _refreshInFlight = (async () => {
      try {
        // Dynamic import to avoid circular dependency
        const { tryRefreshToken, loadCredentials } = await import('./credentials.js');
        const creds = await loadCredentials();
        if (!creds) throw new Error('No credentials');

        const newToken = await tryRefreshToken(creds);
        if (!newToken) throw new Error('Refresh failed');

        _currentToken = newToken;
        return newToken;
      } finally {
        _refreshInFlight = null;
      }
    })();
  }

  let newToken: string;
  try {
    newToken = await _refreshInFlight;
  } catch {
    // Refresh failed — return the original 401 response
    return response;

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Run `mastra auth login` to create credentials.
  2. Set MASTRA_API_TOKEN in the environment for non-interactive environments.
  3. Verify HOME/XDG paths point to the profile that holds ~/.mastra credentials in CI.
  4. Mount or restore the credentials file in ephemeral CI environments.

Example fix

// before (CI yaml)
- run: mastra deploys list
// after
- run: mastra deploys list
  env:
    MASTRA_API_TOKEN: ${{ secrets.MASTRA_API_TOKEN }}
Defensive patterns

Strategy: validation

Validate before calling

import { existsSync } from 'node:fs';
import { homedir } from 'node:os';
import path from 'node:path';
const credsPath = path.join(homedir(), '.mastra', 'credentials.json');
if (!existsSync(credsPath) && !process.env.MASTRA_API_TOKEN) {
  throw new Error('No credentials: run `mastra auth login` or set MASTRA_API_TOKEN');
}

Try / catch

try {
  await platformFetch(url, init);
} catch (err) {
  if (err instanceof Error && err.message === 'No credentials') {
    console.error('Authenticate first: `mastra auth login` or set MASTRA_API_TOKEN');
    return;
  }
  throw err;
}

Prevention

When it happens

Trigger: authenticatedFetch detects a 401 (or stale token) and enters the refresh path, but loadCredentials() resolves null because no credentials file exists and no fallback auth was configured.

Common situations: Running CLI commands on a fresh machine/CI container without ever running `mastra auth login`; HOME differs so the credentials file path is not found; credentials file deleted by cleanup scripts; non-interactive CI without MASTRA_API_TOKEN.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/7eefc351cb5d6910. Report an issue: GitHub.