Quickstart: SvelteKit
Build the notes app with SvelteKit and Svelte 5: a client for the browser, sign-up and sign-in, and the signed-in user's notes with live updates.
A SvelteKit 2 app (Svelte 5, TypeScript) where people sign up, sign in, add notes and see only their own, with the list updating live. About ten minutes.
#Before you start
Do steps 1 to 3 of the Quickstart first: create a project, copy its project URL and anon key (Quickstart), and create the notes table with its row rules (Quickstart). Every signed-in user then sees and changes only their own notes, whatever the app code does.
#1. Create the app and install the SDK
npx sv create notes --template minimal --types ts
cd notes
npm install @sovaria/sdk#2. Add your project URL and anon key
PUBLIC_SOVARIA_URL=https://<project-ref>.sovarias.in
PUBLIC_SOVARIA_ANON_KEY=<anon-key>Variables starting with PUBLIC_ reach the browser. Both values are safe there: the anon key only does what your row rules allow. Never give the service role key a PUBLIC_ name.
#3. Create the client
import { createClient } from '@sovaria/sdk'
import { env } from '$env/dynamic/public'
export type Note = { id: number; body: string; created_at: string }
// In the browser the session is kept in localStorage and refreshed on its own.
// On the server, make one client per request with { persistSession: false }.
export const sovaria = createClient(env.PUBLIC_SOVARIA_URL!, env.PUBLIC_SOVARIA_ANON_KEY!)#4. Sign in and work with notes
<script lang="ts">
import { onMount } from 'svelte'
import { userMessageFor } from '@sovaria/sdk'
import { sovaria, type Note } from '$lib/sovaria'
let session = $state<ReturnType<typeof sovaria.auth.getSession>>(null)
let notes = $state<Note[]>([])
let error = $state('')
async function load() {
// No "where owner_id = …": the row rules add it for every request.
notes = await sovaria.from<Note>('notes').select('id,body,created_at').order('id', { ascending: false })
}
onMount(() => {
session = sovaria.auth.getSession()
const stopAuth = sovaria.auth.onAuthStateChange((_event, s) => { session = s; if (s) load().catch(show) })
// Live: re-read when a note changes (turn on change capture for notes under Realtime).
const stopLive = sovaria.from('notes').subscribe(() => { if (session) load().catch(show) })
return () => { stopAuth(); stopLive() }
})
function show(err: unknown) { error = userMessageFor(err) }
async function account(e: SubmitEvent) {
e.preventDefault()
const f = new FormData(e.currentTarget as HTMLFormElement)
const email = String(f.get('email')), password = String(f.get('password'))
try {
if ((e.submitter as HTMLButtonElement).value === 'up') await sovaria.auth.signUp(email, password)
else await sovaria.auth.signIn(email, password)
} catch (err) { show(err) }
}
async function add(e: SubmitEvent) {
e.preventDefault()
const form = e.currentTarget as HTMLFormElement
try {
await sovaria.from('notes').insert({ body: String(new FormData(form).get('body')) })
form.reset()
await load()
} catch (err) { show(err) }
}
</script>
{#if !session}
<form onsubmit={account}>
<input name="email" type="email" placeholder="Email" required />
<input name="password" type="password" placeholder="Password" required />
<button value="in">Sign in</button>
<button value="up">Create account</button>
</form>
{:else}
<form onsubmit={add}>
<input name="body" placeholder="Write a note" maxlength="2000" required />
<button>Add</button>
</form>
<ul>{#each notes as n (n.id)}<li>{n.body}</li>{/each}</ul>
<button onclick={() => sovaria.auth.signOut()}>Sign out</button>
{/if}
{#if error}<p role="alert">{error}</p>{/if}signUpcreates the user and signs them in. If the project asks people to confirm their email, it answersemail_verification_required: trueand no session until they click the link (Email and password).userMessageFor(err)gives a message written for end users; it never contains SQL, table or policy names.- Live updates need change capture on for
notes(Realtime). Without it the list still refreshes after each add.
#5. Read on the server
A server load has no browser session. For data anyone may read, use the anon key with a client per request. To read as a user, send their access token and call setAccessToken(token) on that client, so the same rules apply on the server (JavaScript: React, Next.js, Vite and Node).
import { createClient } from '@sovaria/sdk'
import { env } from '$env/dynamic/public'
export async function load() {
const sovaria = createClient(env.PUBLIC_SOVARIA_URL!, env.PUBLIC_SOVARIA_ANON_KEY!, { persistSession: false })
// Only what your rules let anonymous visitors read, e.g. a table with a "for select using (true)" policy.
return { posts: await sovaria.from('posts').select('id,title').order('id', { ascending: false }).limit(20) }
}#6. Run it
npm run dev # http://localhost:5173New projects allow http://localhost:*. Before you deploy, add your site's origin under Project settings › API (Allowed origins (CORS) for browser apps). This page uses SvelteKit 2.
#Next steps
- Check who can read and change
notesbefore you ship: Tutorial: prove your table is private before you ship. - Push new notes to other devices as they happen: Database change events.
- Every SDK method: JavaScript SDK.
- Rather describe a whole app and have Sovariq build the pages, tables and rules? Tutorial: build your first app with Sovariq.
#Coming from Firebase or Supabase?
| Platform | What this is called there |
|---|---|
| Firebase | Firebase with SvelteKit |
| Supabase | Supabase with SvelteKit |
| Sovaria | Quickstart: SvelteKit |
Moving an existing app? Migrate from Supabase or Firebase imports users (with their passwords), data, files and rules.