Skip to main content
RapidDev - Software Development Agency
supabase-tutorial

How to Connect Supabase to Netlify

To connect Supabase to Netlify, add your Supabase environment variables (SUPABASE_URL and SUPABASE_ANON_KEY) in the Netlify Dashboard under Site Settings > Environment Variables. Configure your build command and publish directory. For serverless functions that need the service role key, add SUPABASE_SERVICE_ROLE_KEY as a server-only variable. Netlify does not have an official Supabase integration like Vercel, so you configure environment variables manually.

What you'll learn

  • How to add Supabase environment variables to a Netlify project
  • How to configure build settings for frameworks that use Supabase
  • How to access Supabase from Netlify serverless functions securely
  • How to set up deploy previews that work with Supabase
Book a free consultation
4.9Clutch rating
600+Happy partners
17+Countries served
190+Team members
Beginner8 min read10-15 minSupabase (all plans), Netlify (all plans), any JavaScript frameworkMarch 2026RapidDev Engineering Team
TL;DR

To connect Supabase to Netlify, add your Supabase environment variables (SUPABASE_URL and SUPABASE_ANON_KEY) in the Netlify Dashboard under Site Settings > Environment Variables. Configure your build command and publish directory. For serverless functions that need the service role key, add SUPABASE_SERVICE_ROLE_KEY as a server-only variable. Netlify does not have an official Supabase integration like Vercel, so you configure environment variables manually.

Connecting Supabase to a Netlify Deployment

Netlify is a popular deployment platform for static sites and serverless applications. Unlike Vercel, Netlify does not have a built-in Supabase integration that auto-syncs environment variables. Instead, you manually add your Supabase API URL and keys as environment variables in the Netlify Dashboard. This tutorial covers the full setup including client-side configuration, serverless functions, and framework-specific build settings.

Prerequisites

  • A Supabase project with API keys (found in Dashboard > Settings > API)
  • A Netlify account with a deployed site or a site ready to deploy
  • A frontend project using @supabase/supabase-js
  • Your project's Git repository connected to Netlify

Step-by-step guide

1

Find your Supabase API credentials

In the Supabase Dashboard, go to Settings > API (or Project Settings > API). You will find three values you need: the Project URL (e.g., https://abcdefgh.supabase.co), the anon public key (safe for client-side use, respects RLS), and the service role key (server-only, bypasses RLS). Copy these values — you will add them to Netlify in the next step. Never expose the service role key in client-side code.

Expected result: You have your Supabase Project URL, anon key, and service role key copied and ready to add to Netlify.

2

Add environment variables in the Netlify Dashboard

Go to your Netlify site Dashboard, click Site Settings > Environment Variables (or Site Configuration > Environment Variables on newer Netlify UI). Click Add a Variable and add each Supabase credential. The variable names depend on your framework: Next.js uses NEXT_PUBLIC_ prefix, Vite uses VITE_ prefix, and plain JavaScript uses any name. Add the variables for all deploy contexts (production, deploy previews, branch deploys) unless you want different values per context.

typescript
1# For Next.js projects:
2NEXT_PUBLIC_SUPABASE_URL=https://abcdefgh.supabase.co
3NEXT_PUBLIC_SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIs...
4
5# For Vite projects (React, Vue, Svelte):
6VITE_SUPABASE_URL=https://abcdefgh.supabase.co
7VITE_SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIs...
8
9# For server-side only (Netlify Functions):
10SUPABASE_SERVICE_ROLE_KEY=eyJhbGciOiJIUzI1NiIs...

Expected result: Environment variables are saved in Netlify and will be available at build time and in serverless functions.

3

Configure your build settings for the framework

In Site Settings > Build & Deploy, verify your build command and publish directory match your framework. Netlify auto-detects most frameworks, but you may need to adjust settings. The build command compiles your app, and the publish directory is where the output goes. Make sure environment variables with the correct prefix are accessible during the build process.

typescript
1# Next.js
2# Build command: next build
3# Publish directory: .next
4
5# Vite (React, Vue, Svelte)
6# Build command: npm run build
7# Publish directory: dist
8
9# Create React App
10# Build command: npm run build
11# Publish directory: build
12
13# SvelteKit
14# Build command: npm run build
15# Publish directory: build

Expected result: Your site builds successfully on Netlify with Supabase environment variables available during the build.

4

Initialize the Supabase client in your application code

In your frontend code, create the Supabase client using the environment variables you configured. The exact variable name depends on your framework. For client-side code, always use the anon key — never the service role key. The Supabase client will automatically include the anon key in API requests, and your RLS policies will control access.

typescript
1// For Next.js
2import { createClient } from '@supabase/supabase-js'
3
4export const supabase = createClient(
5 process.env.NEXT_PUBLIC_SUPABASE_URL!,
6 process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!
7)
8
9// For Vite
10import { createClient } from '@supabase/supabase-js'
11
12export const supabase = createClient(
13 import.meta.env.VITE_SUPABASE_URL,
14 import.meta.env.VITE_SUPABASE_ANON_KEY
15)

Expected result: The Supabase client connects to your project and can perform queries, authentication, and storage operations.

5

Access Supabase from Netlify serverless functions

For server-side operations that need to bypass RLS (like admin actions or webhook handlers), use Netlify Functions with the service role key. Create functions in the netlify/functions/ directory. Environment variables are automatically available in functions via process.env. Use the service role key to create a Supabase admin client that bypasses Row Level Security — but never expose this client or key to the browser.

typescript
1// netlify/functions/admin-action.ts
2import { createClient } from '@supabase/supabase-js'
3
4export default async function handler(event: any) {
5 // Service role client bypasses RLS
6 const supabaseAdmin = createClient(
7 process.env.NEXT_PUBLIC_SUPABASE_URL!,
8 process.env.SUPABASE_SERVICE_ROLE_KEY!
9 )
10
11 // This query bypasses all RLS policies
12 const { data, error } = await supabaseAdmin
13 .from('users_private')
14 .select('*')
15
16 if (error) {
17 return { statusCode: 500, body: JSON.stringify({ error: error.message }) }
18 }
19
20 return {
21 statusCode: 200,
22 body: JSON.stringify(data),
23 }
24}

Expected result: The serverless function connects to Supabase using the service role key and can perform privileged operations.

6

Configure redirect rules for SPA routing

If your app uses client-side routing (React Router, Vue Router), you need to configure Netlify redirects so that all routes serve your index.html file. Without this, refreshing the page on a deep link like /dashboard returns a 404 error. Create a _redirects file in your public directory or add a redirect rule to netlify.toml. This is especially important for Supabase auth callback URLs that redirect back to your app after OAuth or magic link login.

typescript
1# Option 1: public/_redirects
2/* /index.html 200
3
4# Option 2: netlify.toml
5[[redirects]]
6 from = "/*"
7 to = "/index.html"
8 status = 200

Expected result: All routes serve the SPA correctly, and auth callback URLs redirect properly after login.

Complete working example

netlify-supabase-setup.ts
1// Complete Supabase client configuration for a Netlify deployment
2// Works with Vite-based projects (React, Vue, Svelte)
3
4import { createClient } from '@supabase/supabase-js'
5
6// Client-side Supabase client (uses anon key, respects RLS)
7const supabaseUrl = import.meta.env.VITE_SUPABASE_URL
8const supabaseAnonKey = import.meta.env.VITE_SUPABASE_ANON_KEY
9
10if (!supabaseUrl || !supabaseAnonKey) {
11 throw new Error(
12 'Missing Supabase environment variables. ' +
13 'Add VITE_SUPABASE_URL and VITE_SUPABASE_ANON_KEY ' +
14 'in Netlify Site Settings > Environment Variables.'
15 )
16}
17
18export const supabase = createClient(supabaseUrl, supabaseAnonKey)
19
20// ======================================
21// Example: Netlify serverless function
22// File: netlify/functions/get-stats.ts
23// ======================================
24// import { createClient } from '@supabase/supabase-js'
25//
26// export default async function handler() {
27// const supabaseAdmin = createClient(
28// process.env.VITE_SUPABASE_URL!,
29// process.env.SUPABASE_SERVICE_ROLE_KEY!
30// )
31//
32// const { count } = await supabaseAdmin
33// .from('projects')
34// .select('*', { count: 'exact', head: true })
35//
36// return {
37// statusCode: 200,
38// headers: { 'Content-Type': 'application/json' },
39// body: JSON.stringify({ totalProjects: count }),
40// }
41// }
42
43// ======================================
44// netlify.toml configuration
45// ======================================
46// [build]
47// command = "npm run build"
48// publish = "dist"
49//
50// [[redirects]]
51// from = "/*"
52// to = "/index.html"
53// status = 200

Common mistakes when connecting Supabase to Netlify

Why it's a problem: Using the wrong environment variable prefix for the framework (e.g., VITE_ in a Next.js project)

How to avoid: Next.js requires NEXT_PUBLIC_ prefix for client-side variables. Vite requires VITE_ prefix. Plain Node.js has no prefix requirement. Match the prefix to your build tool.

Why it's a problem: Exposing the SUPABASE_SERVICE_ROLE_KEY in client-side code

How to avoid: Only use the service role key in Netlify Functions (server-side). For client-side code, use the anon key which is safe because RLS policies control access.

Why it's a problem: Not triggering a redeploy after adding environment variables in Netlify

How to avoid: Environment variables are injected at build time. After adding or changing variables, trigger a new deploy from the Netlify Dashboard or push a new commit.

Why it's a problem: Forgetting to update the Supabase auth redirect URL from localhost to the Netlify domain

How to avoid: In Supabase Dashboard > Authentication > URL Configuration, add your Netlify domain (e.g., https://your-site.netlify.app) to the Redirect URLs list.

Best practices

  • Always use the anon key for client-side code and the service role key only in Netlify Functions
  • Set environment variables for all deploy contexts (production, deploy previews) to ensure previews work correctly
  • Add SPA redirect rules in _redirects or netlify.toml for client-side routing to work after page refresh
  • Update Supabase auth redirect URLs when your Netlify domain changes or when adding custom domains
  • Use Netlify's context-specific environment variables to point deploy previews at a staging Supabase project
  • Add a build check that verifies Supabase environment variables are set before building

Still stuck?

Copy one of these prompts to get a personalized, step-by-step explanation.

ChatGPT Prompt

I need to deploy my React app that uses Supabase to Netlify. Walk me through adding the environment variables, configuring the build, setting up redirects for SPA routing, and creating a Netlify Function that uses the Supabase service role key.

Supabase Prompt

Connect my Vite + React + Supabase app to Netlify. Show me how to add environment variables in the Netlify Dashboard, configure the Supabase client with VITE_ prefixed variables, set up _redirects for SPA routing, and update the Supabase auth redirect URLs.

Frequently asked questions

Does Netlify have a built-in Supabase integration like Vercel?

No. Unlike Vercel, Netlify does not have an official Supabase integration that auto-syncs environment variables. You need to add Supabase credentials manually in the Netlify Dashboard under Environment Variables.

Can I use different Supabase projects for production and deploy previews?

Yes. In Netlify, you can set context-specific environment variables. Set production variables pointing to your production Supabase project and deploy preview variables pointing to a staging project.

Why is my Supabase connection failing after deploying to Netlify?

Check that your environment variables are set correctly in Netlify and that you triggered a redeploy after adding them. Verify the variable prefix matches your framework (NEXT_PUBLIC_ for Next.js, VITE_ for Vite).

Can I use Supabase Edge Functions with Netlify?

Yes. Supabase Edge Functions run on Supabase's infrastructure, not Netlify's. Deploy them with the Supabase CLI (supabase functions deploy) and call them from your Netlify-hosted frontend using supabase.functions.invoke().

How do I handle OAuth callbacks on Netlify?

Add your Netlify domain to the Redirect URLs in Supabase Dashboard > Authentication > URL Configuration. For example, add https://your-site.netlify.app and https://your-site.netlify.app/auth/callback.

Do Netlify Functions support the Supabase JS client?

Yes. Install @supabase/supabase-js as a dependency and import it in your Netlify Functions. Environment variables set in the Netlify Dashboard are available via process.env in functions.

Can RapidDev help me deploy my Supabase app to Netlify?

Yes. RapidDev can configure your Netlify deployment, set up environment variables, create serverless functions, and ensure your Supabase integration works correctly across all deploy contexts.

RapidDev

Talk to an Expert

Our team has built 600+ apps. Get personalized help with your project.

Book a free consultation

Need help with your project?

Our experts have built 600+ apps and can accelerate your development. Book a free consultation — no strings attached.

Book a free consultation

We put the rapid in RapidDev

Need a dedicated strategic tech and growth partner? Discover what RapidDev can do for your business! Book a call with our team to schedule a free, no-obligation consultation. We'll discuss your project and provide a custom quote at no cost.