Integration with Astro
With Astro Endpoint, we can run Elysia on Astro directly.
Install an Astro server adapter for your deployment target. Then:
- Set output to server in astro.config.mjs
import { defineConfig } from 'astro/config'
// https://astro.build/configexport default defineConfig({ output: 'server'})- Create src/pages/[…slugs].ts
- Create or import an existing Elysia server in […slugs].ts
- Export the handler with the name of the method you want to expose
import { Elysia, t } from 'elysia'
const app = new Elysia() .get('/api', () => 'hi') .post('/api', { body: t.Object({ name: t.String() }) }, ({ body }) => body)
const handle = ({ request }: { request: Request }) => app.handle(request)
export const GET = handleexport const POST = handleElysia will work normally as expected because of WinterTC compliance.
We recommend Astro on Bun when running on Bun.
With this approach, you can have co-location of both frontend and backend in a single repository and have End-to-end type-safety with Eden.
If your pnpm configuration does not install peer dependencies, add the peers required by Elysia v2 explicitly:
pnpm add typebox exact-mirror openapi-typesPrefix
Section titled “Prefix”If you place an Elysia server not in the root of the pages directory, you need to annotate the prefix on the Elysia server.
For example, if you place the Elysia server in src/pages/api/[…slugs].ts, you need to annotate the prefix as /api on the Elysia server.
import { Elysia, t } from 'elysia'
const app = new Elysia({ prefix: '/api' }) .get('/', () => 'hi') .post('/', { body: t.Object({ name: t.String() }) }, ({ body }) => body)
const handle = ({ request }: { request: Request }) => app.handle(request)
export const GET = handleexport const POST = handleThis will ensure that Elysia routing works properly wherever you place it.
Please refer to Astro Endpoints for more information.