Integration with Vercel Function
Vercel Function support Web Standard Framework by default, so you can run Elysia on Vercel Function without any additional configuration.
- Create a file at src/index.ts
- In src/index.ts, create or import an existing Elysia server
- Export the Elysia server as default export
typescript
import { Elysia, t } from 'elysia'
export default new Elysia()
.get('/', () => 'Hello Vercel Function')
.post('/', ({ body }) => body, {
body: t.Object({
name: t.String()
})
})
- Add a build script, to bundle the code into a single file using
tsdown
or similar
json
{
"scripts": {
"build": "tsdown src/index.ts -d api --dts"
}
}
- Create vercel.json to rewrite all endpoints to the Elysia server
json
{
"$schema": "https://openapi.vercel.sh/vercel.json",
"rewrites": [
{
"source": "/(.*)",
"destination": "/api"
}
]
}
This configuration will rewrite all requests to the /api
route, which is where Elysia server is defined.
No additional configuration is needed for Elysia to work with Vercel Function, as it supports the Web Standard Framework by default.
If this doesn't work
Make sure to export the Elysia server as default export, and the build output is a single file locate in /api/index.js
.
You can also use Elysia's built-in features like validation, error handling, OpenAPI and more, just like you would in any other environment.
For additional information, please refer to Vercel Function documentation.