Skip to content

Cheat Sheet

Here are a quick overview for a common Elysia patterns

Hello World

A simple hello world

typescript
import { Elysia } from 'elysia'

new Elysia()
    .get('/', () => 'Hello World')
    .listen(3000)

Custom HTTP Method

Define route using custom HTTP methods/verbs

See Route

typescript
import { Elysia } from 'elysia'

new Elysia()
    .get('/hi', () => 'Hi')
    .post('/hi', () => 'From Post')
    .put('/hi', () => 'From Put')
    .route('M-SEARCH', '/hi', () => 'Custom Method')
    .listen(3000)

Path Parameter

Using dynamic path parameter

See Path

typescript
import { Elysia } from 'elysia'

new Elysia()
    .get('/id/:id', ({ params: { id } }) => id)
    .get('/rest/*', () => 'Rest')
    .listen(3000)

Return JSON

Elysia converts response to JSON automatically

See Handler

typescript
import { Elysia } from 'elysia'

new Elysia()
    .get('/json', () => {
        return {
            hello: 'Elysia'
        }
    })
    .listen(3000)

Return a file

A file can be return in as formdata response

The response must be a 1-level deep object

typescript
import { Elysia, file } from 'elysia'

new Elysia()
    .get('/json', () => {
        return {
            hello: 'Elysia',
            image: file('public/cat.jpg')
        }
    })
    .listen(3000)

Header and status

Set a custom header and a status code

See Handler

typescript
import { Elysia } from 'elysia'

new Elysia()
    .get('/', ({ set, error }) => {
        set.headers['x-powered-by'] = 'Elysia'

        return error(418, "I'm a teapot")
    })
    .listen(3000)

Group

Define a prefix once for sub routes

See Group

typescript
import { Elysia } from 'elysia'

new Elysia()
    .get("/", () => "Hi")
    .group("/auth", app => {
        return app
            .get("/", () => "Hi")
            .post("/sign-in", ({ body }) => body)
            .put("/sign-up", ({ body }) => body)
    })
    .listen(3000)

Schema

Enforce a data type of a route

See Validation

typescript
import { Elysia, t } from 'elysia'

new Elysia()
    .post('/mirror', ({ body: { username } }) => username, {
        body: t.Object({
            username: t.String(),
            password: t.String()
        })
    })
    .listen(3000)

File upload

See Validation#file

typescript
import { 
Elysia
,
t
} from 'elysia'
new
Elysia
()
.
post
('/body', ({
body
}) =>
body
, {
body
:
t
.
Object
({
file
:
t
.
File
({
format
: 'image/*' }),
multipleFiles
:
t
.
Files
()
}) }) .
listen
(3000)

Lifecycle Hook

Intercept an Elysia event in order

See Lifecycle

typescript
import { Elysia, t } from 'elysia'

new Elysia()
    .onRequest(() => {
        console.log('On request')
    })
    .on('beforeHandle', () => {
        console.log('Before handle')
    })
    .post('/mirror', ({ body }) => body, {
        body: t.Object({
            username: t.String(),
            password: t.String()
        }),
        afterHandle: () => {
            console.log("After handle")
        }
    })
    .listen(3000)

Guard

Enforce a data type of sub routes

See Scope

typescript
import { 
Elysia
,
t
} from 'elysia'
new
Elysia
()
.
guard
({
response
:
t
.
String
()
}, (
app
) =>
app
.
get
('/', () => 'Hi')
// Invalid: will throws error, and TypeScript will report error .
get
('/invalid', () => 1)
Argument of type '() => number' is not assignable to parameter of type 'InlineHandler<MergeSchema<UnwrapRoute<InputSchema<never>, TModule<{}, {}>, "/invalid">, MergeSchema<{}, MergeSchema<{}, { body: unknown; headers: unknown; query: unknown; params: {}; cookie: unknown; response: { 200: string; }; }, "">, "">, "">, { ...; } & { ...; }, "/invalid">'. Type '() => number' is not assignable to type '(context: { body: unknown; query: Record<string, string>; params: {}; headers: Record<string, string | undefined>; cookie: Record<string, Cookie<string | undefined>>; ... 8 more ...; error: <const Code extends 200 | "OK", const T extends Code extends 200 ? { ...; }[Code] : Code extends "Continue" | ... 58 more ... |...'. Type 'number' is not assignable to type 'Response | MaybePromise<string | ElysiaCustomStatusResponse<200, string, 200>>'.
) .
listen
(3000)

Custom context

Add custom variable to route context

See Context

typescript
import { Elysia } from 'elysia'

new Elysia()
    .state('version', 1)
    .decorate('getDate', () => Date.now())
    .get('/version', ({
        getDate,
        store: { version }
    }) => `${version} ${getDate()}`)
    .listen(3000)

Redirect

Redirect a response

See Handler

typescript
import { Elysia } from 'elysia'

new Elysia()
    .get('/', () => 'hi')
    .get('/redirect', ({ redirect }) => {
        return redirect('/')
    })
    .listen(3000)

Plugin

Create a separate instance

See Plugin

typescript
import { Elysia } from 'elysia'

const plugin = new Elysia()
    .state('plugin-version', 1)
    .get('/hi', () => 'hi')

new Elysia()
    .use(plugin)
    .get('/version', ({ store }) => store['plugin-version'])
    .listen(3000)

Web Socket

Create a realtime connection using Web Socket

See Web Socket

typescript
import { Elysia } from 'elysia'

new Elysia()
    .ws('/ping', {
        message(ws, message) {
            ws.send('hello ' + message)
        }
    })
    .listen(3000)

OpenAPI documentation

Create interactive documentation using Scalar (or optionally Swagger)

See swagger

typescript
import { Elysia } from 'elysia'
import { swagger } from '@elysiajs/swagger'

const app = new Elysia()
    .use(swagger())
    .listen(3000)

console.log(`View documentation at "${app.server!.url}swagger" in your browser`);

Unit Test

Write a unit test of your Elysia app

See Unit Test

typescript
// test/index.test.ts
import { describe, expect, it } from 'bun:test'
import { Elysia } from 'elysia'

describe('Elysia', () => {
    it('return a response', async () => {
        const app = new Elysia().get('/', () => 'hi')

        const response = await app
            .handle(new Request('http://localhost/'))
            .then((res) => res.text())

        expect(response).toBe('hi')
    })
})

Custom body parser

Create custom logic for parsing body

See Parse

typescript
import { Elysia } from 'elysia'

new Elysia()
    .onParse(({ request, contentType }) => {
        if (contentType === 'application/custom-type')
            return request.text()
    })

GraphQL

Create a custom GraphQL server using GraphQL Yoga or Apollo

See GraphQL Yoga

typescript
import { Elysia } from 'elysia'
import { yoga } from '@elysiajs/graphql-yoga'

const app = new Elysia()
    .use(
        yoga({
            typeDefs: /* GraphQL */`
                type Query {
                    hi: String
                }
            `,
            resolvers: {
                Query: {
                    hi: () => 'Hello from Elysia'
                }
            }
        })
    )
    .listen(3000)

Contributors

The avatar of contributor named as saltyaom saltyaom
The avatar of contributor named as macabeus macabeus
The avatar of contributor named as Dale Seo Dale Seo
The avatar of contributor named as Anthony Morris Anthony Morris
The avatar of contributor named as Suvan Gowri Shanker Suvan Gowri ShankerThe avatar of contributor named as Jevon Thompson Jevon Thompson
The avatar of contributor named as andi hakim arif andi hakim arif
The avatar of contributor named as Tom Zaiger Tom Zaiger
The avatar of contributor named as bogeychan bogeychan
The avatar of contributor named as Kitpipat Jaritwong Kitpipat Jaritwong
The avatar of contributor named as Mousa Aboubaker Mousa Aboubaker

Changelog