📄 SKILL.md

← Vault

name: supabase-anon-key-mismatch-debug

description: Debug 401 Invalid API key on Supabase REST API when Edge Functions still work

triggers: ["401", "Invalid API key", "anon key mismatch", "Supabase REST fails"]


Supabase anon key mismatch diagnostic

When to use

REST API calls from browser return 401 Invalid API key but Edge Functions work fine. Common cause: Lovable regenerates anon key when project settings change, but .env on VPS still has old key.

Diagnostic steps

1. From VPS, test REST directly:

`bash

curl -s 'https://.supabase.co/rest/v1/profiles?select=id&limit=1' \

-H 'apikey: '

`

2. Test Edge Function (uses service role):

`bash

curl -s 'https://.supabase.co/functions/v1/' \

-H 'Authorization: Bearer '

`

3. If Edge Function works but REST fails → anon key is wrong.

4. Get current anon key from Lovable: Project → Settings → Environment → copy VITE_SUPABASE_PUBLISHABLE_KEY

5. Update .env on VPS, restart (no rebuild needed):

`bash

systemctl restart documentos

`

Key discovery

Lovable regenerates anon key silently when you change project settings. The old key becomes invalid for REST API but Edge Functions keep working because they use service role key.

Project reference