Skip to content

Eden Treaty is the official Elysia client library that generates a type-safe API client directly from your Elysia application type. Since nestelia uses Elysia as its HTTP layer, Eden Treaty works out of the box.

Setup

bash
bun add @elysiajs/eden

Why decorator routes don't produce types automatically

nestelia registers routes at runtime via Reflect.getMetadata. TypeScript sees decorator arguments (e.g. the path in @Get('/users')) as string — not as the literal type '/users'. This means the Elysia instance's generic parameter never learns about the routes through the decorator path alone.

The solution is a typed schema that describes your API surface — either generated automatically by the nestelia-gen CLI or written manually (co-located with each controller).

Automatic generation — nestelia-gen

nestelia-gen statically analyses your controllers using the TypeScript compiler and generates a fully-typed app.schema.ts — including response types from your method annotations. No app bootstrap, no runtime side effects.

bash
bunx nestelia-gen --tsconfig tsconfig.json src/app.schema.ts

Add return type annotations to your controller methods so the generator can pick them up:

typescript
@Controller("/users")
export class UsersController {
  @Get("/")
  getAll(): User[] { … }

  @Get("/:id")
  getOne(@Param(IdParams) p: Static<typeof IdParams>): User | null { … }

  @Post("/")
  create(@Body(CreateDto) body: Static<typeof CreateDto>): User { … }
}

The generated app.schema.ts looks like:

typescript
// auto-generated by nestelia-gen — do not edit manually
import { Elysia, t } from "elysia";
import type { User } from "./users/user.entity";

export const appSchema = new Elysia()
  .get("/users", (): User[] => undefined as never)
  .get("/users/:id", (): User | null => undefined as never, {
    params: t.Object({ id: t.String() })
  })
  .post("/users", (): User => undefined as never, {
    body: t.Object({ name: t.String() })
  });

export type App = typeof appSchema;

Body/params TypeBox expressions are copied verbatim from your controller file. Response types come from your method annotations. Re-run nestelia-gen whenever you add or change routes.

json
// package.json
{
  "scripts": {
    "gen": "nestelia-gen --tsconfig tsconfig.json src/app.schema.ts",
    "build": "bun run gen && tsc"
  }
}

Auto-generate on startup — gen option

Instead of running nestelia-gen as a separate script, pass gen: true to createElysiaApplication and the schema is regenerated automatically every time the app starts:

typescript
const app = await createElysiaApplication(AppModule, { gen: true });

To customise the output path or tsconfig, pass an object:

typescript
const app = await createElysiaApplication(AppModule, {
  gen: { output: "src/schema.ts", tsconfig: "tsconfig.app.json" },
});

This is equivalent to running bunx nestelia-gen [args] before bootstrap — useful in dev so you never have to remember to re-run the CLI manually.

Then use it with withSchema() and treaty:

typescript
// src/main.ts
import { createElysiaApplication } from "nestelia";
import { AppModule } from "./app.module";
import { appSchema } from "./app.schema"; // ← auto-generated

const app = await createElysiaApplication(AppModule);
const typedServer = app.withSchema(appSchema);
export type App = typeof typedServer;

await typedServer.listen(3000);
typescript
// src/client.ts
import { treaty } from "@elysiajs/eden";
import type { App } from "./main";

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

const { data } = await client.users.get();              // User[]
const { data: user } = await client.users.post({ name: "Alice" }); // User
const { data: found } = await client.users({ id: "1" }).get();     // User | null

Manual schema — co-located with the controller

For teams that prefer explicit schemas without a build step, export a hand-written Elysia schema next to each controller. The schema is the single source of truth for client types; the controller is the single source of truth for business logic.

typescript
// src/users/users.controller.ts
import { Elysia, t, type Static } from "elysia";
import { Controller, Get, Post, Delete, Body, Param } from "nestelia";
import type { User } from "./user.entity";
import { UsersService } from "./users.service";

const IdParams = t.Object({ id: t.String() });
const CreateDto = t.Object({ name: t.String() });

export const usersSchema = new Elysia({ prefix: "/users" })
  .get("/", (): User[] => [])
  .post("/", (): User => ({} as User), { body: CreateDto })
  .get("/:id", (): User | null => null, { params: IdParams })
  .delete("/:id", (): { success: boolean } => ({ success: true }), { params: IdParams });

@Controller("/users")
export class UsersController {
  constructor(private readonly users: UsersService) {}

  @Get("/")    getAll(): User[] { return this.users.findAll(); }
  @Post("/")   create(@Body(CreateDto) body: Static<typeof CreateDto>): User { return this.users.create(body); }
  @Get("/:id") getOne(@Param(IdParams) p: Static<typeof IdParams>): User | null { return this.users.findOne(p.id); }
  @Delete("/:id") remove(@Param(IdParams) p: Static<typeof IdParams>): { success: boolean } { return this.users.remove(p.id); }
}

Compose the schemas from all modules in main.ts:

typescript
// src/main.ts
import { createElysiaApplication } from "nestelia";
import { Elysia } from "elysia";
import { AppModule } from "./app.module";
import { usersSchema } from "./users/users.controller";
import { postsSchema } from "./posts/posts.controller";

const app = await createElysiaApplication(AppModule);
const typedServer = app.withSchema(new Elysia().use(usersSchema).use(postsSchema));
export type App = typeof typedServer;

await typedServer.listen(3000);

How withSchema() works

app.withSchema(schema) does a single runtime cast — this.httpServer as unknown as TSchema — and returns it. The live Nestelia server (with all controller routes compiled in) is returned at runtime; TypeScript sees it as the schema type with full route generics. Zero overhead, zero duplication of request handling.

Testing without listen()

Pass the typed server directly to treaty — Elysia's handle() method processes requests in-process:

typescript
import { treaty } from "@elysiajs/eden";
import { createElysiaApplication } from "nestelia";
import { AppModule } from "./app.module";
import { appSchema } from "./app.schema";

const app = await createElysiaApplication(AppModule);
const client = treaty<App>(app.withSchema(appSchema)); // no listen() needed

const { data } = await client.users.get(); // User[]

Released under the MIT License.