Skip to main content

Svelte Quickstart

Learn how to query data from Convex in a Svelte app.

  1. 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
  2. Install the Convex client and server library

    To get started, install the convex and convex-svelte packages.

    cd my-app && npm install convex convex-svelte
  3. Customize the convex path

    SvelteKit doesn't like referencing code outside of source, so customize the convex functionsDir to be under src/.

    "functions": "src/convex/"
  4. 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. The dev command will then continue running to sync your functions with your dev deployment in the cloud.

    npx convex dev
  5. Create sample data for your database

    In a new terminal window, create a sampleData.jsonl file with some sample data.

    {"text": "Buy groceries", "isCompleted": true}
    {"text": "Go for a swim", "isCompleted": true}
    {"text": "Integrate Convex", "isCompleted": false}
  6. 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 the import command.

    npx convex import --table tasks sampleData.jsonl
  7. Expose a database query

    Add a new file tasks.ts in the convex/ 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.

    import { query } from "./_generated/server";

    export const get = query({
    args: {},
    handler: async (ctx) => {
    const tasks = await ctx.db.query("tasks").collect();
    return => ({ ...task, assigner: "tom" }));
  8. 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.

    <script lang="ts">
    import { PUBLIC_CONVEX_URL } from '$env/static/public';
    import { setupConvex } from 'convex-svelte';

    const { children } = $props();

    {@render children()}
  9. Display the data in your app

    In src/routes/+page.svelte use useQuery to subscribe your api.tasks.get API function.

    <script lang="ts">
    import { useQuery } from 'convex-svelte';
    import { api } from '../convex/_generated/api.js';

    const query = useQuery(api.tasks.get, {});

    {#if query.isLoading}
    {:else if query.error}
    failed to load: {query.error.toString()}
    {#each as task}
    {task.isCompleted ? '☑' : '☐'}
    <span>assigned by {task.assigner}</span>
  10. Start the app

    Start the app, open http://localhost:5173 in a browser, and see the list of tasks.

    npm run dev