Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/elysiajs/documentation/llms.txt

Use this file to discover all available pages before exploring further.

SvelteKit server routes expose a request object that conforms to the Web Standard API, making Elysia a natural fit. You define your Elysia server in a catch-all route file and export a fallback function that delegates requests to app.handle.
1

Create the catch-all server route file

Create src/routes/[...slugs]/+server.ts in your SvelteKit project.
2

Define your Elysia server and export the fallback handler

// src/routes/[...slugs]/+server.ts
import { Elysia, t } from 'elysia'

const app = new Elysia()
  .get('/', 'hello SvelteKit')
  .post('/', ({ body }) => body, {
    body: t.Object({
      name: t.String()
    })
  })

interface WithRequest {
  request: Request
}

export const fallback = ({ request }: WithRequest) => app.handle(request)

Route prefix

If you place your server route in a subdirectory (e.g. src/routes/api/[...slugs]/+server.ts), set the prefix option on your Elysia server to match:
// src/routes/api/[...slugs]/+server.ts
import { Elysia, t } from 'elysia'

const app = new Elysia({ prefix: '/api' })
  .get('/', () => 'hi')
  .post('/', ({ body }) => body, {
    body: t.Object({
      name: t.String()
    })
  })

type RequestHandler = (v: { request: Request }) => Response | Promise<Response>

export const fallback: RequestHandler = ({ request }) => app.handle(request)

pnpm peer dependencies

If you use pnpm, install peer dependencies manually:
pnpm add @sinclair/typebox openapi-types
See SvelteKit Routing for further reference on SvelteKit server routes.

Build docs developers (and LLMs) love