supabase-fastapi-gateway
Minimal FastAPI gateway template for your web app/website which uses Supabase.
Right now your frontend probably calls Supabase directly, e.g.:
https://xxxxx.supabase.co/functions/v1/generate-report
https://xxxxx.supabase.co/functions/v1/report-edit
https://xxxxx.supabase.co/functions/v1/get-report
With this gateway, the same calls become:
https://api.yourdomain.com/v1/reports/generate
https://api.yourdomain.com/v1/reports/edit
https://api.yourdomain.com/v1/reports/get
It is intentionally small. Copy it into an existing React/Vite + Supabase app, then let an AI coding agent do the migration.
Frontend -> FastAPI gateway -> Supabase
api.yourdomain.com
Why?
This is not primarily a security fix. Calling Supabase directly from the browser with a publishable/anon key is completely normal. Supabase explicitly designs for it, as long as Row Level Security is enabled and correctly configured. The real security risks are weak RLS policies or accidentally exposing the service_role key, and a gateway does not magically solve either of those.
What a gateway does give you is a better long-term architecture and product surface:
api.yourdomain.com/v1/reports/generate reads as a real product API; xxxxx.supabase.co/functions/v1/generate-report reads as plumbing
- partners and customers can integrate against your own stable contract instead of your vendor's URL shape
- rate limiting, audit logging, abuse controls, and route review is clearer.
- migrating vendors later means changing the gateway, not every frontend call site
- you can talk credibly about API ownership and architecture with partners and customers
This template gives you that thin cosmetic API layer without forcing a backend rewrite.
What is included
api-gateway/: FastAPI app with CORS, settings, bearer-token handling, and explicit routes examples
profile example: authenticated user-scoped CRUD
functions example: one named route for one Edge Function
storage example: public asset passthrough from one allow-listed bucket
frontend-example/api-client.ts: TypeScript client with Supabase-like { data, error }
docs/agent-prompt.md: prompt to hand the migration to Codex, Claude Code, Cursor, or another coding agent
What this is not:
- not a replacement for
supabase-js Auth or Realtime
- not a generic proxy
- not a full backend framework
How to use it
Two steps. The AI coding agent does the rest.
1. Create a new branch
I would advise you to create a new branch, that you can later merge with your main branch when everything is migrated and working.
git checkout -b dev-api
2. Get the template into your app
From your app's root folder, run:
git clone https://github.com/Project430/supabase-fastapi-gateway.git
3. Hand it to your AI coding agent
Open your coding agent (Claude Code, Cursor, Codex, etc.) at your app's root and paste this one line:
Read supabase-fastapi-gateway/docs/agent-prompt.md and follow it end to end.
The agent will place the files where they belong, wire up the Supabase environment variables, run the gateway locally, and migrate your Supabase calls one safe domain at a time.
4. Production Deployment
After your AI coding agent finishes the local migration, deploy the gateway to a public API URL.
Recommended production setup:
frontend project -> https://yourdomain.com
API project -> https://api.yourdomain.com
Use a separate deployment project for the API gateway, even if it uses the same GitHub repo.
Recommended:
frontend project root: ./
API project root: api-gateway
This keeps frontend deploys, API deploys, logs, and environment variables separated.
Vercel example :
Create a new Vercel project from the same GitHub repository.
Use:
- Framework Preset: Other
- Root Directory: api-gateway
- Build Command: empty
- Output Directory: empty
- Install Command: empty
Add these environment variables:
- SUPABASE_URL=...
- SUPABASE_ANON_KEY=...
- FRONTEND_ORIGIN=https://yourdomain.com
- ENVIRONMENT=production
Then set your frontend environment variable (on you main vercel project):
VITE_API_URL=https://api.yourdomain.com
Redeploy the frontend after changing VITE_API_URL.
Point your API subdomain to your deployment provider.
By default, your api documentation will not be publicly accessible in production.
Local API docs are available at:
http://localhost:xxxx/docs
/docs
/redoc
/openapi.json
Deploying on Vercel is simple for small gateways. A VPS is better if you need Docker, Nginx, long-running jobs, custom monitoring, or more backend control.
## Repo layout
```text
api-gateway/
app/main.py
app/auth.py
app/config.py
app/routes/profile.py
app/routes/functions.py
app/routes/storage.py
app/services/supabase_rest.py
app/services/supabase_functions.py
app/services/supabase_storage.py
frontend-example/api-client.ts
docs/
License
MIT. See LICENSE.
Field Test: Lightwheel (V1.1)
- project name: Lightwheel
- result: succeeded
- approximate duration / runs: about 6 runs
- what was improved in this repo after the test: shorter agent prompt, leaner intermediate output, lighter verification flow, fewer docs/comments
- main friction discovered: repeated scans and verbose per-run summaries slowed
Next-driven migration
- why this repo update was needed: to make repeated migration runs faster, lower-token, and easier to continue
Field Test: Vodical Landing page (V1.2)
- project name: Vodical Landing page
- result: succeeded
- approximate duration / runs: 1 run
<<<<<<< HEAD
what was improved in this repo after the test: stricter low-token migration prompt with safer Phase 0 cleanup, explicit no-op path, indirect-usage checks, adaptive verification, and stronger cleanup rules main friction discovered: agents could miss hidden Supabase usage in shared helpers and could hesitate or over-scan on tiny/no-op migrations
- what was improved in this repo after the test: stricter low-token migration prompt with safer Phase 0 cleanup, explicit no-op path, indirect-usage checks, adaptive verification, and stronger cleanup rules
- main friction discovered: agents could miss hidden Supabase usage in shared helpers and could hesitate or over-scan on tiny/no-op migrations
8533129 (V2.1)
- why this repo update was needed: to make repeated Next runs faster, more consistent, and less likely to stop early with a false completion state
Field test: Deployment in production (V2.0)
- project name: Lightwheel
<<<<<<< HEAD
result: after a successful migration locally in v1.1, the project successfully deployed in production.
- result: after a successful migration locally in v1.1, the project successfully deployed in production.
8533129 (V2.1)
- what was improved in this repo after the test: GitHub-reported issues are fixed, the deployment guidance
- main friction discovered: the repo needed clearer production deployment guidance
- why this repo update was needed: improve deployment instructions so future migrations are easier to ship without extra trial and error
Admin-protected write routes (V2.1)
Some routes should never be open to the public — for example, a reviews endpoint that lets you push new entries directly from a script without going through a UI. Leaving a write route unauthenticated means any external caller can create, update, or delete records.
This version adds a require_admin dependency and a reviews route as a reference implementation. Write operations (POST, PATCH, DELETE) require admin credentials; the public GET stays open.
Two authentication strategies are supported:
Strategy A — Supabase JWT with admin role (recommended for interactive admin use)
Set app_metadata.role = "admin" on the admin user in Supabase, then call the route with the user's normal Bearer token. No extra environment variables needed. Supabase validates the JWT and your RLS policies govern what the admin can do.
Strategy B — Static API key (for server-to-server calls and automation)
Generate a long random secret and set it in your environment:
ADMIN_API_KEY=your-long-random-secret
SUPABASE_SERVICE_ROLE_KEY=your-supabase-service-role-key
Call the route with Authorization: Bearer <ADMIN_API_KEY>. The gateway verifies the key with a constant-time comparison and forwards the service role key to Supabase, bypassing RLS.
The gateway always checks admin credentials before reaching Supabase, regardless of which strategy you use. This applies to every write endpoint on the reviews router and to any future route that uses Depends(require_admin).
Supabase RLS note: the gateway auth layer is not a substitute for RLS. Configure your reviews table policies to match your chosen strategy so both layers are consistent.