Can Elysia support Next.js, OpenAPI, and Vercel AI?
can this comply with these requirements?
- next.js, deployed to vercel
- openapi spec generation
- vercel/ai sdk compatibility (specifically, routes that return
createUIMessageStreamResponse
Now I have a comprehensive picture. Let me provide you with a complete answer:
##Assessment: Can Elysia Comply with Your Requirements?
Based on my analysis of the Elysia codebase and documentation, here's the verdict for each requirement:
###✅ 1. Next.js, deployed to Vercel — YES
Elysia has official support for both:
Next.js Integration:
- Works with Next.js App Router via route handlers
- Create a catch-all route at
app/api/[[...slugs]]/route.ts - Export Elysia's
fetchhandler as GET, POST, etc.
// app/api/[[...slugs]]/route.ts
import { Elysia } from 'elysia'
const app = new Elysia({ prefix: '/api' })
.get('/hello', () => 'Hello from Elysia')
export const GET = app.fetch
export const POST = app.fetch
Vercel Deployment:
- Elysia deploys to Vercel with zero configuration
- Supports both Node.js and Bun runtimes
- Vercel has official documentation for Elysia
The WebStandardAdapter in src/adapter/web-standard/index.ts makes this possible by being WinterTC compliant (Web Standard APIs).
###✅ 2. OpenAPI Spec Generation — YES (First-Class Support)
Elysia has exceptional OpenAPI support via @elysiajs/openapi:
import { Elysia } from 'elysia'
import { openapi, fromTypes } from '@elysiajs/openapi'
const app = new Elysia()
.use(openapi({
references: fromTypes() // Auto-generates from TypeScript types!
}))
.post('/user', ({ body }) => body, {
body: t.Object({
name: t.String()
}),
detail: {
summary: 'Create user',
tags: ['User']
}
})
Key features:
- Auto-generates OpenAPI 3.0 spec from route schemas
- OpenAPI Type Gen — unique to Elysia, generates docs from TypeScript types without manual annotations (works with Drizzle, Prisma, etc.)
- Scalar UI at
/openapiendpoint - Raw JSON spec at
/openapi/json - Supports Zod, Valibot via Standard Schema integration
- Full
detailfield for route documentation per OpenAPI v3 spec
###⚠️ 3. Vercel AI SDK (createUIMessageStreamResponse) — Partially Supported
Elysia has AI SDK integration but with caveats for createUIMessageStreamResponse specifically:
What works:
From src/adapter/utils.ts and the documentation, Elysia natively supports:
- Returning
Responseobjects directly — whichcreateUIMessageStreamResponse()returns ReadableStreamstreaming- Generator functions for manual streaming
- Server-Sent Events (SSE)
import { streamText } from 'ai'
import { openai } from '@ai-sdk/openai'
new Elysia()
.post('/chat', async () => {
// Option 1: Return the Response directly
const result = streamText({
model: openai('gpt-4'),
prompt: 'Hello'
})
return result.toDataStreamResponse()
// Option 2: Return ReadableStream
// return result.toDataStream()
})
The createUIMessageStreamResponse concern:
Looking at how Elysia handles responses in src/adapter/web-standard/handler.ts:
case 'Response':
return handleResponse(response as Response, set, request)
Elysia does pass through Response objects, so createUIMessageStreamResponse() should work since it returns a standard Response. However:
-
Potential issue: Elysia may modify headers or interfere with the stream in
handleResponse(). The code shows it deletescontent-encodingheaders to prevent stream interference. -
Recommended approach: Return the Response directly without schema validation:
import { createUIMessageStreamResponse } from 'ai'
new Elysia()
.post('/api/chat', async ({ body }) => {
return createUIMessageStreamResponse({
status: 200,
stream: yourUIMessageStream
})
}, {
// Skip response validation for streaming
response: {}
})
##Summary
| Requirement | Support | Notes |
|---|---|---|
| Next.js + Vercel | ✅ Full | Zero-config, official support |
| OpenAPI Generation | ✅ Excellent | Best-in-class with Type Gen |
createUIMessageStreamResponse | ⚠️ Should work | Return as raw Response, may need testing |
Recommendation: Elysia is a strong choice for requirements 1 & 2. For requirement 3, I'd suggest:
- Test
createUIMessageStreamResponsein your specific setup - Use
result.toDataStreamResponse()from AI SDK if issues arise (definitely works) - Check Elysia's AI SDK docs for latest patterns
Would you like me to dig deeper into any specific aspect?
END OF POST