Articles

How to add a Python backend to a Lovable app without leaving Supabase

Connect a Lovable frontend to FastAPI on Gregale, keep Supabase Auth and Postgres, and verify private PDF downloads with two test users.

Updated

You can connect a Lovable frontend to a Python backend without moving your database or changing how users sign in. Keep Lovable for the UI and your existing Supabase project for Auth and Postgres. Run FastAPI on Gregale for the part that needs Python.

Get the runnable example on GitHub.

This example adds a Download PDF button. A signed-in user can export their report; another signed-in user calling the same URL gets a 404. FastAPI verifies the session with Supabase and reads the row using that user's token. Postgres row-level security (RLS) decides whether the row is visible.

1. Check which backend your Lovable project uses

This guide starts with a Lovable app connected to a Supabase project you control. Use a disposable copy or a new test project, not your production project. If your app uses Lovable Cloud, its managed backend is different: this example does not assume you can copy the credentials or reuse the same backend URL. Check whether your project uses Lovable Cloud or Supabase first.

Download the example files. Create two synthetic test users, A and B, in Supabase. Run setup.sql in the SQL editor, then uncomment the seed statement and replace its user UUID with A's Auth ID. The script exposes one test table to authenticated reads and lets each user read only their own rows. B starts with no rows.

2. Deploy the FastAPI service

Install and sign in to the Gregale CLI. From the unzipped directory, choose a unique app name. Use the HTTPS Supabase project URL, its publishable key, and the exact origin of your published Lovable test frontend (scheme and host, no trailing slash).

bash
APP="my-unique-lovable-pdf-demo"
gregale deploy --create-only --name "$APP"
gregale env set --app "$APP" \
  SUPABASE_URL="https://YOUR_PROJECT.supabase.co" \
  SUPABASE_PUBLISHABLE_KEY="sb_publishable_REPLACE_ME" \
  FRONTEND_ORIGIN="https://YOUR_PUBLISHED_FRONTEND_HOST"
gregale deploy --name "$APP"
gregale app update "$APP" --visibility public --public-auth open --no-require-authn

Use this ingress setting on the disposable demo app only. The browser must be able to reach FastAPI without a Gregale credential; FastAPI protects the PDF route with Supabase authentication. Never put a Gregale account key, a Supabase secret key, or a service_role key in Lovable frontend code. This example doesn't provision a new database. Check Gregale plan limits before sizing your own app; this recipe is not a Free-plan guarantee.

3. Add the button in Lovable

Copy downloadReport.ts from the example into your frontend. Adjust its Supabase import to point to the client your app already uses, and replace apiOrigin with the actual HTTPS URL printed by the Gregale deployment. Use a URL you control and never take it from a query string or user input: the function sends a signed-in user's bearer token there.

In your Lovable project, add a Download PDF button that calls downloadReport(demoReportId) from that file. Show its error message if the request fails. Publish the change to your test frontend. No new auth client or login page is needed.

The button reads the current session with getSession() only to obtain the access token. FastAPI checks that token with Supabase Auth, then forwards it with the project publishable key to the Supabase Data API. The service generates the PDF in memory, marks it private, no-store, and returns it without persisting a copy.

4. Prove that the right user gets the right file

Use two separate browser sessions and the same demo report ID. Test the button from the published frontend origin, not just from a terminal.

SessionExpected result
Signed in as AThe button downloads report.pdf. Open it and check for “April demo report.”
Signed in as BThe same button shows “Report not found.” The request returns 404 without a PDF.
Signed outThe button asks you to sign in. A direct request to the PDF URL with no bearer token returns 401.

If the browser reports a CORS error, check its actual origin in DevTools. Lovable editor previews and custom domains may have different origins. This sample allows exactly the configured origin and the Authorization header. Don't use wildcard origins as a shortcut. A 200 from /healthz tells you the app started; it doesn't tell you the login, database policy, or PDF export works.

If the export returns 502, check that the Supabase Data API exposes the public schema, that the table exists, and that the authenticated SELECT grant and policy are present. Don't disable RLS or use an elevated key to get the demo working.

If your app only needs simple CRUD or TypeScript logic, you might not need a second backend. Add FastAPI when there's a concrete reason, such as a Python PDF library, a data transformation, or an API you already maintain. For the underlying auth and row-access pattern, see FastAPI with Supabase Auth and Postgres.

After testing, remove the disposable app and Supabase project if you no longer need them.

Questions

Can a Lovable app use FastAPI as its Python backend?

Yes. The Lovable frontend can call a separate HTTPS FastAPI service while keeping its existing Supabase Auth and Postgres.

Do I need to move users or data out of Supabase?

No. If your Lovable app uses a Supabase project you control, the browser can send the current Supabase access token to FastAPI, which reads through the Data API under that user’s permissions.