Skip to main content
Available since SvelteKit 2.27. This feature is experimental and subject to change without notice.
Remote functions are a tool for type-safe communication between client and server. They can be called anywhere in your app, but always run on the server, meaning they can safely access server-only modules containing things like environment variables and database clients.

Enabling remote functions

You must opt in by adding the kit.experimental.remoteFunctions option in your svelte.config.js:

Overview

Remote functions are exported from a .remote.js or .remote.ts file and come in four flavors:

query

Read dynamic data from the server

form

Submit form data with progressive enhancement

command

Write data to the server from anywhere

prerender

Prerender static data at build time
On the client, exported functions are transformed to fetch wrappers that invoke their counterparts on the server via a generated HTTP endpoint.

query

The query function allows you to read dynamic data from the server:
Until the promise resolves — and if it errors — the nearest <svelte:boundary> will be invoked.

Query arguments

Query functions can accept an argument. Use a Standard Schema validation library like Zod or Valibot to validate:
Use it in your component:

Refreshing queries

Any query can be re-fetched via its refresh method:
Queries are cached while they’re on the page, meaning getPosts() === getPosts(). You don’t need a reference to update the query.

query.batch

query.batch batches requests that happen within the same macrotask, solving the n+1 problem:
Use it in your component:

form

The form function makes it easy to write data to the server:
The form object contains method and action properties that allow it to work without JavaScript. It also has an attachment that progressively enhances the form when JavaScript is available.

Fields

A form is composed of fields defined by the schema. To get attributes for a field, call its .as(...) method:
Fields can be nested in objects and arrays, with values as:
  • Strings
  • Numbers
  • Booleans
  • File objects

Validation

If submitted data doesn’t pass the schema, each invalid field’s issues() method returns error objects:
You can validate programmatically:

Single-flight mutations

Specify which queries should be refreshed in response to a form submission:

enhance

Customize form submission behavior:

command

The command function allows you to write data to the server from anywhere (unlike form, which is element-specific):
Prefer form where possible, since it gracefully degrades if JavaScript is disabled or fails to load.

Updating queries

Tell SvelteKit which queries need to be refreshed:
Use withOverride for optimistic updates:

prerender

The prerender function is similar to query, except it’s invoked at build time:
Use this for data that changes at most once per redeployment. You can use prerender functions on pages that are otherwise dynamic, allowing for partial prerendering.

Prerender arguments

Specify which values should be prerendered using the inputs option:
By default, prerender functions are excluded from your server bundle. Set dynamic: true to change this:

Using getRequestEvent

Inside remote functions, use getRequestEvent to get the current RequestEvent object:
Some properties of RequestEvent are different inside remote functions. Never use route, params, or url to determine user authorization.

Next steps

Routing

Learn about SvelteKit’s routing system

Load functions

Fetch data for your pages