Back to Blog
Tutorial9 min read2026-06-30

How to Deploy a Bun Elysia API

Elysia on Bun is one of the fastest JavaScript API stacks around. Here's a production deploy: a slim Bun container, end-to-end types, a managed Postgres, and graceful shutdown.

Ajay Kumar
Ajay Kumar
Founder & DevOps, PandaStack

Why Bun + Elysia

Elysia is an ergonomic, end-to-end type-safe web framework built specifically for Bun. Bun's runtime is fast and its built-in tooling (bundler, test runner, package manager) means fewer moving parts. For APIs where throughput matters, this stack is genuinely impressive. Let's deploy one to production.

Step 1: A typed Elysia API

// src/index.ts
import { Elysia, t } from 'elysia';

const app = new Elysia()
  .get('/health', () => ({ status: 'ok' }))
  .post('/users', ({ body }) => createUser(body), {
    body: t.Object({ name: t.String(), email: t.String() }),
  })
  .listen({ port: Number(process.env.PORT) || 3000, hostname: '0.0.0.0' });

console.log(`Elysia on ${app.server?.port}`);

Elysia validates request bodies against the t.Object schema and infers types end to end — no separate validation layer. Bind to 0.0.0.0 and the injected PORT.

Step 2: Containerize with the official Bun image

FROM oven/bun:1.1 AS build
WORKDIR /app
COPY package.json bun.lockb ./
RUN bun install --frozen-lockfile
COPY . .

FROM oven/bun:1.1-slim
WORKDIR /app
ENV NODE_ENV=production
COPY --from=build /app /app
EXPOSE 3000
CMD ["bun", "run", "src/index.ts"]

--frozen-lockfile ensures the build uses exactly what's in bun.lockb. The slim runtime image keeps things small. On PandaStack you can also let buildpacks detect the project and use Bun as the install command (npm/yarn/pnpm/bun are all supported).

Step 3: Wire a managed Postgres

Bun has a fast native Bun.sql / postgres driver, or use postgres.js. Read DATABASE_URL, which PandaStack injects when you attach a managed PostgreSQL:

import { SQL } from 'bun';
const sql = new SQL(process.env.DATABASE_URL!);

async function createUser(body: { name: string; email: string }) {
  const [user] = await sql`
    INSERT INTO users (name, email) VALUES (${body.name}, ${body.email})
    RETURNING id, name, email`;
  return user;
}

Keep one sql instance for connection pooling; respect your tier's connection limit (50 on free, 300 Pro, 1000 Premium).

Step 4: Graceful shutdown

Drain in-flight requests on SIGTERM so redeploys don't drop connections:

process.on('SIGTERM', async () => {
  await app.stop();
  await sql.end();
  process.exit(0);
});

Step 5: Deploy

Connect your repo and push. The build runs in a rootless BuildKit K8s Job pod, the image ships to Artifact Registry, and Helm deploys it:

git push origin main

Watch live build and app logs in the dashboard, add a custom domain for automatic SSL, and get server-side metrics without instrumenting the app.

Step 6: Performance and sizing

Bun + Elysia is light on memory and fast to start, which makes it well-suited to the free tier's scale-to-zero on spot nodes — cold starts are quick. For sustained high throughput, move to a compute-optimized tier (c1/c2) and add replicas for horizontal scaling.

Trade-offs to know

  • Maturity: Bun and Elysia are newer than the Node + Express/Fastify ecosystem. Pin versions and test thoroughly; some npm packages still assume Node specifics.
  • Native modules: packages requiring node-gyp builds may behave differently under Bun — verify your dependency tree.

What you get is a stack that's hard to beat on raw request throughput and developer ergonomics for typed APIs.

References

  • [Elysia documentation](https://elysiajs.com/)
  • [Bun — Docker guide](https://bun.sh/guides/ecosystem/docker)
  • [Bun SQL / database APIs](https://bun.sh/docs/api/sql)
  • [postgres.js](https://github.com/porsager/postgres)

---

Elysia on Bun gives you typed, fast APIs with minimal ceremony. Containerize it, push to PandaStack's [free tier](https://dashboard.pandastack.io), attach a managed Postgres, and let DATABASE_URL do the wiring.

Ready to deploy?

Start free on PandaStack.

Start free on PandaStack

More in Tutorial

Browse all Tutorial articles →

See also