Elysia Web Framework

1.4.28 · active · verified Sun Apr 19

Elysia is a high-performance, ergonomic web framework built specifically for the Bun runtime, emphasizing end-to-end type safety and an exceptional developer experience. It provides a comprehensive set of features for building web servers and APIs, including robust routing, middleware, declarative schema validation (powered by `@sinclair/typebox`), and integrated WebSocket support. The framework is currently stable at version 1.4.28 and maintains a rapid release cadence, frequently incorporating improvements and bug fixes, often in synergy with Bun's development. Its core differentiators include native integration with Bun for superior performance, extensive TypeScript support providing compile-time and runtime type integrity, and a strong focus on developer productivity. Elysia aims to offer a unified type system where types serve as a single source of truth across the application, from API definition to automatic documentation generation, streamlining development and reducing errors.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart sets up a basic Elysia server with a GET route, a POST route with schema validation using `t` (TypeBox), and a simple WebSocket endpoint. It demonstrates common routing, body parsing, and schema definition patterns, then starts the server on port 3000.

import { Elysia, t } from 'elysia'

const app = new Elysia()
  .get('/', () => 'Hello Elysia!')
  .post('/greet', ({ body }) => `Hello, ${body.name}!`, {
    body: t.Object({
      name: t.String({
        minLength: 1,
        description: 'The name of the person to greet.'
      })
    }),
    detail: {
      summary: 'Greets a person by name',
      description: 'Accepts a JSON body with a `name` property and returns a greeting.'
    }
  })
  .ws('/chat', {
    message(ws, message) {
      ws.send(`Echo: ${message}`);
    },
    open(ws) {
      console.log('WebSocket client connected');
      ws.send('Welcome to the chat!');
    },
    close() {
      console.log('WebSocket client disconnected');
    }
  })
  .listen(3000, () => {
    console.log(`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`);
  });

export type App = typeof app;

view raw JSON →