Quick answer
Elysia is a TypeScript web framework built for Bun, and its best trick is that you describe each request body with a small type schema and Elysia validates it before your handler runs. This post builds a small orders API for a shop in Karachi, with real orders and the first error beginners hit.
A shop in Karachi takes orders over the phone and writes each one in a paper notebook. At the end of the day the owner adds up the totals by hand, and one missed line means the sales never match the cash in the drawer. A small API would fix that: send an order in, list them, and see sales per city. Node and Express can do it, but validation is usually hand-written, and a bad request reaches the database before anyone checks it. Elysia runs on Bun instead, starts in a blink, and rejects bad data at the door.
What I would check before using it
My first check is the runtime. Elysia is built for Bun, so the project commits to Bun as the runtime, not Node. Bun starts fast and runs TypeScript directly with no build step. The Koa Tutorial: Build a Node.js Orders API From Scratch post shows the Node side of the same job with more setup.
My second check is validation. Elysia validates the request body against a t.Object schema before the handler runs, so a missing total or a total sent as text never reaches the orders list. The Zod: Stop Bad Data at the Door of Your Checkout post shows the same idea with a separate library; here the schema is built into the framework.
My third check is the ecosystem. Elysia is younger than Express, so there are fewer plugins and fewer ready answers online, and some Node-only libraries will not run under Bun. That is the real trade: speed and a tidy API in exchange for a smaller shelf of ready-made pieces.
My fourth check is the team. If everyone writes TypeScript already, Elysia feels natural, because routes, handlers, and schemas are all typed in one file. If the team is still on plain JavaScript, the learning curve is Bun plus TypeScript at once, which is a lot to take on together.
My verdict, as judgement: use Elysia for a new TypeScript API where Bun is acceptable and input validation matters, which covers most small services and internal tools. Keep Express or Fastify when the project must run on plain Node, or leans on a Node-only library. The recommendation flips if the deployment target has no Bun support, or if the team needs the largest plugin ecosystem available.
Four orders, one API, and a 422 that stopped a bad order
Four orders belong to one small shop: Sana in Karachi, Rs 9,800; Daniyal in Lahore, Rs 13,400; Mahnoor in Islamabad, Rs 11,200; Hamza in Karachi, Rs 6,750.
First, install Elysia. Bun runs TypeScript directly, so there is no build step, and the server listens on port 3000.
$ bun init -y
$ bun add elysia
installed elysia@1.4.32
17 packages installed [1.98s]
$ bun run src/index.ts
Orders API running at http://localhost:3000
Here is the program, which keeps the orders in memory. Each route is one chained call: GET /orders lists everything, POST /orders adds one order validated by a t.Object schema, and GET /orders/totals groups orders per city. The schema runs before the handler, so bad data never reaches the list.
// Orders API for The Practical Developer (Elysia demo).
//
// A small shop in Karachi takes orders over the phone and stores
// them in memory. Three routes:
// GET /orders - list every order on file
// POST /orders - add one order (validated with t.Object)
// GET /orders/totals - sales per city plus a grand total
//
// Run from this folder:
// bun run src/index.ts
// Then in another terminal:
// curl http://localhost:3000/orders
// curl -X POST http://localhost:3000/orders \
// -H "Content-Type: application/json" \
// -d '{"customer":"Sana","city":"Karachi","items":["kurta","dupatta"],"total":9800}'
// curl http://localhost:3000/orders/totals
import { Elysia, t } from "elysia";
type Order = {
id: number;
customer: string;
city: string;
items: string[];
total: number;
};
const orders: Order[] = [];
let nextId = 101;
const app = new Elysia()
.get("/", () => "Orders API is running. Try GET /orders.")
.get("/orders", () => orders)
.post(
"/orders",
({ body }) => {
const order: Order = {
id: nextId,
customer: body.customer,
city: body.city,
items: body.items ?? [],
total: body.total,
};
nextId += 1;
orders.push(order);
return { added: order };
},
{
body: t.Object({
customer: t.String({ minLength: 1 }),
city: t.String({ minLength: 1 }),
items: t.Optional(t.Array(t.String())),
total: t.Number({ minimum: 1 }),
}),
}
)
.get("/orders/totals", () => {
const byCity: Record<string, number> = {};
for (const order of orders) {
byCity[order.city] = (byCity[order.city] ?? 0) + order.total;
}
const grandTotal = orders.reduce((sum, order) => sum + order.total, 0);
const average =
orders.length > 0 ? Math.floor(grandTotal / orders.length) : 0;
return {
ordersOnFile: orders.length,
byCity,
grandTotal,
averageOrder: average,
};
})
.listen(3000);
console.log(
`Orders API running at http://${app.server?.hostname}:${app.server?.port}`
);
$ curl -X POST http://localhost:3000/orders -H "Content-Type: application/json" -d '{"customer":"Sana","city":"Karachi","items":["kurta","dupatta"],"total":9800}'
{"added":{"id":101,"customer":"Sana","city":"Karachi","items":["kurta","dupatta"],"total":9800}}
$ curl -X POST http://localhost:3000/orders -H "Content-Type: application/json" -d '{"customer":"Daniyal","city":"Lahore","items":["sneakers"],"total":13400}'
{"added":{"id":102,"customer":"Daniyal","city":"Lahore","items":["sneakers"],"total":13400}}
$ curl -X POST http://localhost:3000/orders -H "Content-Type: application/json" -d '{"customer":"Mahnoor","city":"Islamabad","items":["handbag","wallet"],"total":11200}'
{"added":{"id":103,"customer":"Mahnoor","city":"Islamabad","items":["handbag","wallet"],"total":11200}}
$ curl -X POST http://localhost:3000/orders -H "Content-Type: application/json" -d '{"customer":"Hamza","city":"Karachi","items":["jacket","shalwar kameez"],"total":6750}'
{"added":{"id":104,"customer":"Hamza","city":"Karachi","items":["jacket","shalwar kameez"],"total":6750}}
The numbers check out: Karachi Rs 9,800 plus Rs 6,750 is Rs 16,550, and the four orders add to Rs 41,150.
$ curl http://localhost:3000/orders/totals
{"ordersOnFile":4,"byCity":{"Karachi":16550,"Lahore":13400,"Islamabad":11200},"grandTotal":41150,"averageOrder":10287}
The thing that went wrong was an order with no total: Elysia rejected it with HTTP 422 before the handler ran, and the error body named the exact field, with type "validation", property "/total", and message "Expected number". The fix is to send a number for total, or mark it optional in the schema and handle the missing value in the handler. A total sent as text failed the same way, and a total of -500 failed with "Expected number to be greater or equal to 1". A mistyped path failed differently: GET /orderz returned HTTP 404 with the plain body "NOT_FOUND".
Common mistakes and how to fix them
First, sending a field with the wrong type. The symptom is HTTP 422 with "Expected number" and property "/total", exactly as the demo showed. The fix is to send a JSON number, not a quoted string.
Second, leaving out a required field. The symptom is the same 422 response, because the field is missing from the body. The fix is to send every required field, or mark it t.Optional with a default in the handler.
Third, restarting the server and losing every order. The symptom is GET /orders returning an empty list after a restart, because the demo stores orders in memory. The fix is to write them to a file or a database before the demo becomes a real service, as the Gin Go Tutorial: Build Fast APIs With Very Little Code post does with a JSON file.
Fourth, running the code with Node instead of Bun. The symptom is an import error for elysia, because the project is built for Bun. The fix is to run it with bun run, or pick a Node framework such as the one in the Fastify: A Fast Node API Without the Ceremony post.
Frequently Asked Questions
Is Elysia the same as Express? No. Express runs on Node and leaves validation to extra libraries, while Elysia runs on Bun and validates request bodies with built-in type schemas before the handler runs.
Does Elysia need a build step? No. Bun runs TypeScript directly, so bun run src/index.ts starts the server with no compiler or bundler in between.
Where do the orders go when the server restarts? Nowhere, they are lost. The demo keeps them in memory, so a real service needs a file or a database for storage.
Is the 422 error the same as a 400 error? Close, but not the same. A 400 means the request itself is malformed, while Elysia returns 422 when well-formed JSON fails the schema, such as a missing or wrongly typed field.
What to remember
- Elysia runs on Bun, and Bun executes TypeScript directly with no build step.
- A t.Object schema validates the request body before the handler runs, and bad data gets a 422.
- The 422 error body names the exact field, with type "validation" and a plain message like "Expected number".
- Routes chain off one Elysia instance, and handlers return plain objects that become JSON.
- Orders in the demo live in memory, so they vanish when the server restarts.
Where this leaves you
Elysia earns its place the day an Express route starts to collect hand-written checks for every field, because the schema replaces them with a few lines that run first. The demo proved it: the server started with one command, four real orders went in with one curl each, and the order with no total was rejected with a 422 that named the missing field instead of slipping into the list. Take an Express endpoint you already run and rebuild it in Elysia. An API that rejects bad data at the door is easier to trust than one that checks it halfway through.
Further reading
- Elysia documentation
- Elysia validation guide
- Bun documentation
- Gin Go Tutorial: Build Fast APIs With Very Little Code
- Koa Tutorial: Build a Node.js Orders API From Scratch
- Fastify: A Fast Node API Without the Ceremony
- Hono: One Small API That Runs Almost Anywhere
- Express Explained: Node's Minimal Web Framework Guide
Waqas Shahid is a software engineer from Karachi with more than 12 years of experience, including 6+ years in engineering leadership. He writes The Practical Developer.

Top comments (0)