Get started with Server Workers in 10 minutes

This quickstart guide will take you from an empty project to a server worker which can interact with your Jazz application.

Create your SvelteKit App

We'll be using SvelteKit for simplicity, but you can use any framework you like.

You can accept the defaults for all the questions, or customise the project as you like.

No content available for tab:

Requires Node.js 20+

Install Jazz

The jazz-tools package includes everything you're going to need to build your first Jazz server worker.

No content available for tab:

Set your API key

Sign up for a free API key at dashboard.jazz.tools for higher limits or production use, or use your email address as a temporary key to get started quickly.

.env
No content available for tab:

Define your schema

We're going to define a simple schema for our server worker. We'll use the root on the worker to store a list of bands. We're also going to add a migration to initialise the root if it doesn't exist.

src/lib/schema.ts
import { co, z } from "jazz-tools";

export const Band = co.map({
  name: z.string(),
});

export const BandList = co.list(Band);

export const JazzFestWorkerAccount = co
  .account({
    root: co.map({
      bandList: BandList,
    }),
    profile: co.profile(),
  })
  .withMigration((account) => {
    if (!account.$jazz.has("root")) {
      account.$jazz.set("root", {
        bandList: [],
      });
      account.root?.$jazz.owner.makePublic();
    }
  });

If you're continuing from the front-end Quickstart, you can extend your existing schema.

Create a Server Worker

Jazz provides a CLI to create server workers. You can create a server worker using the following command:

No content available for tab:

You can copy the output of this command and paste it directly into your .env file:

.env
No content available for tab:

Your JAZZ_WORKER_SECRET should never be exposed to the client.

Defining your HTTP request schema

Next, we're going to set up an HTTP request schema to define our request and response. Here, we tell Jazz that we will send a Band under the key band and expect a bandList in response, which is a list of Bands.

We also need to tell Jazz which keys should be treated as loaded in the request and response using the resolve query.

src/lib/announceBandSchema.ts
No content available for tab:

Configure your Server Worker

We're going to use the startWorker function to start our server worker, and register a POST handler, which will listen for the requests being sent to our server worker.

We'll also use a resolve query here to make sure that the bandList is loaded on the worker's root.

src/routes/api/announce-band/+server.ts
No content available for tab:

Start your server worker

We can now start our development server to make sure everything is working.

No content available for tab:

If you open your browser, you should see the default SvelteKit welcome page.

Not working?

  • Check you set up your .env file correctly with PUBLIC_ where necessary
  • Check you're importing startWorker from jazz-tools/worker
Still stuck?Ask for help on Discord!

Send requests to your server worker

Creating a Jazz Client

If you already have a working provider from the frontend quickstart, you can skip this step.

We're going to wrap our SvelteKit app in a JazzSvelteProvider so that we can use Jazz on our client.

src/routes/+layout.svelte
No content available for tab:

Creating your page component

We're going to send a request to our server worker to announce a new band. Our worker will respond with a list of bands that we can display on our page.

src/routes/+page.svelte
No content available for tab:

Try it out!

Your browser should now be showing you a page with an input field and a button. If you enter a band name and click the button, your server worker will receive the request and add the band to the list.

Congratulations! 🎉 You've just built your first Jazz server worker!

This simple pattern is the foundation for building powerful, real-time applications.

Here are some ideas about what you could use your server worker for:

  • integrating with payment providers
  • sending emails/SMSes
  • gathering data from external APIs
  • managing authoritative state

Looking forward to seeing what you build!

Next steps