Overview - ElysiaJS | ElysiaJS

ID: 2132https://elysiajs.com/eden/treaty/overview.html
Source

Eden Treaty

Eden Treaty is an object‑like representation of an Elysia server that gives you type safety, auto‑completion, and error handling when you interact with it from the client side.

Export the server type

// server.ts
import { Elysia, t } from 'elysia'

const app = new Elysia()
  .get('/hi', () => 'Hi Elysia')
  .get('/id/:id', ({ params: { id } }) => id)
  .post('/mirror', ({ body }) => body, {
    body: t.Object({
      id: t.Number(),
      name: t.String(),
    })
  })
  .listen(3000)

export type App = typeof app

Consume the Elysia API on the client

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

const app = treaty<App>('localhost:3000')

// response type: 'Hi Elysia'
const { data, error } = await app
  .hi
  .get()

Tree‑like syntax

The HTTP path is a resource indicator for a file‑system tree.
Each level in the tree is separated by a /. In JavaScript we use a dot (.) to access deeper resources.

PathTreaty
/.
/hi.hi
/deep/nested.deep.nested

Combining the path with the HTTP method allows us to call the server:

PathMethodTreaty
/GET.get()
/hiGET.hi.get()
/deep/nestedGET.deep.nested.get()
/deep/nestedPOST.deep.nested.post()

Dynamic path

Dynamic path parameters cannot be expressed with a plain dot notation because the parameter name would be lost. Use a function to provide a key value:

// ❌ Unclear what the value is supposed to represent?
treaty.item['skadi'].get()
// ✅ Clear that value is dynamic path is 'name'
treaty.item({ name: 'Skadi' }).get()
PathTreaty
/item.item
/item/:name.item({ name: 'Skadi' })
/item/:name/id.item({ name: 'Skadi' }).id