### Affix prefix example Source: https://github.com/elysiajs/documentation/blob/main/docs/blog/elysia-07.md Demonstrates the affix feature using the prefix method to bulk-remap all properties of a plugin instance. The example prefixes all properties with 'setup' and then accesses the remapped 'setupCarbon' property. ```typescript const setup = new Elysia({ name: 'setup' }) .decorate({ argon: 'a', boron: 'b', carbon: 'c' }) const app = new Elysia() .use( setup .prefix('decorator', 'setup') ) .get('/', ({ setupCarbon }) => setupCarbon) ``` -------------------------------- ### Hello World Example in TypeScript Source: https://github.com/elysiajs/documentation/blob/main/docs/at-glance.md Basic Elysia server setup with routes for GET and POST requests. Uses the Elysia class to define routes and starts listening on port 3000. Navigate to localhost:3000 to see 'Hello Elysia'. ```typescript import { Elysia } from 'elysia' new Elysia() .get('/', 'Hello Elysia') .get('/user/:id', ({ params: { id }}) => id) .post('/form', ({ body }) => body) .listen(3000) ``` -------------------------------- ### Affix prefix all example Source: https://github.com/elysiajs/documentation/blob/main/docs/blog/elysia-07.md Shows how to remap all properties of a plugin using the prefix method with the 'all' keyword. This prefixes every property with 'setup'. ```typescript const app = new Elysia() .use( setup .prefix('all', 'setup') ) .get('/', ({ setupCarbon }) => setupCarbon) ``` -------------------------------- ### Install @elysia/html Source: https://github.com/elysiajs/documentation/blob/main/docs/plugins/html.md Install the plugin using bun. ```bash bun add @elysia/html ``` -------------------------------- ### Install @elysia/static with bun Source: https://github.com/elysiajs/documentation/blob/main/docs/plugins/static.md Install the static plugin package using the bun package manager. ```bash bun add @elysia/static ``` -------------------------------- ### Install Node.js on Windows Source: https://github.com/elysiajs/documentation/blob/main/docs/quick-start.md Installs Node.js using Chocolatey on Windows. ```bash choco install nodejs ``` -------------------------------- ### Install @elysia/server-timing with bun Source: https://github.com/elysiajs/documentation/blob/main/docs/plugins/server-timing.md Install the Server Timing plugin for ElysiaJS using the bun package manager. ```bash bun add @elysia/server-timing ``` -------------------------------- ### Start development server Source: https://github.com/elysiajs/documentation/blob/main/docs/quick-start.md Starts the Elysia development server with auto-reload on file changes. ```bash bun dev ``` -------------------------------- ### Install @elysia/graphql-yoga Source: https://github.com/elysiajs/documentation/blob/main/docs/plugins/graphql-yoga.md Install the GraphQL Yoga plugin for Elysia using bun. ```bash bun add @elysia/graphql-yoga ``` -------------------------------- ### Install Bun on Windows Source: https://github.com/elysiajs/documentation/blob/main/docs/quick-start.md Command to install Bun on Windows using PowerShell. ```bash powershell -c "irm bun.sh/install.ps1 | iex" ``` -------------------------------- ### Install Node.js on Linux (apt) Source: https://github.com/elysiajs/documentation/blob/main/docs/quick-start.md Installs Node.js using apt on Linux. ```bash sudo apt install nodejs ``` -------------------------------- ### Install Elysia Swagger plugin Source: https://github.com/elysiajs/documentation/blob/main/docs/blog/with-prisma.md Install the @elysiajs/swagger package using bun. ```bash bun add @elysiajs/swagger ``` -------------------------------- ### Install Swagger plugin with bun add Source: https://github.com/elysiajs/documentation/blob/main/docs/plugins/swagger.md Install the @elysia/swagger plugin using the bun package manager. ```bash bun add @elysia/swagger ``` -------------------------------- ### Install @elysia/jwt with bun Source: https://github.com/elysiajs/documentation/blob/main/docs/plugins/jwt.md Install the JWT plugin using the bun package manager. ```bash bun add @elysia/jwt ``` -------------------------------- ### Install Bun on MacOS/Linux Source: https://github.com/elysiajs/documentation/blob/main/docs/quick-start.md Command to install Bun on MacOS or Linux using the official script. ```bash curl -fsSL https://bun.sh/install | bash ``` -------------------------------- ### Install @elysia/cron with bun Source: https://github.com/elysiajs/documentation/blob/main/docs/plugins/cron.md Install the @elysia/cron plugin using the bun package manager. ```bash bun add @elysia/cron ``` -------------------------------- ### Install Apollo GraphQL plugin Source: https://github.com/elysiajs/documentation/blob/main/docs/plugins/graphql-apollo.md Install the required packages for the Apollo GraphQL plugin using bun. ```bash bun add graphql @elysia/apollo @apollo/server ``` -------------------------------- ### Install Node.js on Arch (pacman) Source: https://github.com/elysiajs/documentation/blob/main/docs/quick-start.md Installs Node.js and npm using pacman on Arch Linux. ```bash pacman -S nodejs npm ``` -------------------------------- ### Install @elysia/openapi plugin Source: https://github.com/elysiajs/documentation/blob/main/docs/patterns/openapi.md Installs the @elysia/openapi plugin required to generate the Swagger documentation page. ```bash bun add @elysia/openapi ``` -------------------------------- ### Install Drizzle packages Source: https://github.com/elysiajs/documentation/blob/main/docs/integrations/drizzle.md Run this command to install drizzle-orm and drizzle-typebox. Requires Bun as the package manager. ```bash bun add drizzle-orm drizzle-typebox ``` -------------------------------- ### Eden Treaty 2 Unit Test Example Source: https://github.com/elysiajs/documentation/blob/main/docs/blog/elysia-10.md Use Eden Treaty 2 to write unit tests with auto-completion and type safety, instead of starting a mock server and sending fetch requests. This example imports 'treaty' from '@elysiajs/eden' and tests a simple GET endpoint. ```typescript // test/index.test.ts import { describe, expect, it } from 'bun:test' import { Elysia } from 'elysia' import { treaty } from '@elysiajs/eden' const app = new Elysia().get('/hello', () => 'hi') const api = treaty(app) describe('Elysia', () => { it('return a response', async () => { const { data } = await api.hello.get() expect(data).toBe('hi') }) }) ``` -------------------------------- ### HTTP verb methods example with GET and POST Source: https://github.com/elysiajs/documentation/blob/main/docs/essential/route.md Demonstrates using Elysia's built-in HTTP verb methods. The example defines a GET route for '/' and a POST route for '/hi', both returning simple string responses. ```typescript import { Elysia } from 'elysia' new Elysia() .get('/', 'hello') .post('/hi', 'hi') .listen(3000) ``` -------------------------------- ### Run Netlify development environment Source: https://github.com/elysiajs/documentation/blob/main/docs/integrations/netlify.md Starts the local development server to simulate Netlify Edge Functions. ```bash netlify dev ``` -------------------------------- ### GET request with headers Source: https://github.com/elysiajs/documentation/blob/main/docs/eden/treaty/parameters.md For GET or HEAD methods, the body is omitted and only additional parameters (query, headers, fetch) are accepted. This example shows passing headers. ```typescript import { Elysia } from 'elysia' import { treaty } from '@elysia/eden' const app = new Elysia() .get('/hello', () => 'hi') .listen(3000) const api = treaty('localhost:3000') // ✅ works api.hello.get({ // This is optional as not specified in schema headers: { hello: 'world' } }) ``` -------------------------------- ### Initialize Prisma project Source: https://github.com/elysiajs/documentation/blob/main/docs/blog/with-prisma.md Runs Prisma init to set up the Prisma project structure and generate the .env file. ```ts bunx prisma init ``` -------------------------------- ### Change into project directory Source: https://github.com/elysiajs/documentation/blob/main/docs/quick-start.md Navigate into the newly created project folder. ```bash cd app ``` -------------------------------- ### Define a basic GET route with Elysia Source: https://github.com/elysiajs/documentation/blob/main/docs/tutorial/getting-started/your-first-route/index.md Shows the minimal setup to create an Elysia server with a single GET route at '/' that returns 'Hello World!' and listens on port 3000. ```typescript import { Elysia } from 'elysia' new Elysia() .get('/', 'Hello World!') .listen(3000) ``` -------------------------------- ### Create Elysia project with bun create Source: https://github.com/elysiajs/documentation/blob/main/docs/blog/with-prisma.md Initializes a new Elysia server project named 'elysia-prisma' using Bun's create command. ```bash bun create elysia elysia-prisma ``` -------------------------------- ### Get Request IP Source: https://github.com/elysiajs/documentation/blob/main/docs/essential/handler.md Use `server.requestIP` to get the request IP. This example passes the `request` object to `server.requestIP` and returns the result. The server is only available when the HTTP server is running with `listen`. ```typescript import { Elysia } from 'elysia' new Elysia() .get('/ip', ({ server, request }) => { return server?.requestIP(request) }) .listen(3000) ``` -------------------------------- ### Define GET routes with Elysia.get Source: https://github.com/elysiajs/documentation/blob/main/docs/essential/route.md Defines two GET routes: the root path '/' returns 'hello' and '/hi' returns 'hi'. This is the basic routing example from the 'Route' page. ```javascript const demo1 = new Elysia() .get('/', () => 'hello') .get('/hi', () => 'hi') ``` -------------------------------- ### Install Prisma and Prismabox Source: https://github.com/elysiajs/documentation/blob/main/docs/integrations/prisma.md Install Prisma, Prismabox, and the Bun SQLite adapter. The -d flag adds Prisma as a dev dependency. ```bash bun add @prisma/client prismabox prisma-adapter-bun-sqlite && \ bun add -d prisma ``` -------------------------------- ### Consume Elysia API with edenTreaty Source: https://github.com/elysiajs/documentation/blob/main/docs/eden/treaty/legacy.md Import the server type and use edenTreaty to create a type-safe client. The example shows GET, GET with path parameter, and POST requests with inferred response types. ```typescript // client.ts import { edenTreaty } from '@elysia/eden' import type { App } from './server' // [!code ++] const app = edenTreaty('http://localhost:') // response type: 'Hi Elysia' const { data: pong, error } = app.get() // response type: 1895 const { data: id, error } = app.id['1895'].get() // response type: { id: 1895, name: 'Skadi' } const { data: nendoroid, error } = app.mirror.post({ id: 1895, name: 'Skadi' }) ``` -------------------------------- ### Elysia inline response error example Source: https://github.com/elysiajs/documentation/blob/main/docs/integrations/cloudflare-worker.md This example shows an Elysia server that attempts to return an inline string response, which throws an error on Cloudflare Workers. It is used to illustrate the limitation that you cannot define a Response before server start or use a plugin that does so. ```typescript import { Elysia } from 'elysia' new Elysia() // This will throw error .get('/', 'Hello Elysia') .listen(3000) ``` -------------------------------- ### Create new Elysia project with bun create Source: https://github.com/elysiajs/documentation/blob/main/docs/quick-start.md Bootstraps a new Elysia project named 'app' using the official template. ```bash bun create elysia app ``` -------------------------------- ### Install Netlify CLI Source: https://github.com/elysiajs/documentation/blob/main/docs/integrations/netlify.md Installs the Netlify CLI globally using bun to test Edge Functions locally. ```bash bun add -g netlify-cli ``` -------------------------------- ### Hono middleware context extension example Source: https://github.com/elysiajs/documentation/blob/main/docs/migrate/from-hono.txt Uses Hono's createMiddleware to set and get context variables. The example sets 'version' and 'token' via middleware and uses them in handlers. It is not type safe because the context variables are not statically typed. ```ts import { Hono } from 'hono' import { createMiddleware } from 'hono/factory' const app = new Hono() const getVersion = createMiddleware(async (c, next) => { c.set('version', 2) await next() }) app.use(getVersion) app.get('/version', getVersion, (c) => { return c.text(c.get('version') + '') }) const authenticate = createMiddleware(async (c, next) => { const token = c.req.header('authorization') if (!token) { c.status(401) return c.text('Unauthorized') } c.set('token', token.split(' ')[1]) await next() }) app.post('/user', authenticate, async (c) => { c.get('version') return c.text(c.get('token')) }) ``` -------------------------------- ### Basic Better Auth setup in auth.ts Source: https://github.com/elysiajs/documentation/blob/main/docs/integrations/better-auth.md Creates a Better Auth instance with a PostgreSQL pool as the database. This is the initial setup required before mounting to Elysia. ```ts import { betterAuth } from 'better-auth' import { Pool } from 'pg' export const auth = betterAuth({ database: new Pool() }) ``` -------------------------------- ### Return JSON with Elysia Source: https://github.com/elysiajs/documentation/blob/main/docs/integrations/cheat-sheet.md Elysia automatically converts returned objects to JSON responses. This example returns a JSON object on GET /json. ```typescript import { Elysia } from 'elysia' new Elysia() .get('/json', () => { return { hello: 'Elysia' } }) .listen(3000) ``` -------------------------------- ### Apply OpenTelemetry plugin with span processors Source: https://github.com/elysiajs/documentation/blob/main/docs/plugins/opentelemetry.md Install @elysia/opentelemetry and apply the plugin to any Elysia instance. This example configures a BatchSpanProcessor with an OTLP trace exporter. ```typescript import { Elysia } from 'elysia' import { opentelemetry } from '@elysia/opentelemetry' import { BatchSpanProcessor } from '@opentelemetry/sdk-trace-node' import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto' new Elysia() .use( opentelemetry({ spanProcessors: [ new BatchSpanProcessor( new OTLPTraceExporter() ) ] }) ) ``` -------------------------------- ### Create Elysia project with Bun Source: https://github.com/elysiajs/documentation/blob/main/docs/blog/elysia-supabase.md Initializes a new Elysia project in a folder named 'elysia-supabase'. The last argument is the folder name and can be changed. ```bash bun create elysia elysia-supabase ``` -------------------------------- ### Elysia routing example Source: https://github.com/elysiajs/documentation/blob/main/docs/migrate/from-express.txt Defines GET and POST routes with Elysia using method chaining and a single context object. Returns response directly. ```ts import { Elysia } from 'elysia' const app = new Elysia() .get('/', 'Hello World') .post( '/id/:id', ({ status, params: { id } }) => { return status(201, id) } ) .listen(3000) ``` -------------------------------- ### Run server with deno serve Source: https://github.com/elysiajs/documentation/blob/main/docs/integrations/deno.md Command to start the Deno server with watch mode. ```bash deno serve --watch src/index.ts ``` -------------------------------- ### Express routing example Source: https://github.com/elysiajs/documentation/blob/main/docs/migrate/from-express.txt Defines GET and POST routes with Express, using req and res objects. Requires express.json() middleware for JSON body parsing. ```ts import express from 'express' const app = express() app.get('/', (req, res) => { res.send('Hello World') }) app.post('/id/:id', (req, res) => { res.status(201).send(req.params.id) }) app.listen(3000) ``` -------------------------------- ### Run the compiled binary Source: https://github.com/elysiajs/documentation/blob/main/docs/patterns/deploy.md Starts the compiled server binary. Once compiled, Bun is not needed on the machine to run the server. ```bash ./server ``` -------------------------------- ### Query parameters with $query Source: https://github.com/elysiajs/documentation/blob/main/docs/eden/treaty/legacy.md Appends query parameters to the path using the $query property. The example shows a GET request with name and code query parameters. ```typescript app.get({ $query: { name: 'Eden', code: 'Gold' } }) ``` -------------------------------- ### Allow CORS by top-level domain Source: https://github.com/elysiajs/documentation/blob/main/docs/plugins/cors.md Use a RegExp pattern to allow requests from top-level domains matching 'saltyaom.com'. The example sets up an Elysia app with a GET route. ```typescript import { Elysia } from 'elysia' import { cors } from '@elysia/cors' const app = new Elysia() .use( cors({ origin: /.*\.saltyaom\.com$/ }) ) .get('/', () => 'Hi') .listen(3000) ``` -------------------------------- ### Instrumentation setup file Source: https://github.com/elysiajs/documentation/blob/main/docs/patterns/opentelemetry.md Separate OpenTelemetry setup into its own file so the SDK runs before importing instrumented modules like pg. ```typescript import { opentelemetry } from '@elysia/opentelemetry' import { PgInstrumentation } from '@opentelemetry/instrumentation-pg' export const instrumentation = opentelemetry({ instrumentations: [new PgInstrumentation()] }) ``` -------------------------------- ### Run Prisma migration Source: https://github.com/elysiajs/documentation/blob/main/docs/blog/with-prisma.md Runs the initial Prisma migration to sync the database with the schema and generate the Prisma Client. ```bash bunx prisma migrate dev --name init ``` -------------------------------- ### Fastify routing example Source: https://github.com/elysiajs/documentation/blob/main/docs/migrate/from-fastify.txt Defines GET and POST routes with Fastify, using request and reply objects. The POST route returns a 201 status with the id parameter. ```ts import fastify from 'fastify' const app = fastify() app.get('/', (request, reply) => { reply.send('Hello World') }) app.post('/id/:id', (request, reply) => { reply.status(201).send(request.params.id) }) app.listen({ port: 3000 }) ``` -------------------------------- ### Assign endpoints to Swagger tags Source: https://github.com/elysiajs/documentation/blob/main/docs/plugins/swagger.md Use the 'detail.tags' property in endpoint configuration to assign endpoints to groups. This example assigns a GET endpoint to 'App' and a POST endpoint to 'Auth'. ```typescript app.get('/', () => 'Hello Elysia', { detail: { tags: ['App'] } }) app.group('/auth', (app) => app.post( '/sign-up', async ({ body }) => db.user.create({ data: body, select: { id: true, username: true } }), { detail: { tags: ['Auth'] } } ) ) ``` -------------------------------- ### Install Eden and Elysia with Bun Source: https://github.com/elysiajs/documentation/blob/main/docs/eden/installation.md Install Eden on the frontend and Elysia as a dev dependency. Elysia is needed to infer utility types, and its version must match the server. ```bash bun add @elysia/eden bun add -d elysia ``` -------------------------------- ### Consume Elysia API with edenFetch Source: https://github.com/elysiajs/documentation/blob/main/docs/eden/fetch.md Import the server type and use edenFetch to make type-safe requests. The example shows GET and POST requests with inferred response types. ```typescript import { edenFetch } from '@elysia/eden' import type { App } from './server' const fetch = edenFetch('http://localhost:3000') // response type: 'Hi Elysia' const pong = await fetch('/hi', {}) // response type: 1895 const id = await fetch('/id/:id', { params: { id: '1895' } }) // response type: { id: 1895, name: 'Skadi' } const nendoroid = await fetch('/mirror', { method: 'POST', body: { id: 1895, name: 'Skadi' } }) ``` -------------------------------- ### Basic Apollo GraphQL setup Source: https://github.com/elysiajs/documentation/blob/main/docs/plugins/graphql-apollo.md Sets up an Elysia server with Apollo GraphQL, defining a Book type and a Query resolver. Accessing /graphql shows the Apollo playground. ```typescript import { Elysia } from 'elysia' import { apollo, gql } from '@elysia/apollo' const app = new Elysia() .use( apollo({ typeDefs: gql` type Book { title: String author: String } type Query { books: [Book] } `, resolvers: { Query: { books: () => { return [ { title: 'Elysia', author: 'saltyAom' } ] } } } }) ) .listen(3000) ``` -------------------------------- ### Basic trace listener with onBeforeHandle Source: https://github.com/elysiajs/documentation/blob/main/docs/patterns/trace.md Sets up a trace listener that hooks into the onBeforeHandle lifecycle event. The callback receives a parameter object, but no specific action is taken in this example. This is the minimal setup for observing a lifecycle event. ```ts import { Elysia } from 'elysia' const app = new Elysia() .trace(({ onBeforeHandle }) => { // This is trace listener // hover to view the type onBeforeHandle((parameter) => { }) }) .get('/', () => 'Hi') .listen(3000) ``` -------------------------------- ### Install Elysia with JavaScript using bun Source: https://github.com/elysiajs/documentation/blob/main/docs/quick-start.md Installs Elysia and the Node adapter using bun for a JavaScript project. ```bash bun add elysia @elysia/node ``` -------------------------------- ### Assign endpoints to tags using detail property Source: https://github.com/elysiajs/documentation/blob/main/docs/patterns/openapi.md Assign endpoints to tags using the detail property in the endpoint configuration. The example shows a GET route tagged 'App' and a POST route in a group tagged 'Auth'. ```typescript new Elysia() .get('/', () => 'Hello Elysia', { detail: { tags: ['App'] } }) .group('/auth', (app) => app.post( '/sign-up', ({ body }) => db.user.create({ data: body, select: { id: true, username: true } }), { detail: { tags: ['Auth'] } } ) ) ```