mastra-ai/mastra · error · Error

Supabase URL and anon key are required, please provide them

Error message

Supabase URL and anon key are required, please provide them in the options or set the environment variables SUPABASE_URL and SUPABASE_ANON_KEY

What it means

The Supabase auth provider requires a project URL and anon key to create its Supabase client. Both are resolved from options.url/options.anonKey or SUPABASE_URL/SUPABASE_ANON_KEY. If either is missing, construction throws because the provider cannot function without a client.

Source

Thrown at auth/supabase/src/index.ts:22

import { createClient } from '@supabase/supabase-js';
import type { SupabaseClient, User } from '@supabase/supabase-js';

interface MastraAuthSupabaseOptions extends MastraAuthProviderOptions<User> {
  url?: string;
  anonKey?: string;
}

export class MastraAuthSupabase extends MastraAuthProvider<User> {
  protected supabase: SupabaseClient;

  constructor(options?: MastraAuthSupabaseOptions) {
    super({ name: options?.name ?? 'supabase' });

    const supabaseUrl = options?.url ?? process.env.SUPABASE_URL;
    const supabaseAnonKey = options?.anonKey ?? process.env.SUPABASE_ANON_KEY;

    if (!supabaseUrl || !supabaseAnonKey) {
      throw new Error(
        'Supabase URL and anon key are required, please provide them in the options or set the environment variables SUPABASE_URL and SUPABASE_ANON_KEY',
      );
    }

    this.supabase = createClient(supabaseUrl, supabaseAnonKey);

    this.registerOptions(options);
  }

  async authenticateToken(token: string): Promise<User | null> {
    const { data, error } = await this.supabase.auth.getUser(token);

    if (error) {
      return null;
    }

    return data.user;
  }

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Set both SUPABASE_URL and SUPABASE_ANON_KEY environment variables
  2. Pass them explicitly: new MastraAuthSupabase({ url: 'https://xyz.supabase.co', anonKey: '...' })
  3. Confirm dotenv/config or platform env loading runs before provider construction
  4. Copy the correct values from Supabase dashboard > Project Settings > API

Example fix

// before
const auth = new MastraAuthSupabase({ url: process.env.SUPABASE_URL });
// after
const auth = new MastraAuthSupabase({
  url: process.env.SUPABASE_URL,
  anonKey: process.env.SUPABASE_ANON_KEY,
});
Defensive patterns

Strategy: validation

Validate before calling

function assertSupabaseConfig(opts) {
  const url = opts?.url ?? process.env.SUPABASE_URL;
  const key = opts?.anonKey ?? process.env.SUPABASE_ANON_KEY;
  if (!url || !key) throw new Error('Supabase config missing: set SUPABASE_URL and SUPABASE_ANON_KEY');
  if (!url.startsWith('https://') || !url.includes('.supabase.co')) throw new Error('SUPABASE_URL is not a valid Supabase project URL');
  return { url, key };
}

Type guard

function hasSupabaseOptions(o) {
  return typeof o === 'object' && o !== null &&
    typeof o.url === 'string' && o.url.length > 0 &&
    typeof o.anonKey === 'string' && o.anonKey.length > 0;
}

Try / catch

let auth;
try {
  auth = new MastraAuthSupabase(options);
} catch (e) {
  if (e.message.includes('Supabase URL and anon key')) {
    throw new ConfigError('Missing SUPABASE_URL / SUPABASE_ANON_KEY — check env loading');
  }
  throw e;
}

Prevention

When it happens

Trigger: `new MastraAuthSupabase(options)` (or super() in the provider) where url or anonKey is undefined and the matching SUPABASE_URL / SUPABASE_ANON_KEY env vars are unset or empty.

Common situations: Missing .env loading in the server entrypoint; env vars not copied to the deployment; using the service key var name (SUPABASE_SERVICE_KEY) instead of SUPABASE_ANON_KEY; typo in option key (e.g. {supabaseUrl}).

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


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