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. Install agent skills (optional)#

Agent Skills are curated instruction sets that give your AI agent procedural knowledge about working with Supabase.

Install them in your project with:

1
npx skills add supabase/agent-skills

3. Set up your database#

When your Supabase project is up and running, create an instruments table with some sample data. Then set 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 the table publicly readable.

Or do this manually in your project's SQL Editor, by pasting the SQL below, and clicking Run.

1
-- Create the table
2
create table instruments (
3
id bigint primary key generated always as identity,
4
name text not null
5
);
6
7
-- Insert sample data into the table
8
insert into instruments (name)
9
values
10
('violin'),
11
('viola'),
12
('cello');
13
14
-- Grant the privileges the role needs, which is read access
15
grant select on public.instruments to anon;
16
17
-- Enable row level security for the table
18
alter table instruments enable row level security;
19
20
-- Create a policy to allow the anon role to read from the instruments table
21
create policy "public can read instruments"
22
on public.instruments
23
for select to anon
24
using (true);

4. 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

5. 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.

Open Connect panel

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
})

6. 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

7. 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

8. 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.