Always consult [elysiajs.com/llms.txt](https://elysiajs.com/llms.txt) for code examples and latest API. ElysiaJS is a TypeScript framework for building Bun-first (but not limited to Bun) type-safe, high-performance backend servers. This skill provides comprehensive guidance for developing with Elysia, including routing, validation, authentication, plugins, integrations, and deployment. Trigger this skill when the user asks to:
bun create elysia appimport { Elysia, t, status } from 'elysia' const app = new Elysia() .get('/', () => 'Hello World') .post('/user', ({ body }) => body, { body: t.Object({ name: t.String(), age: t.Number() }) }) .get('/id/:id', ({ params: { id } }) => { if(id > 1_000_000) return status(404, 'Not Found') return id }, { params: t.Object({ id: t.Number({ minimum: 1 }) }), response: { 200: t.Number(), 404: t.Literal('Not Found') } }) .listen(3000)
import { Elysia } from 'elysia' new Elysia() .get('/', 'GET') .post('/', 'POST') .put('/', 'PUT') .patch('/', 'PATCH') .delete('/', 'DELETE') .options('/', 'OPTIONS') .head('/', 'HEAD') `### Path Parameters` .get('/user/:id', ({ params: { id } }) => id) .get('/post/:id/:slug', ({ params }) => params) `### Query Parameters` .get('/search', ({ query }) => query.q) // GET /search?q=elysia → "elysia" `### Request Body` .post('/user', ({ body }) => body) `### Headers` .get('/', ({ headers }) => headers.authorization)
import { Elysia, t } from 'elysia' .post('/user', ({ body }) => body, { body: t.Object({ name: t.String(), age: t.Number(), email: t.String({ format: 'email' }), website: t.Optional(t.String({ format: 'uri' })) }) }) `### Nested Objects` body: t.Object({ user: t.Object({ name: t.String(), address: t.Object({ street: t.String(), city: t.String() }) }) }) `### Arrays` body: t.Object({ tags: t.Array(t.String()), users: t.Array(t.Object({ id: t.String(), name: t.String() })) }) `### File Upload` .post('/upload', ({ body }) => body.file, { body: t.Object({ file: t.File({ type: 'image', // image/* mime types maxSize: '5m' // 5 megabytes }), files: t.Files({ // Multiple files type: ['image/png', 'image/jpeg'] }) }) }) `### Response Validation` .get('/user/:id', ({ params: { id } }) => ({ id, name: 'John', email: 'john@example.com' }), { params: t.Object({ id: t.Number() }), response: { 200: t.Object({ id: t.Number(), name: t.String(), email: t.String() }), 404: t.String() } })
import { z } from 'zod' .post('/user', ({ body }) => body, { body: z.object({ name: z.string(), age: z.number().min(0), email: z.string().email() }) }) `## Error Handling` .get('/user/:id', ({ params: { id }, status }) => { const user = findUser(id) if (!user) { return status(404, 'User not found') } return user }) `## Guards (Apply to Multiple Routes)` .guard({ params: t.Object({ id: t.Number() }) }, app => app .get('/user/:id', ({ params: { id } }) => id) .delete('/user/:id', ({ params: { id } }) => id) ) `## Macro` .macro({ hi: (word: string) => ({ beforeHandle() { console.log(word) } }) }) .get('/', () => 'hi', { hi: 'Elysia' })
src/ ├── index.ts # Main server entry ├── modules/ │ ├── auth/ │ │ ├── index.ts # Auth routes (Elysia instance) │ │ ├── service.ts # Business logic │ │ └── model.ts # TypeBox schemas/DTOs │ └── user/ │ ├── index.ts │ ├── service.ts │ └── model.ts └── plugins/ └── custom.ts public/ # Static files (if using static plugin) test/ # Unit tests
onError to handle local custom errorsElysia.models({ ...models }) and prefix model by namespace `Elysia.prefix('model', 'Namespace.')Model.nameModelstatus (import { status } from 'elysia') for errorreturn Error instead of throw Errorlocal (default) - current instance + descendantsscoped - parent + current + descendantsglobal - all instancesprefix, model name will be capitalized by default.OpenAPI Type Gen - function name fromTypes from @elysiajs/openapi for generating OpenAPI from types, see plugins/openapi.mdEden, Eden Treaty - e2e type safe RPC client for share type from backend to frontendroute.md for as it contains the most important foundation building blocks with examples.plugin.md and validation.md is important as well but can be check as needed.bun-fullstack-dev-server.md - Bun Fullstack Dev Server with HMR. React without bundler.cookie.md - Detailed documentation on cookiedeployment.md - Production deployment guide / Dockereden.md - e2e type safe RPC client for share type from backend to frontendguard.md - Setting validation/lifecycle all at oncemacro.md - Compose multiple schema/lifecycle as a reusable Elysia via key-value (recommended for complex setup, eg. authentication, authorization, Role-based Access Check)plugin.md - Decouple part of Elysia into a standalone componentroute.md - Elysia foundation building block: Routing, Handler and Contexttesting.md - Unit tests with examplesvalidation.md - Setup input/output validation and list of all custom validation ruleswebsocket.md - Real-time featuresbearer.md - Add bearer capability to Elysia (@elysiajs/bearer)cors.md - Out of box configuration for CORS (@elysiajs/cors)cron.md - Run cron job with access to Elysia context (@elysiajs/cron)graphql-apollo.md - Integration GraphQL Apollo (@elysiajs/graphql-apollo)graphql-yoga.md - Integration with GraphQL Yoga (@elysiajs/graphql-yoga)html.md - HTML and JSX plugin setup and usage (@elysiajs/html)jwt.md - JWT / JWK plugin (@elysiajs/jwt)openapi.md - OpenAPI documentation and OpenAPI Type Gen / OpenAPI from types (@elysiajs/openapi)opentelemetry.md - OpenTelemetry, instrumentation, and record span utilities (@elysiajs/opentelemetry)server-timing.md - Server Timing metric for debug (@elysiajs/server-timing)static.md - Serve static files/folders for Elysia Server (@elysiajs/static)ai-sdk.md - Using Vercel AI SDK with Elysiaastro.md - Elysia in Astro API routebetter-auth.md - Integrate Elysia with better-authcloudflare-worker.md - Elysia on Cloudflare Worker adapterdeno.md - Elysia on Denodrizzle.md - Integrate Elysia with Drizzle ORMexpo.md - Elysia in Expo API routenextjs.md - Elysia in Nextjs API routenodejs.md - Run Elysia on Node.jsnuxt.md - Elysia on API routeprisma.md - Integrate Elysia with Prismareact-email.d - Create and Send Email with React and Elysiasveltekit.md - Run Elysia on Svelte Kit API routetanstack-start.md - Run Elysia on Tanstack Start / React Queryvercel.md - Deploy Elysia to Vercelbasic.ts - Basic Elysia examplebody-parser.ts - Custom body parser example via .onParsecomplex.ts - Comprehensive usage of Elysia servercookie.ts - Setting cookieerror.ts - Error handlingfile.ts - Returning local file from serverguard.ts - Setting mulitple validation schema and lifecyclemap-response.ts - Custom response mapperredirect.ts - Redirect responserename.ts - Rename context's propertyschema.ts - Setup validationstate.ts - Setup global stateupload-file.ts - File upload with validationwebsocket.ts - Web Socket for realtime communicationpatterns/mvc.md - Detail guideline for using Elysia with MVC patterns