/supabase-tutorials

How to use Supabase with SvelteKit?

Integrate Supabase with SvelteKit using our step-by-step guide—set up your project, configure credentials, fetch data, and run your app efficiently.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to use Supabase with SvelteKit?

 

Step 1: Set up a new SvelteKit project

 

First, ensure you have Node.js and npm installed. Then create a new SvelteKit project by opening your terminal and running:

npm init svelte@next my-supabase-sveltekit-app

Navigate into the project directory:

cd my-supabase-sveltekit-app

Install the necessary dependencies:

npm install

 

Step 2: Add Supabase to your project

 

Install the Supabase JavaScript client library:

npm install @supabase/supabase-js

Now, you have Supabase available in your SvelteKit project.

 

Step 3: Set up Supabase credentials

 

Create a new file in the root of your project called .env. This file will be used to store your Supabase credentials:


VITE_SUPABASE_URL='your-supabase-url'
VITE_SUPABASE_ANON\_KEY='your-supabase-anon-key'

Replace 'your-supabase-url' and 'your-supabase-anon-key' with the credentials from your Supabase dashboard under the "API" settings.

 

Step 4: Initialize Supabase in your project

 

Create a new file called supabaseClient.js in the src/lib directory with the following content:


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

const supabaseUrl = import.meta.env.VITE_SUPABASE_URL;
const supabaseAnonKey = import.meta.env.VITE_SUPABASE_ANON\_KEY;

export const supabase = createClient(supabaseUrl, supabaseAnonKey);

This file initializes the Supabase client using the environment variables you set in the .env file.

 

Step 5: Use Supabase in a Svelte component

 

Now that Supabase is initialized, you can use it in any Svelte component. For example, to fetch data from a table:

Create a new Svelte file in the src/routes directory, named index.svelte:


<script>
  import { supabase } from '$lib/supabaseClient';

  let data = [];

  const fetchData = async () => {
    const { data: items, error } = await supabase
      .from('your-table-name')
      .select('\*');

    if (error) {
      console.error(error);
    } else {
      data = items;
    }
  };

  fetchData();
</script>

<style>
  /_ Add your styles here _/
</style>

<main>
  <h1>Supabase Data</h1>
  <ul>
    {#each data as item}
      <li>{item.name}</li>
    {/each}
  </ul>
</main>

Make sure to replace 'your-table-name' with the actual table name from your Supabase database.

 

Step 6: Running Your Application

 

To see your SvelteKit and Supabase app in action, start the development server:

npm run dev

Open your browser and navigate to the provided URL, usually http://localhost:3000, to view your application.

 

This will give you a starting point to work with Supabase in a SvelteKit app, allowing you to perform operations such as reading data from your Supabase database.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022