#!/usr/bin/env python3 """ Quick script to verify your Supabase API key format and connection. """ import os from dotenv import load_dotenv load_dotenv() url = os.getenv("SUPABASE_URL") key = os.getenv("SUPABASE_SERVICE_KEY") print("=" * 70) print("Supabase API Key Verification") print("=" * 70) print() if not url: print("❌ SUPABASE_URL is not set in .env file") exit(1) if not key: print("❌ SUPABASE_SERVICE_KEY is not set in .env file") exit(1) # Clean the key key = key.strip() print(f"📋 SUPABASE_URL: {url[:30]}...") print(f"📋 SUPABASE_SERVICE_KEY: {key[:20]}...{key[-10:] if len(key) > 30 else ''} ({len(key)} chars)") print() # Check key format issues = [] if not key.startswith("eyJ"): issues.append("❌ Key doesn't start with 'eyJ' (not a JWT token)") if len(key) < 100: issues.append(f"❌ Key is too short ({len(key)} chars, expected ~200+)") if len(key) > 500: issues.append(f"⚠️ Key is unusually long ({len(key)} chars)") if " " in key or "\n" in key or "\t" in key: issues.append("❌ Key contains whitespace (spaces, newlines, tabs)") if key.startswith('"') or key.endswith('"'): issues.append("❌ Key is wrapped in quotes (remove quotes from .env)") if key.startswith("'") or key.endswith("'"): issues.append("❌ Key is wrapped in single quotes (remove quotes from .env)") if issues: print("⚠️ Issues found with API key format:") for issue in issues: print(f" {issue}") print() else: print("✅ Key format looks good!") print() # Try to connect print("🔗 Testing connection to Supabase...") try: from supabase import create_client client = create_client(url, key) # Try a simple query try: client.table("admin_rules").select("id").limit(0).execute() print("✅ Connection successful! API key is valid.") print() print("💡 Next steps:") print(" 1. Make sure tables exist (run SQL scripts in Supabase)") print(" 2. Run: python migrate_sqlite_to_supabase.py") except Exception as e: error_str = str(e) if "Invalid API key" in error_str or "401" in error_str: print("❌ Connection failed: Invalid API key") print() print("🔧 How to fix:") print(" 1. Go to https://app.supabase.com") print(" 2. Select your project") print(" 3. Go to Settings → API") print(" 4. Find 'service_role' key (NOT 'anon' key)") print(" 5. Click 'Reveal' to show the full key") print(" 6. Copy the ENTIRE key (it's very long)") print(" 7. Update SUPABASE_SERVICE_KEY in .env file") print(" 8. Make sure NO quotes or spaces around the value") elif "does not exist" in error_str or "relation" in error_str.lower(): print("⚠️ Connection works, but table doesn't exist yet") print(" This is OK - create tables first, then migrate") else: print(f"❌ Connection error: {error_str}") except ImportError: print("❌ Supabase Python client not installed") print(" Run: pip install supabase") except Exception as e: print(f"❌ Error: {e}") print() print("=" * 70)