Svelte Quickstart
Learn how to query data from Convex in a Svelte app.
- Create a SvelteKit app
Create a SvelteKit app using the
npx create svelte@latest
command.Other sets of options will work with the library as long as Svelte 5 is used but for this quickstart guide:
- For "Which Svelte app template," choose "Skeleton project."
- For "Add type checking with TypeScript," choose "Yes, using TypeScript syntax."
- For "Select additional options," enable "Try the Svelte 5 preview."
npm create svelte@latest my-app
- Install the Convex client and server library
To get started, install the
convex
andconvex-svelte
packages.cd my-app && npm install convex convex-svelte
- Customize the convex path
SvelteKit doesn't like referencing code outside of source, so customize the convex functionsDir to be under
src/
.convex.json{
"functions": "src/convex/"
} - Set up a Convex dev deployment
Next, run
npx convex dev
. This will prompt you to log in with GitHub, create a project, and save your production and deployment URLs.It will also create a
convex/
folder for you to write your backend API functions in. Thedev
command will then continue running to sync your functions with your dev deployment in the cloud.npx convex dev
- Create sample data for your database
In a new terminal window, create a
sampleData.jsonl
file with some sample data.sampleData.jsonl{"text": "Buy groceries", "isCompleted": true}
{"text": "Go for a swim", "isCompleted": true}
{"text": "Integrate Convex", "isCompleted": false} - Add the sample data to your database
Now that your project is ready, add a
tasks
table with the sample data into your Convex database with theimport
command.npx convex import --table tasks sampleData.jsonl
- Expose a database query
Add a new file
tasks.ts
in theconvex/
folder with a query function that loads the data.Exporting a query function from this file declares an API function named after the file and the export name,
api.tasks.get
.convex/tasks.tsimport { query } from "./_generated/server";
export const get = query({
args: {},
handler: async (ctx) => {
const tasks = await ctx.db.query("tasks").collect();
return tasks.map((task) => ({ ...task, assigner: "tom" }));
},
}); - Set up Convex
Create a new file
src/routes/+layout.svelte
and set up the Convex client there to make it available on every page of your app.src/routes/+layout.svelte<script lang="ts">
import { PUBLIC_CONVEX_URL } from '$env/static/public';
import { setupConvex } from 'convex-svelte';
const { children } = $props();
setupConvex(PUBLIC_CONVEX_URL);
</script>
{@render children()} - Display the data in your app
In
src/routes/+page.svelte
useuseQuery
to subscribe yourapi.tasks.get
API function.src/routes/page.tsx<script lang="ts">
import { useQuery } from 'convex-svelte';
import { api } from '../convex/_generated/api.js';
const query = useQuery(api.tasks.get, {});
</script>
{#if query.isLoading}
Loading...
{:else if query.error}
failed to load: {query.error.toString()}
{:else}
<ul>
{#each query.data as task}
<li>
{task.isCompleted ? '☑' : '☐'}
<span>{task.text}</span>
<span>assigned by {task.assigner}</span>
</li>
{/each}
</ul>
{/if} - Start the app
Start the app, open http://localhost:5173 in a browser, and see the list of tasks.
npm run dev