Skip to main content

Getting Started with Bun and ElysiaJS

January 18, 2026
3 min read
Web Development
#TypeScript
#Bun
#API
#ElysiaJS

Why Bun and ElysiaJS?

When building modern backend APIs in TypeScript, developers often have to stitch together a runtime, a bundler, a validation library, and separate OpenAPI tooling. Bun simplifies the runtime side with built-in TypeScript execution, a fast package manager, and native HTTP server performance, while ElysiaJS builds directly on top of Bun's HTTP primitives with end-to-end type safety.

I used this exact stack to power the Portfolio Backend API, pairing ElysiaJS with PostgreSQL, Redis, and Meilisearch to keep response times low and developer ergonomics high.

Setting Up Your First Project

Initialize a new ElysiaJS project with Bun in seconds:

bun create elysia my-api
cd my-api
bun run dev

Unlike Node-based setups that require extra transpilers or watch wrappers, Bun executes TypeScript files directly with near-instant reloads.

Defining Validated Routes with TypeBox

ElysiaJS embeds TypeBox directly into route definitions. When you declare a body, query, or params schema, Elysia validates incoming requests at runtime and infers the exact TypeScript types inside your handler automatically:

import { Elysia, t } from "elysia";

const app = new Elysia({ prefix: "/api/v1" })
  .get("/health", () => ({ status: "ok", timestamp: Date.now() }))
  .post(
    "/projects",
    ({ body, set }) => {
      set.status = 201;
      return {
        created: true,
        slug: body.slug,
        title: body.title,
      };
    },
    {
      body: t.Object({
        title: t.String({ minLength: 3, maxLength: 120 }),
        slug: t.String({ pattern: "^[a-z0-9-]+$" }),
        featured: t.Optional(t.Boolean()),
      }),
    }
  )
  .listen(3000);

export type App = typeof app;

Notice that final line: export type App = typeof app;. That single type export unlocks end-to-end type safety across your frontend or test suite.

End-to-End Type Safety with Eden Treaty

Instead of manually writing API client types or syncing Swagger codegen on every change, Elysia's Eden Treaty client consumes App directly:

import { treaty } from "@elysiajs/eden";
import type { App } from "../server";

const api = treaty<App>("http://localhost:3000");

const { data, error } = await api.api.v1.projects.post({
  title: "Unified Payment Gateway",
  slug: "unified-payment-gateway",
  featured: true,
});

If you rename a field on the server, TypeScript catches the mismatch on the client immediately during CI checks.

Production Patterns Worth Adopting Early

  • Centralized error handling: Use Elysia's onError lifecycle hook to map validation errors, auth failures, and unexpected exceptions into consistent JSON responses without leaking stack traces.
  • Plugin encapsulation: Group auth guards, rate limiters, and feature routes into modular new Elysia() instances so dependencies stay explicit.
  • Graceful shutdown & health checks: Expose a lightweight /health endpoint for Docker/Portainer health checks and close database/Redis connections cleanly on SIGTERM.

Next Steps

Once your routes and validation are in place, the next step is making your database layer just as type-safe. Read Building Type-Safe APIs with Drizzle ORM to see how Drizzle and PostgreSQL pair cleanly with ElysiaJS.