A handler is a function that receives an HTTP request and returns a response. In Elysia, every route is backed by a handler — either a function you define or an inline literal value.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/elysiajs/documentation/llms.txt
Use this file to discover all available pages before exploring further.
Context
Every handler receives a single argument — the context — which carries all information about the current request. The context is unique per request; it is never shared between concurrent requests (except forstore, the global mutable state).
Context properties
| Property | Description |
|---|---|
body | HTTP message body: JSON, form data, or file upload |
query | Parsed query-string as a plain object |
params | Path parameters parsed as a plain object |
headers | Request headers as a plain object |
cookie | Mutable signal store for reading and writing cookies |
store | Global mutable state shared across all requests |
request | The raw Web Standard Request |
server | Bun server instance (Bun only) |
path | Pathname of the request |
Utility functions
| Function | Description |
|---|---|
status(code, body?) | Return a response with a specific HTTP status |
redirect(url, status?) | Redirect to another URL |
set.headers | Append or remove response headers |
Returning a status code
Use thestatus function to return a response with a specific HTTP status code. This is the recommended approach because TypeScript can verify that the returned value matches your response schema.
Prefer
status() over the legacy set.status assignment. The status function enables TypeScript to narrow the return type per status code, which unlocks end-to-end type safety with Eden.set — mutating the response
set is a mutable object on the context that controls the outgoing response.
set.headers
Append or remove response headers before the response is sent.set.headers with status:
Cookies
Elysia exposes cookies as a mutable signal store. Read and write a cookie by accessing its name directly — no explicit get/set calls required.Redirect
Redirect the client to another resource. Whenredirect() is returned, Elysia ignores any other returned value.
File responses
Return a single file using thefile utility, or build a multipart form response with form.
Streaming responses
Return a streaming response by using a generator function withyield. Elysia automatically sets the appropriate transfer-encoding headers.
Server-Sent Events
Wrap yielded values withsse to produce a proper text/event-stream response.
Consuming streams with Eden
When using the Eden treaty client, a streaming route is exposed as anAsyncGenerator that you iterate with for await.
Low-level request access
The raw Web StandardRequest is available on the context for cases where you need headers, body streams, or other low-level details not exposed by Elysia’s helpers.
Server instance (Bun only)
The Bun server instance is available on the context after.listen() is called.