Skip to content

Connect Your App to Supabase

Connect a Supabase project to your app, choose the right credentials, and protect data with row-level security.

Supabase combines managed PostgreSQL with authentication, file storage, and realtime subscriptions. Choose it when your app needs those services alongside a relational schema. The browser client uses the project URL and a public API key, while a server-side Postgres client uses a private connection string.

Connect a Supabase project

Create or select a project in Supabase

Use the project that will hold your application’s database and files. Selecting Supabase in Studio records the provider choice, but doesn’t create the Supabase project or populate its credentials.

Set the client environment variables

For the Next.js example below, set NEXT_PUBLIC_SUPABASE_URL and NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY to the project’s URL and publishable key. If your existing app uses a legacy anon key, keep its existing public variable such as NEXT_PUBLIC_SUPABASE_ANON_KEY until you switch the client to a publishable key.

Check access to the tables

Enable row-level security (RLS) on exposed tables and write policies for the rows each user should be able to read or change. Test with both a signed-in user and a signed-out client before relying on the query in your app.

Next.js exposes NEXT_PUBLIC_ values to the browser. Keep Postgres connection strings, secret keys, and legacy service_role keys in the server environment.

This example uses @supabase/supabase-js to read orders with their related items, newest first. It assumes your database defines the relationship and its access policies allow the query to return those rows.

import { createClient } from "@supabase/supabase-js";

const url = process.env.NEXT_PUBLIC_SUPABASE_URL;
const key = process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY;
if (!url || !key) throw new Error("Set the public Supabase URL and key");

export const supabase = createClient(url, key);

export async function fetchOrders() {
  const { data, error } = await supabase
    .from("orders")
    .select("*, items(*)")
    .order("created_at", { ascending: false });

  if (error) throw error;
  return data;
}

Check permissions before changing a query

If the orders query returns a permissions error, check whether the table is exposed to the Data API and whether the calling role has read access. If it succeeds but returns no rows, check whether that user’s row-level policies allow them to see the test orders. Verify read access on the related items table too.

Choose the services you need

Use Supabase Auth for Google, GitHub, or email and password sign-in. Use Storage buckets to upload and serve images, user media, and documents. If a view should update when data changes, use Realtime subscriptions. Apply schema changes to Postgres separately from configuring those services.

Next

Was this page helpful?