DEV Community

Alex Spinov
Alex Spinov

Posted on

Elysia Has a Free API — The Fastest Bun Web Framework

Elysia is a TypeScript web framework built for Bun. With end-to-end type safety, built-in validation via TypeBox, and performance that rivals Go — it's the fastest way to build APIs in JavaScript.

Why Elysia?

  • Fastest JS framework — outperforms Express by 18x in benchmarks
  • End-to-end type safety — types flow from route to response
  • Built-in validation — TypeBox schemas validate request/response automatically
  • Plugin system — JWT, CORS, Swagger, GraphQL built-in

Quick Start

bun create elysia myapp
cd myapp
bun run dev
Enter fullscreen mode Exit fullscreen mode
import { Elysia } from 'elysia';

new Elysia()
  .get('/', () => 'Hello, Elysia!')
  .get('/user/:id', ({ params: { id } }) => ({ id, name: 'Alice' }))
  .listen(3000);
Enter fullscreen mode Exit fullscreen mode

Validation with TypeBox

import { Elysia, t } from 'elysia';

new Elysia()
  .post('/users', ({ body }) => {
    // body is fully typed: { name: string, email: string, age: number }
    return { created: body };
  }, {
    body: t.Object({
      name: t.String(),
      email: t.String({ format: 'email' }),
      age: t.Number({ minimum: 0 }),
    }),
    response: t.Object({
      created: t.Object({
        name: t.String(),
        email: t.String(),
        age: t.Number(),
      }),
    }),
  })
  .listen(3000);
Enter fullscreen mode Exit fullscreen mode

Groups and Guards

new Elysia()
  .group('/api', (app) =>
    app
      .get('/health', () => ({ status: 'ok' }))
      .guard({
        headers: t.Object({
          authorization: t.String(),
        }),
      }, (app) =>
        app
          .get('/me', ({ headers }) => {
            // headers.authorization is guaranteed to exist
            return { user: 'authenticated' };
          })
          .get('/settings', () => ({ theme: 'dark' }))
      )
  )
  .listen(3000);
Enter fullscreen mode Exit fullscreen mode

Plugins

import { Elysia } from 'elysia';
import { swagger } from '@elysiajs/swagger';
import { cors } from '@elysiajs/cors';
import { jwt } from '@elysiajs/jwt';

new Elysia()
  .use(swagger())
  .use(cors())
  .use(jwt({ name: 'jwt', secret: 'my-secret' }))
  .post('/login', async ({ jwt, body }) => {
    const token = await jwt.sign({ id: 1, role: 'admin' });
    return { token };
  })
  .get('/protected', async ({ jwt, headers }) => {
    const payload = await jwt.verify(headers.authorization);
    if (!payload) return new Response('Unauthorized', { status: 401 });
    return { user: payload };
  })
  .listen(3000);
Enter fullscreen mode Exit fullscreen mode

Eden (Type-Safe Client)

// server.ts
const app = new Elysia()
  .get('/users', () => [{ id: 1, name: 'Alice' }])
  .post('/users', ({ body }) => body, {
    body: t.Object({ name: t.String() }),
  });

export type App = typeof app;

// client.ts
import { edenTreaty } from '@elysiajs/eden';
import type { App } from './server';

const client = edenTreaty<App>('http://localhost:3000');

// Fully typed!
const { data } = await client.users.get();
const { data: created } = await client.users.post({ name: 'Bob' });
Enter fullscreen mode Exit fullscreen mode

Need high-performance data APIs? Check out my Apify actors for web scraping at scale, or email spinov001@gmail.com for custom Elysia/Bun solutions.

Elysia, Hono, or Express — which is your framework for Bun? Share below!

Top comments (0)