SvelteKit
Build and query a ZXCV database from a SvelteKit app, then ship it to the edge.
You’ll end with a route that reads live data in a load function and renders it
in +page.svelte.
The anon key is public and safe to ship, because access is enforced server-side by your authorization rules.
Create a project
In the dashboard click Start building, or run zxcv
from your terminal. Open Settings → API and copy your project ref and
anon key.
Create a table
In the SQL editor, create a todos table with a couple of rows:
create table todos (
id bigint generated always as identity primary key,
title text not null,
is_complete boolean not null default false
);
insert into todos (title) values ('Read the docs'), ('Ship an app');Install the SDK
npm
bash npm install @zxcv.build/sdkInitialize the client
Add your keys to .env. SvelteKit exposes variables prefixed with PUBLIC_ to
the browser via $env/static/public, which is exactly what the anon key needs:
PUBLIC_ZXCV_PROJECT=acme-store
PUBLIC_ZXCV_ANON_KEY=your-anon-keyCreate a shared client:
import { createClient } from '@zxcv.build/sdk'
import { PUBLIC_ZXCV_PROJECT, PUBLIC_ZXCV_ANON_KEY } from '$env/static/public'
export const zxcv = createClient({
project: PUBLIC_ZXCV_PROJECT,
anonKey: PUBLIC_ZXCV_ANON_KEY,
})Fetch and render
Load the data in a +page.ts load function — it runs on the server for the
first render and on the client during navigation:
import { zxcv } from '$lib/zxcv'
export async function load() {
const { data: todos } = await zxcv
.from('todos')
.select('id, title, is_complete')
.order('id')
return { todos: todos ?? [] }
}Render what load returned in +page.svelte:
<script lang="ts">
export let data
</script>
<ul>
{#each data.todos as todo (todo.id)}
<li>{todo.title}</li>
{/each}
</ul>Need the query to stay on the server (e.g. to keep a signed-in user’s session
server-only)? Rename the file to +page.server.ts — the load signature is
the same. Want live updates? Subscribe with Realtime
in an onMount inside +page.svelte.
Deploy
Ship to the global edge from your terminal:
npm i -g zxcv
zxcv link acme-store
zxcv deploy --prodEvery push gets a preview URL; --prod promotes to your domain. See
Deploy for custom domains and canaries.
Next steps
- Auth — add sign-in and row-level security
- Realtime — live updates in the browser
- SDK reference · Database