Skip to content
Getting Started

Use Supabase with Refine

Learn how to create a Supabase project, add some sample data to your database, and query the data from a Refine app.


1. Create a Supabase project#

Before you can use Supabase, you need a Supabase project. You can create a project visually in the Dashboard or programmatically using the Management API.

Create a new Supabase project from the Dashboard of any organization you belong to.

2. Set up your database#

When your Supabase project is up and running, create an instruments table with some sample data.

Then set a secure baseline by setting only the privileges each Postgres role needs, add Row Level Security (RLS) for enhanced security for database data by default, and create an RLS policy to make the data in your table publicly readable.

You can click this button to prefill all the SQL needed in the SQL editor of your project in the Dashboard.

Prefill SQL

3. Create a Refine app#

Create a Refine app using the create refine-app.

The refine-supabase preset adds @refinedev/supabase supplementary package that supports Supabase in a Refine app. @refinedev/supabase out-of-the-box includes the Supabase dependency: supabase-js.

1
npm create refine-app@latest -- --preset refine-supabase my-app

Refine welcome page

Get API details#

To interact with data in database tables, you use the client libraries that wrap the auto-generated Data API endpoints, authenticating using the Project URL and key from the project Connect dialog.

Project URL
Publishable key

4. Update supabaseClient with environment variables#

Update the supabaseClient with the SUPABASE_URL and SUPABASE_KEY of your Supabase API, which you can get from the helper above, or from the project Connect panel. The supabaseClient is used in auth provider and data provider methods that allow the Refine app to connect to your Supabase backend.

src/utility/supabaseClient.ts
1
import { createClient } from '@refinedev/supabase'
2
3
const SUPABASE_URL = '<your-supabase-url>'
4
const SUPABASE_KEY = '<your-supabase-publishable-key>'
5
6
export const supabaseClient = createClient(SUPABASE_URL, SUPABASE_KEY, {
7
db: {
8
schema: 'public',
9
},
10
auth: {
11
persistSession: true,
12
},
13
})

5. Add instruments resource and pages#

Use the following code to automatically add resources and generate code for the pages to show the instruments data using Refine Inferencer.

This defines pages for list, create, show and edit actions inside the src/pages/instruments/ directory with a <HeadlessInferencer /> component.

The <HeadlessInferencer /> component depends on @refinedev/react-table and @refinedev/react-hook-form packages. To avoid errors, you should install them as dependencies with npm install @refinedev/react-table @refinedev/react-hook-form.

1
npm run refine create-resource instruments

6. Add routes for instruments pages#

Add routes for the list, create, show, and edit pages.

src/App.tsx
1
import { Refine } from '@refinedev/core'
2
import { RefineKbar, RefineKbarProvider } from '@refinedev/kbar'
3
import routerProvider, {
4
DocumentTitleHandler,
5
NavigateToResource,
6
UnsavedChangesNotifier,
7
} from '@refinedev/react-router'
8
import { dataProvider, liveProvider } from '@refinedev/supabase'
9
import { BrowserRouter, Route, Routes } from 'react-router-dom'
10
11
import './App.css'
12
13
import authProvider from './authProvider'
14
import {
15
InstrumentsCreate,
16
InstrumentsEdit,
17
InstrumentsList,
18
InstrumentsShow,
19
} from './pages/instruments'
20
import { supabaseClient } from './utility'
21
22
function App() {
23
return (
24
<BrowserRouter>
25
<RefineKbarProvider>
26
<Refine
27
dataProvider={dataProvider(supabaseClient)}
28
liveProvider={liveProvider(supabaseClient)}
29
authProvider={authProvider}
30
routerProvider={routerProvider}
31
options={{
32
syncWithLocation: true,
33
warnWhenUnsavedChanges: true,
34
}}
35
resources={[
36
{
37
name: 'instruments',
38
list: '/instruments',
39
create: '/instruments/create',
40
edit: '/instruments/edit/:id',
41
show: '/instruments/show/:id',
42
},
43
]}
44
>
45
<Routes>
46
<Route index element={<NavigateToResource resource="instruments" />} />
47
<Route path="/instruments">
48
<Route index element={<InstrumentsList />} />
49
<Route path="create" element={<InstrumentsCreate />} />
50
<Route path="edit/:id" element={<InstrumentsEdit />} />
51
<Route path="show/:id" element={<InstrumentsShow />} />
52
</Route>
53
</Routes>
54
<RefineKbar />
55
<UnsavedChangesNotifier />
56
<DocumentTitleHandler />
57
</Refine>
58
</RefineKbarProvider>
59
</BrowserRouter>
60
)
61
}
62
63
export default App

7. View instruments pages#

Start the app with the following command:

1
npm run dev

Open http://localhost:5173/instruments in a browser, and you should be able to see the instruments pages along the /instruments routes. You can edit and add new instruments using the Inferencer generated UI.

The Inferencer auto-generated code gives you a good starting point on which to keep building your list, create, show and edit pages. You can get these by clicking the Show the auto-generated code buttons in their respective pages.