📄 SKILL.md

← Vault

name: supabase-edge-functions-safe-rest

description: Safe Supabase Edge Functions REST API access pattern — avoids schema cache staleness, handles INSERT errors, and configures external CRM connections.


Supabase Edge Functions — Safe REST API Access Pattern

The Problem

Supabase Edge Functions (Deno) sometimes need to call the REST API internally (PostgREST at /rest/v1/). Using native fetch with the service role key sometimes fails with:

Important: The Edge Function must return only minimal safe data (e.g., allowed: true/false), never raw database rows that might contain sensitive info.

Auth admin operations (password reset, etc.)

`typescript

const supabase = createClient(url, key);

// Works for: updateUser, admin.listUsers, etc.

`

Note: The service role JWT (SUPABASE_SERVICE_ROLE_KEY env var) grants access to auth/v1/admin/* endpoints. The management API token (sbp_...) is for api.supabase.comdifferent endpoint, different purpose, cannot be used for row-level data access.

CRM external connections

When an Edge Function needs to call an external Supabase project:

Required secrets in Edge Function config:

`

EXTERNAL_SUPABASE_URL=https://[project-ref].supabase.co

EXTERNAL_SUPABASE_ANON_KEY=eyJ...

EXTERNAL_SUPABASE_SERVICE_ROLE_KEY=eyJ...

`

Without these, CRM sync functions return "Credenciais do banco externo não configuradas."

Deploy command

`bash

cd /var/www/comercialrs

SUPABASE_ACCESS_TOKEN="sbp_..." supabase functions deploy --no-verify-jwt

`