Skip to content
You are reading the v2 docs, currently in beta.V1 docs
oRPC
Esc
navigateopen⌘Jpreview
On this page

SvelteKit Adapter

Use oRPC inside a SvelteKit project by mounting a handler in an endpoint.

SvelteKit is a framework for rapidly developing robust, performant web applications using Svelte. Its endpoints follow the Fetch API, so oRPC integrates through the Fetch API Adapter.

Server

import type { RequestHandler } from './$types'
import { onError } from '@orpc/server'
import { RPCHandler } from '@orpc/server/fetch'

const handler = new RPCHandler(router, {
  interceptors: [
    onError((error) => {
      console.error(error)
    }),
  ],
})

const handle: RequestHandler = async ({ request }) => {
  const { response } = await handler.handle(request, {
    prefix: '/rpc',
    context: {} // Provide initial context if needed
  })

  return response ?? new Response('Not found', { status: 404 })
}

export const GET = handle
export const POST = handle
export const PUT = handle
export const PATCH = handle
export const DELETE = handle

Client

During SSR, use SvelteKit’s fetch, which forwards the cookie and authorization headers and calls the endpoint directly without an HTTP round trip.

import { RPCLink } from '@orpc/client/fetch'

const link = new RPCLink({
  url: '/rpc',
  fetch: async (url, init) => {
    if (import.meta.env.SSR) {
      const { getRequestEvent } = await import('$app/server')
      return getRequestEvent().fetch(url, init)
    }

    return fetch(url, init)
  },
})

Optimize SSR

SvelteKit’s fetch already skips the HTTP round trip, but requests are still serialized and deserialized. To remove that overhead as well, use a server-side client during SSR as described in Optimizing SSR.

Last updated on September 11, 2026

Was this page helpful?