Silgi
Integrations

SvelteKit

Use Silgi with SvelteKit API routes — catch-all endpoint with full type safety.

SvelteKit uses file-based API routes. Silgi integrates via a catch-all +server.ts.

Setup

// src/routes/api/rpc/[...path]/+server.ts
import {  } from 'silgi/sveltekit'
import {  } from '$lib/server/rpc'

const  = (, {
  : '/api/rpc',
  : () => ({
    : getDB(),
    : .locals.user,
  }),
})

export const  = 
export const  = 

Context from SvelteKit hooks

Use SvelteKit's handle hook to set event.locals, then access in the context factory:

// src/hooks.server.ts
export async function ({ ,  }) {
  .locals.user = await getUser(.cookies.get('session'))
  return ()
}
// src/routes/api/rpc/[...path]/+server.ts
const  = silgiSvelteKit(appRouter, {
  : () => ({
    : .locals.user,
  }),
})

What's next?

  • Server — standalone serve() as an alternative
  • Integrations — other available integrations

On this page