--- title: 'Use Supabase with SvelteKit' subtitle: 'Learn how to create a Supabase project, add some sample data to your database, and query the data from a SvelteKit app.' breadcrumb: 'Framework Quickstarts' --- <$Partial path="quickstart_db_setup.mdx" /> ## 3. Create a SvelteKit app [#create-an-app] Create a SvelteKit app using the `sv` CLI. ```bash npx sv create my-app ``` ## 4. Set up AI tooling (optional) [#set-up-ai-tooling] <$Partial path="quickstart_ai_tooling.mdx" /> ## 5. Install the Supabase client library [#install-the-client-library] Use the `supabase-js` client library to query and update your Supabase data from a SvelteKit app. Navigate to the SvelteKit app and install `supabase-js`. ```bash cd my-app && npm install @supabase/supabase-js ``` ## 6. Declare Supabase environment variables [#declare-environment-variables] At the root of your project, create a `.env` file and add your Supabase connection variables. Get them from the following helper, or from the project [**Connect** panel](/dashboard/project/_?showConnect=true&framework=sveltekit&connectTab=frameworks): ```dotenv name=.env PUBLIC_SUPABASE_URL= PUBLIC_SUPABASE_PUBLISHABLE_KEY= ``` <$Partial path="api_settings.mdx" variables={{ "framework": "sveltekit", "tab": "frameworks" }} /> ## 7. Create the Supabase client [#create-the-client] In your SvelteKit app, create a `src/lib` directory, create a file called `supabaseClient.js`, and add the following code to initialize the Supabase client: <$CodeTabs> ```js name=src/lib/supabaseClient.js import { createClient } from '@supabase/supabase-js' import { PUBLIC_SUPABASE_PUBLISHABLE_KEY, PUBLIC_SUPABASE_URL } from '$env/static/public' export const supabase = createClient(PUBLIC_SUPABASE_URL, PUBLIC_SUPABASE_PUBLISHABLE_KEY) ``` ```ts name=src/lib/supabaseClient.ts import { createClient } from '@supabase/supabase-js' import { PUBLIC_SUPABASE_PUBLISHABLE_KEY, PUBLIC_SUPABASE_URL } from '$env/static/public' export const supabase = createClient(PUBLIC_SUPABASE_URL, PUBLIC_SUPABASE_PUBLISHABLE_KEY) ``` ## 8. Query data from the app [#query-data] Use the `load` function to fetch the data server-side and display the query results as a list. In the `src/routes` directory, create a `+page.server.js` file with the following code. <$CodeTabs> ```js name=src/routes/+page.server.js import { supabase } from '$lib/supabaseClient' export async function load() { const { data, error } = await supabase.from('instruments').select() if (error) { console.error('Error loading instruments:', error.message) return { instruments: [], error: error.message } } return { instruments: data ?? [], error: null, } } ``` ```ts name=src/routes/+page.server.ts import { supabase } from '$lib/supabaseClient' import type { PageServerLoad } from './$types' type Instrument = { id: number name: string } export const load: PageServerLoad = async () => { const { data, error } = await supabase.from('instruments').select<'*', Instrument>() if (error) { console.error('Error loading instruments:', error.message) return { instruments: [], error: error.message } } return { instruments: data ?? [], error: null, } } ``` In `src/routes/+page.svelte`, replace the existing content with the following code. ```svelte name=src/routes/+page.svelte {#if data.error}

Error loading instruments: {data.error}

{:else}
    {#each data.instruments as instrument}
  • {instrument.name}
  • {/each}
{/if} ``` ## 9. Start the app [#start-the-app] Start the app and go to `http://localhost:5173` in a browser. The page lists the instruments. ```bash npm run dev ``` <$Partial path="quickstart_going_to_production.mdx" /> ## Next steps Build on this app with the following resources: - Set up [Auth](/docs/guides/auth) for your app - [Insert more data](/docs/guides/database/import-data) into your database - Upload and serve static files using [Storage](/docs/guides/storage)