elysiajs
Original:🇺🇸 English
Not Translated
14 scriptsChecked / no sensitive code detected
Create backend with ElysiaJS, a type-safe, high-performance framework.
3installs
Added on
NPX Install
npx skill4agent add saltyaom/vibe-book-rental elysiajsSKILL.md Content
ElysiaJS Development Skill
Always consult elysiajs.com/llms.txt for code examples and latest API.
Overview
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.
When to Use This Skill
Trigger this skill when the user asks to:
- Create or modify ElysiaJS routes, handlers, or servers
- Setup validation with TypeBox or other schema libraries (Zod, Valibot)
- Implement authentication (JWT, session-based, macros, guards)
- Add plugins (CORS, OpenAPI, Static files, JWT)
- Integrate with external services (Drizzle ORM, Better Auth, Next.js, Eden Treaty)
- Setup WebSocket endpoints for real-time features
- Create unit tests for Elysia instances
- Deploy Elysia servers to production
Quick Start
Quick scaffold:
bash
bun create elysia appBasic Server
typescript
import { 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)Basic Usage
HTTP Methods
typescript
import { Elysia } from 'elysia'
new Elysia()
.get('/', 'GET')
.post('/', 'POST')
.put('/', 'PUT')
.patch('/', 'PATCH')
.delete('/', 'DELETE')
.options('/', 'OPTIONS')
.head('/', 'HEAD')Path Parameters
typescript
.get('/user/:id', ({ params: { id } }) => id)
.get('/post/:id/:slug', ({ params }) => params)Query Parameters
typescript
.get('/search', ({ query }) => query.q)
// GET /search?q=elysia → "elysia"Request Body
typescript
.post('/user', ({ body }) => body)Headers
typescript
.get('/', ({ headers }) => headers.authorization)TypeBox Validation
Basic Types
typescript
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
typescript
body: t.Object({
user: t.Object({
name: t.String(),
address: t.Object({
street: t.String(),
city: t.String()
})
})
})Arrays
typescript
body: t.Object({
tags: t.Array(t.String()),
users: t.Array(t.Object({
id: t.String(),
name: t.String()
}))
})File Upload
typescript
.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
typescript
.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()
}
})Standard Schema (Zod, Valibot, ArkType)
Zod
typescript
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
typescript
.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)
typescript
.guard({
params: t.Object({
id: t.Number()
})
}, app => app
.get('/user/:id', ({ params: { id } }) => id)
.delete('/user/:id', ({ params: { id } }) => id)
)Macro
typescript
.macro({
hi: (word: string) => ({
beforeHandle() { console.log(word) }
})
})
.get('/', () => 'hi', { hi: 'Elysia' })Project Structure (Recommended)
Elysia takes an unopinionated approach but based on user request. But without any specific preference, we recommend a feature-based and domain driven folder structure where each feature has its own folder containing controllers, services, and models.
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 testsEach file has its own responsibility as follows:
- Controller (index.ts): Handle HTTP routing, request validation, and cookie.
- Service (service.ts): Handle business logic, decoupled from Elysia controller if possible.
- Model (model.ts): Define the data structure and validation for the request and response.
Best Practice
Elysia is unopinionated on design pattern, but if not provided, we can relies on MVC pattern pair with feature based folder structure.
- Controller:
- Prefers Elysia as a controller for HTTP dependant controller
- For non HTTP dependent, prefers service instead unless explicitly asked
- Use to handle local custom errors
onError - Register Model to Elysia instance via and prefix model by namespace `Elysia.prefix('model', 'Namespace.')
Elysia.models({ ...models }) - Prefers Reference Model by name provided by Elysia instead of using an actual
Model.name
- Service:
- Prefers class (or abstract class if possible)
- Prefers interface/type derive from
Model - Return (
status) for errorimport { status } from 'elysia' - Prefers instead of
return Errorthrow Error
- Models:
- Always export validation model and type of validation model
- Custom Error should be in contains in Model
Elysia Key Concept
Elysia has a every important concepts/rules to understand before use.
Encapsulation - Isolates by Default
Lifecycles (hooks, middleware) don't leak between instances unless scoped.
Scope levels:
- (default) - current instance + descendants
local - - parent + current + descendants
scoped - - all instances
global
ts
.onBeforeHandle(() => {}) // only local instance
.onBeforeHandle({ as: 'global' }, () => {}) // exports to allMethod Chaining - Required for Types
Must chain. Each method returns new type reference.
❌ Don't:
ts
const app = new Elysia()
app.state('build', 1) // loses type
app.get('/', ({ store }) => store.build) // build doesn't exists✅ Do:
ts
new Elysia()
.state('build', 1)
.get('/', ({ store }) => store.build)Explicit Dependencies
Each instance independent. Declare what you use.
ts
const auth = new Elysia()
.decorate('Auth', Auth)
.model(Auth.models)
new Elysia()
.get('/', ({ Auth }) => Auth.getProfile()) // Auth doesn't exists
new Elysia()
.use(auth) // must declare
.get('/', ({ Auth }) => Auth.getProfile())Global scope when:
- No types added (cors, helmet)
- Global lifecycle (logging, tracing)
Explicit when:
- Adds types (state, models)
- Business logic (auth, db)
Deduplication
Plugins re-execute unless named:
ts
new Elysia() // rerun on `.use`
new Elysia({ name: 'ip' }) // runs once across all instancesOrder Matters
Events apply to routes registered after them.
ts
.onBeforeHandle(() => console.log('1'))
.get('/', () => 'hi') // has hook
.onBeforeHandle(() => console.log('2')) // doesn't affect '/'Type Inference
Inline functions only for accurate types.
For controllers, destructure in inline wrapper:
ts
.post('/', ({ body }) => Controller.greet(body), {
body: t.Object({ name: t.String() })
})Get type from schema:
ts
type MyType = typeof MyType.staticReference Model
Model can be reference by name, especially great for documenting an API
ts
new Elysia()
.model({
book: t.Object({
name: t.String()
})
})
.post('/', ({ body }) => body.name, {
body: 'book'
})Model can be renamed by using /
.prefix.suffixts
new Elysia()
.model({
book: t.Object({
name: t.String()
})
})
.prefix('model', 'Namespace')
.post('/', ({ body }) => body.name, {
body: 'Namespace.Book'
})Once , model name will be capitalized by default.
prefixTechnical Terms
The following are technical terms that is use for Elysia:
- - function name
OpenAPI Type GenfromfromTypesfor generating OpenAPI from types, see@elysiajs/openapiplugins/openapi.md - ,
Eden- e2e type safe RPC client for share type from backend to frontendEden Treaty
Resources
Use the following references as needed.
It's recommended to checkout for as it contains the most important foundation building blocks with examples.
route.mdplugin.mdvalidation.mdreferences/
Detailed documentation split by topic:
- - Bun Fullstack Dev Server with HMR. React without bundler.
bun-fullstack-dev-server.md - - Detailed documentation on cookie
cookie.md - - Production deployment guide / Docker
deployment.md - - e2e type safe RPC client for share type from backend to frontend
eden.md - - Setting validation/lifecycle all at once
guard.md - - Compose multiple schema/lifecycle as a reusable Elysia via key-value (recommended for complex setup, eg. authentication, authorization, Role-based Access Check)
macro.md - - Decouple part of Elysia into a standalone component
plugin.md - - Elysia foundation building block: Routing, Handler and Context
route.md - - Unit tests with examples
testing.md - - Setup input/output validation and list of all custom validation rules
validation.md - - Real-time features
websocket.md
plugins/
Detailed documentation, usage and configuration reference for official Elysia plugin:
- - Add bearer capability to Elysia (
bearer.md)@elysiajs/bearer - - Out of box configuration for CORS (
cors.md)@elysiajs/cors - - Run cron job with access to Elysia context (
cron.md)@elysiajs/cron - - Integration GraphQL Apollo (
graphql-apollo.md)@elysiajs/graphql-apollo - - Integration with GraphQL Yoga (
graphql-yoga.md)@elysiajs/graphql-yoga - - HTML and JSX plugin setup and usage (
html.md)@elysiajs/html - - JWT / JWK plugin (
jwt.md)@elysiajs/jwt - - OpenAPI documentation and OpenAPI Type Gen / OpenAPI from types (
openapi.md)@elysiajs/openapi - - OpenTelemetry, instrumentation, and record span utilities (
opentelemetry.md)@elysiajs/opentelemetry - - Server Timing metric for debug (
server-timing.md)@elysiajs/server-timing - - Serve static files/folders for Elysia Server (
static.md)@elysiajs/static
integrations/
Guide to integrate Elysia with external library/runtime:
- - Using Vercel AI SDK with Elysia
ai-sdk.md - - Elysia in Astro API route
astro.md - - Integrate Elysia with better-auth
better-auth.md - - Elysia on Cloudflare Worker adapter
cloudflare-worker.md - - Elysia on Deno
deno.md - - Integrate Elysia with Drizzle ORM
drizzle.md - - Elysia in Expo API route
expo.md - - Elysia in Nextjs API route
nextjs.md - - Run Elysia on Node.js
nodejs.md - - Elysia on API route
nuxt.md - - Integrate Elysia with Prisma
prisma.md - - Create and Send Email with React and Elysia
react-email.d - - Run Elysia on Svelte Kit API route
sveltekit.md - - Run Elysia on Tanstack Start / React Query
tanstack-start.md - - Deploy Elysia to Vercel
vercel.md
examples/ (optional)
- - Basic Elysia example
basic.ts - - Custom body parser example via
body-parser.ts.onParse - - Comprehensive usage of Elysia server
complex.ts - - Setting cookie
cookie.ts - - Error handling
error.ts - - Returning local file from server
file.ts - - Setting mulitple validation schema and lifecycle
guard.ts - - Custom response mapper
map-response.ts - - Redirect response
redirect.ts - - Rename context's property
rename.ts - - Setup validation
schema.ts - - Setup global state
state.ts - - File upload with validation
upload-file.ts - - Web Socket for realtime communication
websocket.ts
patterns/ (optional)
- - Detail guideline for using Elysia with MVC patterns
patterns/mvc.md